来自Sklearn,python的错误

时间:2017-11-28 08:47:11

标签: python machine-learning scikit-learn

我正在学习python / ML并遇到过这些错误。我不知道,因为代码很好。

代码

from sklearn import datasets
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
X, y = mglearn.datasets.make_forge()
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
line = np.linspace(-3, 3, 1000).reshape(-1, 1)
for n_neighbors, ax in zip ([1,3,9], axes):
    reg = KNeighborsRegressor(n_neighbors=n_neighbors)
    reg.fit(X_train, y_train)
    ax.plot(line, reg.predict(line))
    ax.plot(X_train, y_train, '^', c=mglearn.cm2(0), markersize=8)
    ax.plot(X_test, y_test, 'v', c=mglearn.cm2(1), markersize=8)

    ax1.set_title(
        "{} neighour(s)\n train score: {:.2f} test score: {:.2f}".format(
            n_neighbors, reg.score(X_train, y_train),
            reg.score(X_test, y_test)))
    ax.set_xlabel("feature")
    ax.set_ylabel("target")
axes[0].legend(['model predictions', 'training data/target',
    'test data/target'], loc='best')

错误

Traceback (most recent call last):
  File "ch2.py", line 161, in <module>
    ax.plot(line, reg.predict(line))
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\sklearn\neighbors\regression.py", line 144, in predict
    neigh_dist, neigh_ind = self.kneighbors(X)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\sklearn\neighbors\base.py", line 385, in kneighbors
    for s in gen_even_slices(X.shape[0], n_jobs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\sklearn\externals\joblib\parallel.py", line 779, in __call__
    while self.dispatch_one_batch(iterator):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\sklearn\externals\joblib\parallel.py", line 625, in dispatch_one_batch
    self._dispatch(tasks)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\sklearn\externals\joblib\parallel.py", line 588, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 111, in apply_async
    result = ImmediateResult(func)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 332, in __init__
    self.results = batch()
  File "C:\Program Files (x86)\Python36-32\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:\Program Files (x86)\Python36-32\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
ValueError: query data dimension must match training data dimension

我似乎无法弄清楚错误是什么,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

正如其他人所说,X和线具有不同数量的特征。这是我的书中的一个示例,完整代码here

X, y = mglearn.datasets.make_wave()

将为您提供本书中使用的1d数据集以及我链接到的笔记本。

答案 1 :(得分:0)

您确实忘记了导入Mglearn。可以通过pip install Mglearn将其安装在Ubuntu中。之后, 进口mglearn 它会开始工作,我也发生了同样的事情!