在Python中实现逻辑回归时Scikit-learn出现ValueError
我刚接触机器学习,想在Python中用scikit-learn来做逻辑回归预测。我已经用一个小的模拟数据集设置好了逻辑回归,但当我把代码扩展到更大的数据集时,遇到了一个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)
我的输入数据是一个二维数组(矩阵),有149行和231列。我想把它和输入答案数组对接,输入答案数组也有149行,正好对应输入数据的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 个回答
1
在这里,形状的第一个元素表示行数,第二个元素表示列数。所以你有231个数据条目,但只有149个标签。试着把你的数据转置一下:inputData.T