• Register
0 votes
198 views

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

Please log in or register to answer this question.

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:

x.shape

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 581 views
581 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
0 votes
1 answer 42 views
42 views
Problem: 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 continuous
asked Jan 9 Mashhoodch 9.1k points
1 vote
2 answers 1.3K views
1.3K 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, 2020 stewart 4k points
0 votes
1 answer 7 views
7 views
Problem input contains nan, infinity or a value too large for dtype('float64').
asked Feb 11 charles mathews 3.8k points
0 votes
1 answer 20 views
20 views
Problem indexerror: arrays used as indices must be of integer (or boolean) type
asked Feb 11 charles mathews 3.8k points
0 votes
2 answers 38 views
38 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, 2020 psandprop 2.4k points
0 votes
1 answer 4 views
4 views
Problem : Reshape your data either using array.reshape(-1, 1)
asked 1 day ago Shovo210 2.1k points
0 votes
1 answer 19 views
19 views
Problem: np.nan is an invalid document, expected byte or unicode string.
asked 3 days ago charles mathews 3.8k points
0 votes
1 answer 157 views
157 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, 2020 jwilliam 3.9k points
0 votes
1 answer 147 views
147 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