KNeighborsClassifier query data dimension must match training data dimension

962
December 01, 2017, at 09:16 AM

I'm trying to find the training error on a model across different values for k.

Here is my code:

x_eq = np.array(equal_scales[['x1','x2']])
y_eq = np.array(equal_scales[['category']])
y_eq = y_eq.ravel()
train_error_list = []
for i in range(30):
    i = i+1
    knn4 = KNeighborsClassifier(n_neighbors=i)
    knn4.fit(x_eq, y_eq)
    #evaluate accuracy training-error
    train_accuracy = knn2.score(x_eq, y_eq)
    train_error = 1 - train_accuracy
    train_error_list.append(train_error)

x_eq is a numpy array with shape (100,2) looking like this:

array([[ -1.33255739e-01,  -1.68917051e+00],
       [  1.22342923e+00,  -4.58386227e-01],
       [ -7.50266300e-01,   1.84619283e-01],
       [ -5.75475318e-01,   7.98265338e-01],
       [ -2.59622198e-01,  -8.56879897e-01],
       [ -4.54110670e-01,   1.36136079e+00],
       [  6.87128005e-01,  -1.56213731e+00],
       [ -8.52919125e-01,  -2.15160667e-01],
       [  1.08863420e+00,   1.88649799e+00],
       [ -6.08777374e-05,   1.43126563e+00],
       [ -4.00791901e-01,   2.06030370e-01],
   [  7.36267212e-01,   2.29608975e-01],

y_eq is a numpy array with shape (100,) looking like this:

array(['B', 'C', 'B', 'B', 'B', 'B', 'C', 'B', 'C', 'B', 'B', 'C', 'B',
       'C', 'B', 'D', 'B', 'B', 'A', 'B', 'A', 'D', 'B', 'A', 'B', 'C',
       'B', 'B', 'B', 'C', 'B', 'C', 'B', 'B', 'C', 'B', 'B', 'B', '

I get the following error message:

ValueError: query data dimension must match training data dimension

I have fit a very similar model earlier and I am not sure how this one is different from my previous tests. Thank you for your help!

Rent Charter Buses Company
READ ALSO
Dropping rows in Dataframe: labels not contained in axis

Dropping rows in Dataframe: labels not contained in axis

I have a table with three columns: user_id, book_id and ratingSo, one row shows what rating a user gave to a book

720
Python Message Encryption/Decryption Program… How hard is it to decrypt?

Python Message Encryption/Decryption Program… How hard is it to decrypt?

Here's a program I wrote to encrypt a message and decrypt the message

248
How to prevent from blocking IP in scraping Alibaba products?

How to prevent from blocking IP in scraping Alibaba products?

I have a python script to scrape Alibaba productsWhen I start to run it and visited several Alibaba products, Alibaba website start to block my IP because I visited their site every 3 seconds

298
How to pivot a dataframe

How to pivot a dataframe

I've seen a lot of questions that ask about pivot tablesEven if they don't know that they are asking about pivot tables, they usually are

300