简体   繁体   English

为什么我的 KNeighborsClassifier 返回一些空预测?

[英]Why does my KNeighborsClassifier return some empty predictions?

I'm trying to train a classifier to predict digits from photos and I'm using this dataset: https://www.kaggle.com/ardamavi/sign-language-digits-dataset我正在尝试训练一个分类器来预测照片中的数字,我正在使用这个数据集: https://www.kaggle.com/ardamavi/sign-language-digits-dataset

But when I use the.predict() function it returns for some labels as [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] which does not predict anything.但是,当我使用 the.predict() function 时,它会为某些标签返回 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ,这不会预测任何内容。 The problem also increases as I increase n_neighbors.当我增加 n_neighbors 时,问题也会增加。

import datasetreader
from sklearn.neighbors import KNeighborsClassifier

X_train, X_test, y_train, y_test = datasetreader.get_dataset(
    '/Sign-Language-Digits-Dataset-master/Dataset')

nsamples, nx, ny = X_train.shape
d2_X_train = X_train.reshape((nsamples,nx*ny))

nsamples, nx, ny = X_test.shape
d2_X_test = X_test.reshape((nsamples,nx*ny))

clf = KNeighborsClassifier(n_neighbors = 5).fit(d2_X_train, y_train)
y_pred = clf.predict(X_test)

# Predicting y
print("Amount of testdata to predict on: ", len(X_test)) # prints 413
print("Actual predicts: ", sum(sum(y_pred))) # prints only 270.0, should be 413

I was able to solve it by rearrange y_train to be a vector with the target names, before I had it as a matrix where the index showed the right answer.我能够通过将 y_train 重新排列为具有目标名称的向量来解决它,然后我将它作为一个矩阵,其中索引显示了正确的答案。 Still don't understand why it returned only some empty answers because of that though.尽管如此,仍然不明白为什么它只返回一些空答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM