Continuously getting error in KNN Algorithm - elbow method?

Viewed 46

I am having problem in this elbow method.

error_rate = []
for i in range(1,40):    
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(X_train,y_train)
    pred_i = knn.predict(X_test)
    error_rate.append(np.mean(pred_i != y_test))

The last line of code is causing problem:

ValueError: Unable to coerce to Series, length must be 1: given 300
1 Answers

Please ensure that the shapes of pred_i and y_test are equivalent. They should be rank-1 arrays.

print(pred_i.shape) # output: (n_test_sample, )
print(y_test.shape) # output: (n_test_sample, )
Related