【问题标题】:Scikit-learn ValueError when implementing logistic regression in Python在 Python 中实现逻辑回归时 Scikit-learn ValueError
【发布时间】:2014-09-17 22:22:59
【问题描述】:

我是机器学习的新手,我正在尝试使用 scikit-learn 在 Python 中设置逻辑回归以进行预测。我已经设置了一个小型模拟数据集,但是在扩展此代码以适用于更大的数据集时,我遇到了有关 ValueError 的问题。这是我的代码:

inputData = np.genfromtxt(file, skip_header=1, unpack=True)
print "X array shape: ",inputData.shape 
inputAnswers = np.genfromtxt(file2, skip_header=1, unpack=True)
print "Y array shape: ",inputAnswers.shape

logreg = LogisticRegression(penalty='l2',C=2.0)
logreg.fit(inputData, inputAnswers)

inputData 二维数组(矩阵)有 149 行和 231 列。我正在尝试将其拟合到具有 149 行的 inputAnswers 数组,正确对应于 inputData 数组的 149 行。但是,这是我收到的输出:

X array shape:  (231, 149)
Y array shape:  (149,)
Traceback (most recent call last):
File "LogRegTry_rawData.py", line 26, in <module>
logreg.fit(inputData, inputAnswers)
File "[path]", line 676, in fit
(X.shape[0], y.shape[0]))
ValueError: X and y have incompatible shapes.
X has 231 samples, but y has 149.

我了解错误的含义,但我不确定为什么会出现这种情况以及如何解决它。任何帮助是极大的赞赏。谢谢!

【问题讨论】:

    标签: python arrays scikit-learn prediction logistic-regression


    【解决方案1】:

    在形状中,第一个元素是行数,第二个元素是列数。所以你有 231 个条目,只有 149 个标签。尝试转置您的数据:inputData.T

    【讨论】:

    • 谢谢!我使用了 np.transpose() 函数,这很有效。我想知道为什么 np.genfromtxt 将其读取为“倒置”,但是...
    • unpack=True 正在转置数据
    猜你喜欢
    • 2017-03-31
    • 2019-09-20
    • 2016-02-21
    • 2016-07-31
    • 2018-11-05
    • 2020-06-12
    • 2018-03-01
    • 2012-06-27
    • 2016-06-17
    相关资源
    最近更新 更多