• Register
0 votes

Problem  :

I am new to Python, I want to do the simple nearest neighbors classification but I am always facing below error while trying to execute following code.

ValueError: Found input variables with inconsistent numbers of samples: [489, 1890]
My code snippet as below :
myneigh = KNeighborsClassifier(n_neighbors=3)
myneigh.fit(X_bus, y_bus)

How can I fix above error?

8 4 2
2,300 points

1 Answer

0 votes

Solution :

I have faced such error earlier. Your error wants to tell you that a size of your X_bus and y_bus samples are not at all same. So my suggestion to you is to revisit your bus test split and then you need to make sure that you are executing it currectly. You can do it as shown below:

X_bus, X_test, y_bus, y_test = bus_test_split(X, y)
myneigh = KNeighborsClassifier(n_neighbors=3)
myneigh.fit(X_bus, y_bus)
Wrong order will again produce the same error "ValueError: Found input variables with inconsistent numbers of samples”

Also note the Scikit-Learn will not accept the rank 1 array if you try to call the shape method on the x as below:


Then it will surely return you something which is similar to this (30,), where 30 is your number of rows so it should be exactly like (30,1).

So to make it work you can try using reshape as shown below:

x = dataset.iloc[:,0]
x = x.reshape((len(x),1))
5 2 1
4,980 points

Related questions

1 vote
1 answer 322 views
Problem : I am facing bellow strange error undefinedmetricwarning: precision is ill-defined and being set to 0.0 due to no predicted samples.
asked Nov 8, 2019 peterlaw 6.9k points
1 vote
2 answers 890 views
Problem : Currently I am trying to develop the tweet classifier. I have already trained the knn classifier with the tfidf dataset. In this dataset each and every row has the length of 3.173. After training a model it will load it into the file to ... my training data efficiently. Please find below the error which I am facing. ValueError: query data dimension must match training data dimension.
asked Apr 23 stewart 4k points
0 votes
2 answers 13 views
I'm trying to fit an SGDRegressor to my data and then check the accuracy. The fitting works fine, but then the predictions are not in the same datatype(?) as the original target data, and I get the error ValueError: Can't handle mix of multiclass and ... fit(X, y) predictions = clf.predict(X_test) print "Accuracy:", ms.accuracy_score(y_test,predictions) What should I do differently? Thank you!
asked Oct 25 psandprop 2.4k points
0 votes
1 answer 104 views
Problem : I have below error for trying to load the saved SVM model. I have tried uninstalling the sklearn, NumPy and SciPy, and reinstalling a latest versions all-together again (using pip). I am still facing below error. “Runtimewarning : Numpy.dtype size changed, may indicate binary incompatibility” How to get rid of the above mentioned issue?
asked Jan 21 jwilliam 3.9k points
0 votes
1 answer 115 views
Problem : Help needed with this error runtimewarning: numpy.dtype size changed, may indicate binary incompatibility. expected 96, got 88
asked Nov 8, 2019 peterlaw 6.9k points