简体   繁体   中英

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

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 problem also increases as I increase 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. Still don't understand why it returned only some empty answers because of that though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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