python - sklearn.KNeighborsClassifier gives very low accuracy score


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(), train_y) 

y__pred = mimi.predict(train_X)

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([[0] *len(a), [1] *len(b)])

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


More Articles

reactjs - What is the benefit of @emotion/core over emotion for a React project?

If I install emotion then the API is nice and clear: package.json:"dependencies": { "emotion": "^10.0.9",React component: import React from "react";import { css } from "emotion";const someStyle = css` display: none;`function MyComponent() { return ( <div className={someStyle} /> );}Ho

specifications - Essential techniques for pinpointing missing requirements?

An initial draft of requirements specification has been completed and now it is time to take stock of requirements, review the specification. Part of this process is to make sure that there are no sizeable gaps in the specification. Needless to say that the gaps lead to highly inaccurate estimates,

javascript - jQuery .load: any way to load the same page when page is refreshed

So I have a website that loads pages to a container div:function goto(addr) { $("#content").load(addr);}and a link that executes it<a href="#" id="aboutus" onclick="goto('page/aboutus.php');">About us</a>My problem is that whenever the page is refreshed, the loaded content resets to t

cookies - PHP Unique Computer ID

Is it possible for php(or javascript in the worst case) to create a unique id for a user that is not cookie or ip dependant. I have seen on that on each city the count only goes up once a day(it has a unique id for everyone i think) and even if I delete my cookies and refresh ip it st

c# - Regular expression to match a string that contains only numbers, not letters

My code is currently using the following Regex expression which matches on numbers:Regex numberExpression = new Regex(@"(?<Number>\d+)");This current works fine for input strings like "1", "100", "1a", "a1", etc....But I want to change it so it does NOT match when the input string contains a l

Scala Collection in JSF

I have developed a Scala and JSF application for learning purpose. In this app I have to convert all my Scala collection objects to Java cllectios before it get rendered in JSF. Is there any easy way this can be achived with something like ScalaElResolver, if yes anybody have a sample code for Scala

css - How to add equal padding between buttons? want to put equal space between the 4 buttons at the top, but I'm having trouble. At first the "social-buttons" id was in a DIV floating left, but that caused the slider to get pushed to the right.So I changed everything to and now the padding style doesn't seem to have an

javascript - Why are props being shared between seperate instances of an emotion-js component?

Why does emotion-js appear to share the props from seperate component instances?<Button isDisabled={'true'} /><Button />const Button = styled.button` background-color: ${props => props.isDisabled !== 'true' ? 'green' : 'grey'};`;export default (props) => { return (<Button>

mysql - PHP-FPM crashes when having too many users while doing a heavy job

I have a Server running Apache/2.2.22 (Debian), PHP 5.6.17 as FPM and MySQL 5.6.25.The project runs using a CMS called Redaxo (I don't think it's that important, but I'll tell ya anyway). In Redaxo there are some functions which take some time (e.g. deleting cache and rebuilding it takes 1-2 minutes

Java's "forEach" to Scala

How can the following be converted to scala? If I live it as it is, I'm getting a big type mismatch expected.entrySet().forEach(entry -> {..})I tried specifying entry to java.util.Map.Entry, or changing to scala foreach, doesn't work.Let me know if you need any more info/code, and I'll create som