机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价
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.linear_model import LinearRegression, SGDRegressor
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 使用两种线性回归模型进行训练和预测
# 初始化LinearRegression线性回归模型
lr = LinearRegression()
# 训练
lr.fit(x_train, y_train)
# 预测 保存预测结果
lr_y_predict = lr.predict(x_test) # 初始化SGDRRegressor随机梯度回归模型
sgdr = SGDRegressor()
# 训练
sgdr.fit(x_train, y_train)
# 预测 保存预测结果
sgdr_y_predict = sgdr.predict(x_test) # 5 模型评估
# 对Linear模型评估
lr_score = lr.score(x_test, y_test)
print("Linear的默认评估值为:", lr_score)
lr_R_squared = r2_score(y_test, lr_y_predict)
print("Linear的R_squared值为:", lr_R_squared)
lr_mse = mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict))
print("Linear的均方误差为:", lr_mse)
lr_mae = mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict))
print("Linear的平均绝对误差为:", lr_mae) # 对SGD模型评估
sgdr_score = sgdr.score(x_test, y_test)
print("SGD的默认评估值为:", sgdr_score)
sgdr_R_squared = r2_score(y_test, sgdr_y_predict)
print("SGD的R_squared值为:", sgdr_R_squared)
sgdr_mse = mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict))
print("SGD的均方误差为:", sgdr_mse)
sgdr_mae = mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict))
print("SGD的平均绝对误差为:", sgdr_mae) '''
Linear的默认评估值为: 0.6763403830998702
Linear的R_squared值为: 0.6763403830998701
Linear的均方误差为: 25.09698569206773
Linear的平均绝对误差为: 3.5261239963985433 SGD的默认评估值为: 0.659795654161198
SGD的R_squared值为: 0.659795654161198
SGD的均方误差为: 26.379885392159494
SGD的平均绝对误差为: 3.5094445431026413
'''
机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价的更多相关文章
- 机器学习之路: python k近邻分类器 KNeighborsClassifier 鸢尾花分类预测
使用python语言 学习k近邻分类器的api 欢迎来到我的git查看源代码: https://github.com/linyi0604/MachineLearning from sklearn.da ...
- 机器学习之路: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 回归树 DecisionTreeRegressor 预测波士顿房价
python3 学习api的使用 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.datasets import ...
- 机器学习之路:python支持向量机回归SVR 预测波士顿地区房价
python3 学习使用api 支持向量机的两种核函数模型进行预测 git: https://github.com/linyi0604/MachineLearning from sklearn.dat ...
- 机器学习之路--Python
常用数据结构 1.list 列表 有序集合 classmates = ['Michael', 'Bob', 'Tracy'] len(classmates) classmates[0] len(cla ...
- 机器学习之路: python 决策树分类DecisionTreeClassifier 预测泰坦尼克号乘客是否幸存
使用python3 学习了决策树分类器的api 涉及到 特征的提取,数据类型保留,分类类型抽取出来新的类型 需要网上下载数据集,我把他们下载到了本地, 可以到我的git下载代码和数据集: https: ...
- 机器学习算法的Python实现 (1):logistics回归 与 线性判别分析(LDA)
先收藏............ 本文为笔者在学习周志华老师的机器学习教材后,写的课后习题的的编程题.之前放在答案的博文中,现在重新进行整理,将需要实现代码的部分单独拿出来,慢慢积累.希望能写一个机器学 ...
- 机器学习之路:python线性回归分类器 LogisticRegression SGDClassifier 进行良恶性肿瘤分类预测
使用python3 学习了线性回归的api 分别使用逻辑斯蒂回归 和 随机参数估计回归 对良恶性肿瘤进行预测 我把数据集下载到了本地,可以来我的git下载源代码和数据集:https://gith ...
随机推荐
- 针对用户在个人中心绑定手机认证的一些js代码。
需求: 1:手机号码校验(格式的校验,手机号码是否已经绑定过)---未实现 2:填完手机号码,点击发送验证码,手机会收到一条信息 3:发送验证码按钮不可用,变成重新发送的倒计时 1):60秒以后又可以 ...
- 利用Django REST framework快速实现文件上传下载功能
安装包 pip install Pillow 设置 首先在settings.py中定义MEDIA_ROOT与MEDIA_URL.例如: MEDIA_ROOT = os.path.join(BASE_D ...
- 2015.07.15——prime素数
prime素数 1.素数也叫质数,定义是一个数只能被1和它自身整除. 素数从2开始,0,1都不是素数. 2.素数的判断(C++) 3.给定某个数,求小于这个数的所有素数 2.素数的判断(C++) bo ...
- JDK1.8源码TreeMap
基于红黑树(Red-Black tree)的 NavigableMap 实现:键的排序由构造方法决定:自然排序,Comparator排序:非线程安全(仅改变与现有键关联的值不是结构上的修改):线程安全 ...
- nginx配置location总结及rewrite规则写法【转】
转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...
- C#之WinForm基础 新建一个不可编辑的comboBox
慈心积善融学习,技术愿为有情学.善心速造多好事,前人栽树后乘凉.我今于此写经验,愿见文者得启发. 1.拉控件 2.添加可选数据 3.改变基本样式 4.效果图 C#优秀,值得学习.Winform,WPF ...
- 25 The Go image/draw package go图片/描绘包:图片/描绘包的基本原理
The Go image/draw package go图片/描绘包:图片/描绘包的基本原理 29 September 2011 Introduction Package image/draw de ...
- Scala中“=>”用法及含义
=> has several meanings in Scala, all related to its mathematical meaning as implication. 1. In a ...
- 如何查看页面是否开启了gzip压缩
1.谷歌浏览器 F12 2.在表头单击鼠标右键 3.如果开启了gzip则显示gzip,没有则是空
- group by 和 distinct 去重比较
distinct方式就是两两对比,需要遍历整个表.group by分组类似先建立索引再查索引,所以两者对比,小表destinct快,不用建索引.大表group by快.一般来说小表就算建索引,也不会慢 ...