YouthPrank
YouthPrank

Reputation: 23

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:

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

1997 1997

987654321

0

Traceback (most recent call last):

File "C:/Python27/qqer.py", line 52, in clf.fit(X, y)

File "C:\Python27\lib\site-packages\sklearn\svm\base.py", line 166, in fit (X.shape[0], y.shape[0]))

ValueError: X and y have incompatible shapes.

X has 1 samples, but y has 1997.

---> If i get printed out the same shapes for X and y, why would I get such error? Any idea guys?

Upvotes: 2

Views: 8124

Answers (1)

Fred Foo
Fred Foo

Reputation: 363817

The shape of X must be (n_samples, n_features) as explained in the SVC.fit docstring. 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).

Upvotes: 4

Related Questions