python实现K近邻回归,采用等权重和不等权重的方法
作者:UESTC_C2_403 时间:2021-12-21 19:29:26
如下所示:
from sklearn.datasets import load_boston
boston = load_boston()
from sklearn.cross_validation import train_test_split
import numpy as np;
X = boston.data
y = boston.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 33, test_size = 0.25)
print 'The max target value is: ', np.max(boston.target)
print 'The min target value is: ', np.min(boston.target)
print 'The average terget value is: ', np.mean(boston.target)
from sklearn.preprocessing import StandardScaler
ss_X = StandardScaler()
ss_y = StandardScaler()
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)
y_train = ss_y.fit_transform(y_train)
y_test = ss_y.transform(y_test)
from sklearn.neighbors import KNeighborsRegressor
uni_knr = KNeighborsRegressor(weights = 'uniform')
uni_knr.fit(X_train, y_train)
uni_knr_y_predict = uni_knr.predict(X_test)
dis_knr = KNeighborsRegressor(weights = 'distance')
dis_knr.fit(X_train, y_train)
dis_knr_y_predict = dis_knr.predict(X_test)
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
print 'R-squared value of uniform weights KNeighorRegressor is: ', uni_knr.score(X_test, y_test)
print 'The mean squared error of uniform weights KNeighorRegressor is: ', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(uni_knr_y_predict))
print 'The mean absolute error of uniform weights KNeighorRegressor is: ', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(uni_knr_y_predict))
print 'R-squared of distance weights KNeighorRegressor is: ', dis_knr.score(X_test, y_test)
print 'the value of mean squared error of distance weights KNeighorRegressor is: ', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(dis_knr_y_predict))
print 'the value of mean ssbsolute error of distance weights KNeighorRegressor is: ', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(dis_knr_y_predict))
来源:https://blog.csdn.net/UESTC_C2_403/article/details/72848636
标签:python,K近邻,权重
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中字符编码简介、方法及使用建议
2021-10-11 21:58:33
golang中的空slice案例
2023-09-02 12:26:36
![](https://img.aspxhome.com/file/2023/3/101043_0s.png)
Pyhhon之Pygame的Font文本和字体
2021-08-14 13:00:33
python中出现invalid syntax报错的几种原因分析
2023-01-26 22:01:24
php checkbox复选框值的获取与checkbox默认值输出方法
2023-11-14 13:41:12
浅谈anaconda python 版本对应关系
2023-12-06 01:02:35
快速掌握 Mysql数据库对文件操作的封装
2009-02-23 17:37:00
ASP 日期的加减运算实现代码
2011-03-08 10:47:00
利用global.asa计划执行程序
2008-03-05 12:49:00
使用SQL2000将现有代码作为Web服务提供
2009-02-19 17:20:00
基于ASP的站内多值搜索
2010-05-11 20:03:00
10款实用的jQuery图片插件
2011-01-25 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20111/25/jquery_image_2-33s.jpg)
UCDChina.com 关于用户期望的讨论
2008-07-10 11:55:00
非常全面的Python常见基础面试题及答案
2022-11-14 17:03:33
![](https://img.aspxhome.com/file/2023/2/84072_0s.gif)
ASP编程入门进阶(十四):Browser & Linkin
2008-06-12 07:08:00
Yahoo! BrowserPlus 发布
2008-11-20 13:35:00
Django中的文件的上传的几种方式
2023-08-26 22:48:51
网页效果图设计之色彩索引
2008-03-23 13:53:00
![](https://img.aspxhome.com/file/UploadPic/up/2008032314313926.gif)
Python获取航线信息并且制作成图的讲解
2023-08-28 18:18:56
![](https://img.aspxhome.com/file/2023/6/61986_0s.png)
VBScript GetObject 函数用法介绍
2008-01-30 17:00:00