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 预测波士顿地区房价的更多相关文章

  1. 机器学习之路: python 决策树分类DecisionTreeClassifier 预测泰坦尼克号乘客是否幸存

    使用python3 学习了决策树分类器的api 涉及到 特征的提取,数据类型保留,分类类型抽取出来新的类型 需要网上下载数据集,我把他们下载到了本地, 可以到我的git下载代码和数据集: https: ...

  2. 机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价

    python3 学习api的使用 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.datasets import ...

  3. 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价

    python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...

  4. 机器学习之路: python k近邻分类器 KNeighborsClassifier 鸢尾花分类预测

    使用python语言 学习k近邻分类器的api 欢迎来到我的git查看源代码: https://github.com/linyi0604/MachineLearning from sklearn.da ...

  5. 机器学习之路--Python

    常用数据结构 1.list 列表 有序集合 classmates = ['Michael', 'Bob', 'Tracy'] len(classmates) classmates[0] len(cla ...

  6. 机器学习之路:python 集成回归模型 随机森林回归RandomForestRegressor 极端随机森林回归ExtraTreesRegressor GradientBoostingRegressor回归 预测波士顿房价

    python3 学习机器学习api 使用了三种集成回归模型 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.dat ...

  7. 机器学习之路:python k近邻回归 预测波士顿房价

    python3 学习机器学习api 使用两种k近邻回归模型 分别是 平均k近邻回归 和 距离加权k近邻回归 进行预测 git: https://github.com/linyi0604/Machine ...

  8. 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  9. 吴裕雄 python 机器学习——支持向量机线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

随机推荐

  1. 训练赛第二场G题 ZOJ 2343

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2343 解题报告:首先我假设最后的正确的结果是a[1] , a[2 ...

  2. sql server中的日期函数

    DATEADD   在向指定日期加上一段时间的基础上,返回新的 datetime 值. 语法           DATEADD ( datepart , number, date ) 参数 (1) ...

  3. JavaScript常用数组方法

    JavaScript数组方法有以下这些: forEach() 方法对数组的每一个元素执行一次提供的函数. map() 方法创建一个新数组,其结果是该数组都执行一次函数,原函数保持不变. filter( ...

  4. 20155303 2016-2017-2 《Java程序设计》第四周学习总结

    20155303 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 继承避免多个类间重复定义共同行为,使用关键字exten ...

  5. IDL界面程序直接调用envi菜单对应功能

    参考自http://blog.sina.com.cn/s/blog_764b1e9d010115qu.html 参考文章的方法是构建一个button控件,通过单击实现,这种方法比较复杂,不是我们经常能 ...

  6. Team Foundation Server 2010服务器安装

    本安装指南使用Windows Server 2008企业版为基础,安装Windows Server 2008 SP2(必须),在此操作系统环境上进行TFS2010的安装与配置. 三.系统用户设置 1. ...

  7. angular 如何使用第三方组件ng-bootstrap

    1.在你的项目中以下指令    npm install --save @ng-bootstrap/ng-bootstrap 安装完成会显示 + @ng-bootstrap/ng-bootstrap@1 ...

  8. Windows 10安装MongoDB(安装&启动)

    Windows 10家庭中文版,MongoDB 3.6.3, 最近在学习Scrapy,可以却从未将scraped data存储到数据库中.在看过一些文档后,Scrapy会和MongoDB结合使用(还有 ...

  9. 练习题 --- 10种Xpath定位

    写出10种不同的Xpath定位语法

  10. 接口测试工具--Poster与Postman的简单实用

    HTTP/SOAP协议接口的功能测试: 1.浏览器URL(GET请求) http://127.0.0.1:8000/login/?username=zhangsan&password=1234 ...