KNearest Neighbors in sklearn - ValueError: query data dimension must match training data dimension

KNearest Neighbors in sklearn - ValueError: query data dimension must match training data dimension

我正在尝试对我在 UCI 机器学习数据库中找到的一些文本识别数据进行 k 最近邻预测。 (https://archive.ics.uci.edu/ml/datasets/Letter+Recognition)

我交叉验证了数据并测试了准确性,没有任何问题,但我不能 运行 classifier.predict()。任何人都可以阐明为什么我会收到此错误吗?我在 sklearn 网站上阅读了维度诅咒,但实际上我在修复代码时遇到了麻烦。

到目前为止我的代码如下:

import pandas as pd
import numpy as np
from sklearn import preprocessing, cross_validation, neighbors

df = pd.read_csv('KMeans_letter_recog.csv')    

X = np.array(df.drop(['Letter'], 1))
y = np.array(df['Letter'])

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size = 0.2) #20% data used

clf = neighbors.KNeighborsClassifier()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test) #test
print(accuracy) #this works fine

example = np.array([7,4,3,2,4,5,3,6,7,4,2,3,5,6,8,4])
example = X.reshape(len(example), -1)

prediction = clf.predict(example)
print(prediction) #error

df.head() 产生:

 Letter   x-box   y-box   box_width   box_height   on_pix   x-bar_mean  \
0      T       2       8           3            5        1            8   
1      I       5      12           3            7        2           10   
2      D       4      11           6            8        6           10   
3      N       7      11           6            6        3            5   
4      G       2       1           3            1        1            8   

    y-bar_mean   x2bar_mean   y2bar_mean   xybar_mean   x2y_mean   xy2_mean  \
0           13            0            6            6         10          8   
1            5            5            4           13          3          9   
2            6            2            6           10          3          7   
3            9            4            6            4          4         10   
4            6            6            6            6          5          9   

    x-ege   xegvy   y-ege   yegvx  
0       0       8       0       8  
1       2       8       4      10  
2       3       7       3       9  
3       6      10       2       8  
4       1       7       5      10  

我的错误提要是这样的:

Traceback (most recent call last):
  File "C:\Users\jai_j\Desktop\Python Projects\K Means ML.py", line 31, in <module>
    prediction = clf.predict(example)
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\neighbors\classification.py", line 145, in predict
    neigh_dist, neigh_ind = self.kneighbors(X)
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\neighbors\base.py", line 381, in kneighbors
    for s in gen_even_slices(X.shape[0], n_jobs)
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\externals\joblib\parallel.py", line 608, in dispatch_one_batch
    self._dispatch(tasks)
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\externals\joblib\parallel.py", line 571, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 109, in apply_async
    result = ImmediateResult(func)
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 326, in __init__
    self.results = batch()
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "C:\Users\jai_j\Desktop\Python Projects\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in <listcomp>
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "sklearn\neighbors\binary_tree.pxi", line 1294, in sklearn.neighbors.kd_tree.BinaryTree.query (sklearn\neighbors\kd_tree.c:11325)
ValueError: query data dimension must match training data dimension

在此先感谢您的帮助,同时我会继续寻找答案

您的问题是您没有重塑 example 并且您正在重塑到不正确的尺寸。您正在将 X 数组重塑为 (16, N),其中 NX.

中的观察数

因此,当您尝试对 example 进行预测时,您最终会使用分类器来预测 X 重塑为 N 列,而不是 16 列在你训练的那一个。

你似乎想预测你的单个例子,所以你应该重塑它而不是 X。据推测,您想要 example = example.reshape(1, -1) 而不是 example = X.reshape(len(example), -1)

最初,您创建 example,形状为 (16,)。您应该使用 (1, -1) 作为尺寸将其重塑为 (1, 16)。这将生成一个形状为 (1, 16) 的数组,它适合您的分类器。

为了清楚起见,请尝试将您的代码更改为:

example = np.array([7,4,3,2,4,5,3,6,7,4,2,3,5,6,8,4])
example = example.reshape(1, -1)

prediction = clf.predict(example)
print(prediction) # shouldn't error anymore

我隔离了单个命令行,这是 xxxx.predict(example) 问题而不是 X.reshape(x,x)----- 输入错误或 .reshape(x,x)

另外,代替:

example = example.reshape(1,-1),

另一种选择是:

example = example[np.newaxis, :]