机器学习之路:python支持向量机回归SVR 预测波士顿地区房价
python3 学习使用api
支持向量机的两种核函数模型进行预测
git: https://github.com/linyi0604/MachineLearning
from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
import numpy as np # 1 准备数据
# 读取波士顿地区房价信息
boston = load_boston()
# 查看数据描述
# print(boston.DESCR) # 共506条波士顿地区房价信息,每条13项数值特征描述和目标房价
# 查看数据的差异情况
# print("最大房价:", np.max(boston.target)) # 50
# print("最小房价:",np.min(boston.target)) # 5
# print("平均房价:", np.mean(boston.target)) # 22.532806324110677 x = boston.data
y = boston.target # 2 分割训练数据和测试数据
# 随机采样25%作为测试 75%作为训练
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33) # 3 训练数据和测试数据进行标准化处理
ss_x = StandardScaler()
x_train = ss_x.fit_transform(x_train)
x_test = ss_x.transform(x_test) ss_y = StandardScaler()
y_train = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test = ss_y.transform(y_test.reshape(-1, 1)) # 4.1 支持向量机模型进行学习和预测
# 线性核函数配置支持向量机
linear_svr = SVR(kernel="linear")
# 训练
linear_svr.fit(x_train, y_train)
# 预测 保存预测结果
linear_svr_y_predict = linear_svr.predict(x_test) # 多项式核函数配置支持向量机
poly_svr = SVR(kernel="poly")
# 训练
poly_svr.fit(x_train, y_train)
# 预测 保存预测结果
poly_svr_y_predict = linear_svr.predict(x_test) # 5 模型评估
# 线性核函数 模型评估
print("线性核函数支持向量机的默认评估值为:", linear_svr.score(x_test, y_test))
print("线性核函数支持向量机的R_squared值为:", r2_score(y_test, linear_svr_y_predict))
print("线性核函数支持向量机的均方误差为:", mean_squared_error(ss_y.inverse_transform(y_test),
ss_y.inverse_transform(linear_svr_y_predict)))
print("线性核函数支持向量机的平均绝对误差为:", mean_absolute_error(ss_y.inverse_transform(y_test),
ss_y.inverse_transform(linear_svr_y_predict)))
# 对多项式核函数模型评估
print("对多项式核函数的默认评估值为:", poly_svr.score(x_test, y_test))
print("对多项式核函数的R_squared值为:", r2_score(y_test, poly_svr_y_predict))
print("对多项式核函数的均方误差为:", mean_squared_error(ss_y.inverse_transform(y_test),
ss_y.inverse_transform(poly_svr_y_predict)))
print("对多项式核函数的平均绝对误差为:", mean_absolute_error(ss_y.inverse_transform(y_test),
ss_y.inverse_transform(poly_svr_y_predict))) '''
线性核函数支持向量机的默认评估值为: 0.651717097429608
线性核函数支持向量机的R_squared值为: 0.651717097429608
线性核函数支持向量机的均方误差为: 27.0063071393243
线性核函数支持向量机的平均绝对误差为: 3.426672916872753
对多项式核函数的默认评估值为: 0.40445405800289286
对多项式核函数的R_squared值为: 0.651717097429608
对多项式核函数的均方误差为: 27.0063071393243
对多项式核函数的平均绝对误差为: 3.426672916872753
'''
机器学习之路:python支持向量机回归SVR 预测波士顿地区房价的更多相关文章
- 机器学习之路: python 决策树分类DecisionTreeClassifier 预测泰坦尼克号乘客是否幸存
使用python3 学习了决策树分类器的api 涉及到 特征的提取,数据类型保留,分类类型抽取出来新的类型 需要网上下载数据集,我把他们下载到了本地, 可以到我的git下载代码和数据集: https: ...
- 机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价
python3 学习api的使用 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.datasets import ...
- 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价
python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...
- 机器学习之路: python k近邻分类器 KNeighborsClassifier 鸢尾花分类预测
使用python语言 学习k近邻分类器的api 欢迎来到我的git查看源代码: https://github.com/linyi0604/MachineLearning from sklearn.da ...
- 机器学习之路--Python
常用数据结构 1.list 列表 有序集合 classmates = ['Michael', 'Bob', 'Tracy'] len(classmates) classmates[0] len(cla ...
- 机器学习之路:python 集成回归模型 随机森林回归RandomForestRegressor 极端随机森林回归ExtraTreesRegressor GradientBoostingRegressor回归 预测波士顿房价
python3 学习机器学习api 使用了三种集成回归模型 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.dat ...
- 机器学习之路:python k近邻回归 预测波士顿房价
python3 学习机器学习api 使用两种k近邻回归模型 分别是 平均k近邻回归 和 距离加权k近邻回归 进行预测 git: https://github.com/linyi0604/Machine ...
- 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
- 吴裕雄 python 机器学习——支持向量机线性回归SVR模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
随机推荐
- NYOJ 116 士兵杀敌(二) (树状数组)
题目链接 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的.小工是南将军手下的军师,南将军经常想知道第m号到第n号士兵的总杀敌数,请你帮助小工来回答南将军吧.南将军的某次询问之后 ...
- OpenCV LIBTIFF_4.0 link errors
以前用Caffe用的好好的,今天重装后居然报了很多这样的错误 /usr/lib/libopencv_highgui.so.' 1> /usr/lib/libopencv_highgui.so.' ...
- H5 键盘兼容性小结
H5 键盘兼容性小结 在 H5 项目中,我们会经常遇到页面中存在单个甚至多个 input/textarea 输入框与底部固定元素的布局情况.在 input/textarea 输入框获取焦点时,会自动触 ...
- equals方法变量和常量位置区别
对于字符串比较,我的习惯用法是 变量.equals(常量) 比如: a.equals("a") 今天看视频才知道变量在前面与后面有很大影响,正确的写法是常量放前面(可以 ...
- 解决java计算中double类型结果不一致问题,使用BigDecimal解决
一.需求:从数据表中读出一个double的数据,比如是3.5,没问题,但是如果再用3.5进行计算,比如乘以100,结果就是350了,而是35000000004 因为是浮点运算,所有语言中的浮点数都会有 ...
- 82.Linux之VMware10.0.4_x64安装
一直想写linux前期软件的一些安装配置的博客,因为中途去弄CORDIC算法了,今天上午刚弄好,除法,乘累加,三角函数等都能达到要求,所以现在来写这块的博客,CORDIC博客就不写了,因为网上很多.V ...
- accept系统调用
/* * For accept, we attempt to create a new socket, set up the link * with the client, wake up the c ...
- idea开发工具下载安装教程
我用这款工具主要用于java开发 在安装这个工具之前需要配置java的环境 java的jdk环境配置 jdk:1.8 jdk官网下载链接 --->点我 进入之后,下拉 选择 jdk1.8版本 ...
- 详述Java对象创建
Java是一门面向对象的语言,Java程序运行过程中无时无刻都有对象被创建出来.在语言层面上,创建对象(克隆.反序列化)就是一个new关键字而已,但是虚拟机层面上却不是如此.我们看一下在虚拟机层面上创 ...
- 洛谷P2149 Elaxia的路线
传送门啦 分析: 我最开始想的是跑两遍最短路,然后记录一下最短路走了哪些边(如果有两条最短路就选经过边多的),打上标记.两边之后找两次都标记的边有多少就行了. 但...我并没有实现出来. 最后让我们看 ...