sklearn中的KNearest Neighbors - ValueError:查询数据维度必须与训练数据维度匹配

时间:2017-01-04 19:42:29

标签: python-3.x numpy machine-learning scikit-learn nearest-neighbor

我试图对我在UCI机器学习数据库中找到的一些文本识别数据进行k近邻预测。 (this answer

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

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

 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  

df.head()产生:

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

我的错误Feed是这样的:

{{1}}

提前感谢您的帮助,我会在此期间继续寻找答案

3 个答案:

答案 0 :(得分:1)

您的问题是您没有重塑example并且您正在重塑不正确的维度。您正在将X数组重新定义为(16, N),其中NX中的观察数。

因此,当您尝试在example上进行预测时,最终会使用您的分类器预测X重新定义为N列,而不是16列。你训练过的人。

您似乎想要预测单个示例,因此您应该重塑它而不是X。据推测,您需要example = example.reshape(1, -1)而不是example = X.reshape(len(example), -1)

最初,您使用形状example创建(16,)。您应该使用(1, 16)作为维度,将其重塑为(1, -1)。这将生成一个形状为(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

答案 1 :(得分:0)

我隔离了单独的命令行,它是xxxx.predict(示例)问题而不是X.reshape(x,x)-----输入错误或.reshape(x,x)

答案 2 :(得分:0)

另外,代替:

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

另一种方法是:

example = example[np.newaxis, :]