使用sklearn.datasets.make_classification时,我生成了一个不寻常的错误,如下所示:
从位于http://scikit-learn.org/stable/auto_examples/plot_classifier_comparison.html的代码“plot_classifier_comparison.py”开始,我更改了以下语句(运行正常)
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
random_state=1, n_clusters_per_class=1)
到此(即只添加一个功能):
X, y = make_classification(n_features=3, n_redundant=0, n_informative=2,
random_state=1, n_clusters_per_class=1)
并收到以下错误回溯(其中路径名当然是我的机器的本地名称):
Traceback (most recent call last):
File "F:/Python Packages/ChartyPy3/plot_classifier_comparison.py", line 94, in <module>
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
File "F:\Anaconda\lib\site-packages\sklearn\neighbors\classification.py", line 190, in predict_proba
neigh_dist, neigh_ind = self.kneighbors(X)
File "F:\Anaconda\lib\site-packages\sklearn\neighbors\base.py", line 311, in kneighbors
return_distance=return_distance)
File "binary_tree.pxi", line 1298, in sklearn.neighbors.kd_tree.BinaryTree.query (sklearn\neighbors\kd_tree.c:10427)
ValueError: query data dimension must match training data dimension
现在,我已经确定前两个数据集(即“make_moons”和“make_circles”)在所有分类器中运行良好。但是第三个数据集(即“linearly_separable”)不会:将“KNeighborsClassifier(3)”应用于第三个数据集会从对sklearn.neighbors.kd_tree.BinaryTree.query的调用生成错误回溯。我也尝试使用make_classification的所有默认值,即
X, y = make_classification(n_samples=100, n_features=20, n_informative=2,
n_redundant=2, n_repeated=0, n_classes=2, n_clusters_per_class=2,
weights=None, flip_y=0.01, class_sep=1.0, hypercube=True, shift=0.0,
scale=1.0, shuffle=True, random_state=None)
但是这也产生了相同的错误追溯,并且具有相同的错误消息,即“ValueError:查询数据维度必须与训练数据维度匹配”
我无法理解为什么更改要素总数,或者仅使用默认值作为“make_classification”的输入,应该会产生此错误。我正在使用Python 3.4.1(64位实现)以及开发人员的64位版本的scikit-learn。任何有关此错误的指导和/或如何解决此问题,将不胜感激。
答案 0 :(得分:0)
该示例将每个分类器应用于2-d点网格,以绘制其决策函数。将3-d输入(3个特征)训练的分类器应用于2-d输入将不起作用。