KNN模型,精度(clf.score)返回0



我正在使用一个简单的KNN模型,用3NN来预测权重,然而,准确度是0.0,我不知道为什么。这个代码可以给我一个58/59的体重预测。

这是可复制的代码

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)

您正在对Weight进行分类,它是一个连续(而不是离散(变量。这应该是一个回归,而不是一个分类。试试KNeighborsRegressor。

要评估您的结果,请使用R2分数等回归指标。

如果你的分数很低,那可能意味着不同的事情:训练集太小,测试集与训练集太不同,回归模型不充分。。。

最新更新