KNeighborsClassifier .predict() function doesn't work
<p>i am working with KNeighborsClassifier algorithm from scikit-learn library in Python. I followed basic instructions e.g.  split my data and labels into training and test data, then trained my model on a training data. Now I am trying to predict accuracy of testing data but get an error. Here is my code:</p>

<pre><code>from sklearn.neighbors import KNeighborsClassifier 
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score

 data_train, data_test, label_train, label_test = train_test_split(df, labels, 
                                                              test_size=0.2,
                                                              random_state=7)
mod = KNeighborsClassifier(n_neighbors=4)
mod.fit(data_train, label_train)
predictions = mod.predict(data_test)

print accuracy_score(label_train, predictions)
</code></pre>

<p>The error I get:</p>

<pre><code>ValueError: Found arrays with inconsistent numbers of samples: [140 558]
</code></pre>

<p>140 is the portion of training data and 558 is the test data based on the test_size=0.2 (my data set is 698 samples). I verified that labels and data sets are of the same size 698. However, I get this error which is basically trying to compare test data and training data sets.</p>

<p>Does anyone knows what is wrong here? What should I use to train my model against to and what should I use to predict the score?</p>

<p>Thanks!</p>

<p>You should calculate the <code>accuracy_score</code> with <code>label_test</code>, not <code>label_train</code>. You want to compare the actual labels of the test set, <code>label_test</code>, to the predictions from your model, <code>predictions</code>, for the test set.</p>

python pandas machine-learning