kelvin.aaa2
kelvin.aaa2

Reputation: 135

KNN model, accuracy(clf.score) returns 0

I am working one a simple KNN model with 3NN to predict a weight, However, the accuracy is 0.0, I don't know why. The code can give me a prediction on weight with 58 / 59.

This is the reproducible code

import numpy as np
from sklearn import preprocessing, neighbors
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.metrics import accuracy_score



#Create df
data = {"ID":[i for i in range(1,11)],
        "Height":[5,5.11,5.6,5.9,4.8,5.8,5.3,5.8,5.5,5.6],
        "Age":[45,26,30,34,40,36,19,28,23,32],
        "Weight": [77,47,55,59,72,60,40,60,45,58]
        }

df = pd.DataFrame(data, columns = [x for x in data.keys()])
print("This is the original df:")
print(df)

#Feature Engineering 
df.drop(["ID"], 1, inplace = True)


X = np.array(df.drop(["Weight"],1))
y = np.array(df["Weight"])


#define training and testing
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size =0.2)

#Build clf with n =3 
clf = neighbors.KNeighborsClassifier(n_neighbors=3)
clf.fit(X_train, y_train)


#accuracy
accuracy = clf.score(X_test, y_test)
print("\n accruacy = ", accuracy)


#Prediction on 11th
ans = np.array([5.5,38])
ans = ans.reshape(1,-1)

prediction = clf.predict(ans)
print("\nThis is the ans: ", prediction)

Upvotes: 2

Views: 1359

Answers (1)

felice
felice

Reputation: 1363

You are classifying Weight which is a continuous (not a discrete) variable. This should be a regression rather than a classification. Try KNeighborsRegressor.

To evaluate your result, use metrics for regression such as R2 score.

If your score is low, that can mean different things: training set too small, test set too different from training set, regression model not adequate...

Upvotes: 1

Related Questions