使用Knn分类器时形状错误无效

问题描述 投票:1回答:1

以下是XY可变形状:

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)

## Output for shapes 
X_train.shape  = (970, 298) 
X_test.shape   = (478, 298)
len(y_train)   =  970
len(y_test)    =  478

现在我从Multi-output分配Knn分类器:

knn = KNeighborsClassifier(n_neighbors=3)
classifier = MultiOutputClassifier(knn, n_jobs=-1)
classifier.fit(X_train,y_train)

predictions = classifier.predict(X_test)
print classifier.score(y_test,predictions)

当我尝试运行它时,我收到以下错误:

ValueError:X和Y矩阵的维度不兼容:X.shape [1] == 3而Y.shape [1] == 298

现在我可以看出错误是与变量形状有关的东西,也许我正在将它们混合在一起进行训练或测试。

尝试搜索但无济于事,我犯了什么错误?

样品:

X = (0, 96) 0.24328157992528274
(0, 191)    0.4086854706249901
(0, 279)    0.3597892480519696
(0, 209)    0.6262243704015803
(0, 287)    0.15142673105175225
(0, 44) 0.2839334104854308
(0, 31) 0.27493029497336746
(0, 62) 0.2702778021025414

Y  =[1252, 12607, 12596], [12480, 12544, 12547], [1252, 12607, 12547], [12480, 12607, 12547], [12480, 12607, 12596], [1252, 12607, 12547], [12480, 12544, 12547], [1252, 12607, 12596], [1252, 12607, 12596], [12480, 12544, 12547], [12480, 12607, 12596]
python machine-learning scikit-learn classification knn
1个回答
0
投票

来自Documentation:

Returns the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters: 
X : array-like, shape = (n_samples, n_features)
Test samples.

y : array-like, shape = (n_samples) or (n_samples, n_outputs)
True labels for X.

sample_weight : array-like, shape = [n_samples], optional
Sample weights.

Returns:    
score : float
Mean accuracy of self.predict(X) wrt. y

因此,你需要给分数函数Xy而不是y_truey_pred

尝试:

print classifier.score(X_test, np.array(y_test))
© www.soinside.com 2019 - 2024. All rights reserved.