knnm模型回归

58 篇文章 0 订阅
16 篇文章 0 订阅
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)#将数据传入了clf这个对象中,这个对象已经能有预测的能力了已经有了一个趋势了就是有了那个回归函数了

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("-- --")
#平均绝对值误差(mean_absolute_error)
print("平均绝对值误差(mean_absolute_error) :%s " % mean_absolute_error(y_test ,predictions))
print("-- --")
#均方差(mean-squared-error)
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值