在Python中实现逻辑回归时Scikit-learn ValueError

时间:2014-07-26 00:23:20

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

我是机器学习的新手,我正在尝试使用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 2D数组(矩阵)有149行和231列。我试图将它装入inputAnswers数组,该数组有149行,正确对应于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.

我理解错误的含义,但我不确定它在这种情况下出现的原因以及解决方法。任何帮助是极大的赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

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