【发布时间】:2017-05-19 05:28:04
【问题描述】:
我正在尝试对我在 UCI 机器学习数据库中找到的一些文本识别数据进行 k 最近邻预测。 (https://archive.ics.uci.edu/ml/datasets/Letter+Recognition)
我交叉验证了数据并测试了准确性,没有任何问题,但我无法运行分类器.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
提前感谢您的帮助,我会在此期间继续寻找答案
【问题讨论】:
-
我的猜测是您尝试输入的训练数据与预测维度不匹配,很可能是 n 维度。如果你有 10 个特征,而你传递了 9 个,那可能是个问题。
-
感谢您的回复 - 我检查了功能;有 16 个特征,我确保在预测时使用了相同的数字。不幸的是,我仍然收到错误消息。
标签: python-3.x numpy machine-learning scikit-learn nearest-neighbor