[英]X and y have incompatible shapes
I was trying to fit a classifier on a 1 dimensional feature vector of 1997 training examples with a sample of the same size containing my y's: 我试图在1997年训练示例的一维特征向量上使用包含y的相同大小的样本拟合分类器:
clf = svm.SVC()
j = 0
a = 0
listX = []
listY = []
while a <= 1996:
ath_X = "".join(linesplit[a])
listX = listX + [int(ath_X)]
a+=1
while j <= 1996:
jth_Y = "".join(linesplit1[j])
listY = listY + [((int(jth_Y))-1)]
j+=1
X = np.array(listX)
y = np.array(listY)
print("%s %s %s %s" % ('Dimension of X: ', len(X), 'Dimension of y: ', len(y)))
print("%s %s" % (X.shape[0], y.shape[0]))
print(X[1996])
print(y[1996])
clf.fit(X, y)
ficheiro1.close()
ficheiro.close()
print("We're done")
---> This is what gets printed out: --->这是打印出来的:
Dimension of X: 1997 Dimension of y: 1997 X维度:1997年y维度:1997年
1997 1997 1997 1997
987654321 987654321
0 0
Traceback (most recent call last): 追溯(最近一次通话):
File "C:/Python27/qqer.py", line 52, in clf.fit(X, y) clf.fit(X,y)中的文件“ C:/Python27/qqer.py”,第52行
File "C:\\Python27\\lib\\site-packages\\sklearn\\svm\\base.py", line 166, in fit (X.shape[0], y.shape[0])) 适合的文件“ C:\\ Python27 \\ lib \\ site-packages \\ sklearn \\ svm \\ base.py”,行166(X.shape [0],y.shape [0]))
ValueError: X and y have incompatible shapes. ValueError:X和y具有不兼容的形状。
X has 1 samples, but y has 1997. X有1个样本,但y有1997。
---> If i get printed out the same shapes for X and y, why would I get such error? --->如果我将X和y的形状打印出来,为什么会出现这种错误? Any idea guys?
有想法吗?
The shape of X
must be (n_samples, n_features)
as explained in the SVC.fit
docstring. X
的形状必须为(n_samples, n_features)
如SVC.fit
文档字符串中所述。 A 1-d array is interpreted as a single sample (for convenience when doing predictions on single samples). 一维数组被解释为单个样本(为方便起见,对单个样本进行预测)。 Reshape your
X
to (n_samples, 1)
. 将
X
重塑为(n_samples, 1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.