import numpy as np
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_absolute_error ,mean_squared_log_error ,r2_score
X_train = np.array([
[158 , 1 ],
[170 , 1 ],
[183 , 1 ],
[191, 1 ],
[155 , 0 ],
[163 , 0 ],
[180 , 0 ],
[158 , 0 ] ,
[170 , 0 ]
])
y_train= np.array([64 ,86, 84, 80, 49, 59 , 67, 54 ,67])
X_test = np.array([
[168 ,1 ],
[180 , 1],
[160 ,0],
[169 ,0]
])
y_test = [65 , 96 , 52 , 67]
k = 3
clf = KNeighborsRegressor( n_neighbors = k )
clf.fit(X_train , y_train)
predictions = clf.predict(X_test)
print("predicte weight :%s"% predictions)
print("really weight :%s" % y_test )
print("-- --")
print("Coefficient of detemination 决定系数(拟合优度r2):%s " % r2_score(y_test ,predictions))
print("-- --")
print("平均绝对值误差(mean_absolute_error) :%s " % mean_absolute_error(y_test ,predictions))
print("-- --")
print("均方差(mean-squared-error) :%s " % mean_squared_log_error(y_test , predictions ))
'''
print("另一种方法计算:")#应该不行,因为他不能轻易转换为01二进制
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report
lb =LabelBinarizer()
y = lb.transform(y_test)
predictions = lb.transform(predictions)
print(classification_report(y , predictions , target_names= ["name"] , labels = [1] ) )
'''
predicte weight :[70.66666667 79. 59. 70.66666667]
really weight :[65, 96, 52, 67]
-- --
Coefficient of detemination 决定系数(拟合优度r2):0.6290565226735438
-- --
平均绝对值误差(mean_absolute_error) :8.333333333333336
-- --
均方差(mean-squared-error) :0.01551486028554209
Process finished with exit code 0