使用 Knn 分类器时出现无效形状错误
Invalid shape error while using Knn Classfier
以下是 X
和 Y
变量形状:
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
现在我从 Knn
分配 Multi-output
分类器:
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: Incompatible dimension for X and Y matrices: X.shape[1] ==
3 while 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]
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
因此,您需要为评分函数提供 X
、y
而不是 y_true
和 y_pred
尝试:
print classifier.score(X_test, np.array(y_test))
以下是 X
和 Y
变量形状:
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
现在我从 Knn
分配 Multi-output
分类器:
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: Incompatible dimension for X and Y matrices: X.shape[1] == 3 while 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]
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
因此,您需要为评分函数提供 X
、y
而不是 y_true
和 y_pred
尝试:
print classifier.score(X_test, np.array(y_test))