简体   繁体   English

X和y的形状不兼容

[英]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.

相关问题 sklearn错误:“ X和y的形状不兼容。” - sklearn error: “X and y have incompatible shapes.” scikit-learn文本文档的分类ValueError:X和y的形状不兼容 - scikit-learn Classification of text documents ValueError: X and y have incompatible shapes x和y必须具有相同的第一尺寸,但形状为(30,)和(1,) - x and y must have same first dimension, but have shapes (30,) and (1,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 不能大于 2-D,但具有 (2, 1, 1) 和 (2,) 形状 - ValueError: x and y can be no greater than 2-D, but have shapes (2, 1, 1) and (2,) ValueError:输入具有不兼容的形状 - ValueError: Inputs have incompatible shapes 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) X 和 Y 矩阵的尺寸不兼容 - Incompatible dimension for X and Y matrices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM