﻿ python - sklearn.KNeighborsClassifier gives very low accuracy score - Oipapio- oipapio.com

# python - sklearn.KNeighborsClassifier gives very low accuracy score Question:

I am new to machine learning.

I created a data, random numbers in two sets. I am trying how to find a sample, however when doing following, I receive very low accuracy score:

``````from random import randint as R
from matplotlib import pyplot as plt
import numpy as np

from sklearn.neighbors import KNeighborsClassifier as KNC
from sklearn.cross_validation import train_test_split as tts
from sklearn.metrics import accuracy_score

a = [R(100,200) for x in range(100)]

b = [R(1000,2000) for x in range(100)]

c = a+b

X = np.array(c).reshape(len(c),1)
y = np.arange(len(c))

train_X, test_X, train_y,test_y = tts(X,y,test_size=0.4)

mimi = KNC()
mimi.fit(train_X, train_y)

y__pred = mimi.predict(train_X)
print(accuracy_score(train_y,y__pred))
print(mimi.score(train_X,train_y))
``````

I receive a result of 0.18... What exactly does this mean? That a prediction score is just 18%? Please, can you explain to me in most simple way. I would really appreciate it. 1 Answer:

By doing `y = np.arange(len(c))` you have `c` different classes (here 200 classes) with only one example for each class. Learning the nearest neighbors on such a setup does not have any sense.

What you want (If I'm guessing right) is to have one class for data `a` and another class for data `b`. Change `y` to:

``````y = np.concatenate([ *len(a),  *len(b)])
``````

You'll see that you obtain an accuracy score of 1.0, which means that you successfully classify all your testing example.