ValueError: Found input variables
来源:4-6 网格搜索与k近邻算法中更多超参数
慕仰9422463
2020-02-12
%%time
from sklearn.model_selection import GridSearchCV
grid_search = GridSearchCV(knn_clf, param_grid)
grid_search.fit(X_train, y_train)
ValueError Traceback (most recent call last)
<ipython-input-19-10311b8b707f> in <module>
1 from sklearn.model_selection import GridSearchCV
2 grid_search = GridSearchCV(knn_clf, param_grid)
----> 3 grid_search.fit(X_train, y_train)
D:\Software\Anaconda\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
628 refit_metric = 'score'
629
--> 630 X, y, groups = indexable(X, y, groups)
631 n_splits = cv.get_n_splits(X, y, groups)
632
D:\Software\Anaconda\lib\site-packages\sklearn\utils\validation.py in indexable(*iterables)
228 else:
229 result.append(np.array(X))
--> 230 check_consistent_length(*result)
231 return result
232
D:\Software\Anaconda\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
203 if len(uniques) > 1:
204 raise ValueError("Found input variables with inconsistent numbers of"
--> 205 " samples: %r" % [int(l) for l in lengths])
206
207
ValueError: Found input variables with inconsistent numbers of samples: [1437, 360]
写回答
1回答
-
根据你提供的错误信息,X_train 和 y_train 中的样本数量不匹配。使用 shape 看一下这两个变量的维度是不是你要的?
这个课程的所有代码都可以通过慕课网的下载区下载。尝试下载课程的官方代码,在你的环境下运行,看是否有问题?如果没有问题,请仔细调试比对,看看自己的程序问题在哪里?
传送门:https://git.imooc.com/coding-169/coding-169
继续加油!:)
012020-02-15
相似问题