Sklearn线性回归
Sklearn线性回归
原理
线性回归是最为简单而经典的回归模型,用了最小二乘法的思想,用一个n-1维的超平面拟合n维数据
数学形式
\]
其中称\(w=(w_1,w_2,w_3,...w_n)\)为系数矩阵(coef_),称\(w_0\)为截距(intercept_)
基本步骤
- 准备数据集
- 使用线性回归
- 训练模型
- 使用训练后的模型预测
- 模型评估
下面以二维数据举例
例子
#coding=utf-8
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
def main():
#数据预处理
ad = pd.read_csv('./Advertising.csv',index_col=0)
#为了方便,只取一列作为研究
X = ad[['TV']] #注意此时X的数据类型是dataFrame,如果只有一个括号,类型为Series会报错
Y = ad[['Sales']]
#这里采用交叉验证法划分数据集
X_train, X_test, Y_train, Y_test =train_test_split(X, Y)
#创建回归模型对象
lr = linear_model.LinearRegression()
lr.fit(X_train.values.reshape(-1, 1), Y_train.values.reshape(-1, 1))
#显示训练结果
print lr.intercept_,lr.coef_
print lr.score(X_test, Y_test) #用R^2评估
plt.plot(X,lr.predict(X))
plt.scatter(X,Y)
plt.show()
if __name__ == '__main__':
main()
'''
输出结果
[ 7.21071682] [[ 0.0460963]]
0.713025893451
'''

关于模型评估
这里采用的是\(R^2\)拟合优度检验,是一个属于0~1的值,\(R^2\)越大表示拟合程度越好

Sklearn线性回归的更多相关文章
- sklearn线性回归实现房价预测模型
目录 题目要求 单特征线性回归 方案一 方案二 多特征线性回归 两份数据 ex1data1.txt ex1data2.txt 题目要求 建立房价预测模型:利用ex1data1.txt(单特征)和ex1 ...
- sklearn 线性回归
# import numpy as np import pandas as pd from pandas import Series,DataFrame import matplotlib.pyplo ...
- 『科学计算』通过代码理解线性回归&Logistic回归模型
sklearn线性回归模型 import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model de ...
- Scikit-Learn 机器学习笔记 -- 线性回归、逻辑回归、softma
import numpy as np from matplotlib import pyplot as plt # 创建线性回归数据集 def create_dataset(): X = 2 * ...
- skearn自学路径
sklearn学习总结(超全面) 关于sklearn,监督学习几种模型的对比 sklearn之样本生成make_classification,make_circles和make_moons pytho ...
- Sklearn库例子2:分类——线性回归分类(Line Regression )例子
线性回归:通过拟合线性模型的回归系数W =(w_1,…,w_p)来减少数据中观察到的结果和实际结果之间的残差平方和,并通过线性逼近进行预测. 从数学上讲,它解决了下面这个形式的问题: Lin ...
- sklearn库 线性回归库 LinearRegression
import numpy as np import sklearn.datasets #加载原数据 from sklearn.model_selection import train_test_spl ...
- 使用sklearn机器学习库实现线性回归
import numpy as np # 导入科学技术框架import matplotlib.pyplot as plt # 导入画图工具from sklearn.linear_model imp ...
- sklearn学习笔记之简单线性回归
简单线性回归 线性回归是数据挖掘中的基础算法之一,从某种意义上来说,在学习函数的时候已经开始接触线性回归了,只不过那时候并没有涉及到误差项.线性回归的思想其实就是解一组方程,得到回归函数,不过在出现误 ...
随机推荐
- 一脸懵逼学习Zookeeper(动物园管理员)---》高度可靠的分布式协调服务
1:Zookeeper是一个分布式协调服务:就是为用户的分布式应用程序提供协调服务 A.zookeeper是为别的分布式程序服务的 B.Zookeeper本身就是一个分布式程序(只要有半数以上节点存活 ...
- .NET Core在安装(VS2015)与部署
.NET Core开发环境搭建 使用VS2015开发.NET Core项目,环境的搭建可以参考官网,大致安装步骤如下: 1.首先你得装个vs2015 并且保证已经升级至 update3及以上,下载链接 ...
- EntityFramework 优化建议(转)
转载地址:http://blog.jd-in.com/947.html Entity Framework目前最新版本是6.1.3,当然Entity Framework 7 目前还是预览版,并不能投入正 ...
- Hankson 的趣味题
题解: 硬是把一道傻逼题写到了200行.. 长长的模板就有70行.. 由于我没有做的时候觉得并不保证$a1|a0$ $b0|b1$ 然后就加了很多特判.. 我的做法就是暴力分解质因数 T*sqrt(n ...
- parted 分区命令
fdisk 是针对 MBR的分区 ,因为MBR分区空间最大不能超过2T 最多分4个主分区 , 所以parted可以修改磁盘为GPT 可以支持更大的分区,更多的分区 1 查看分区 : #part ...
- 2018牛客网暑假ACM多校训练赛(第十场)F Rikka with Line Graph 最短路 Floyd
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round10-F.html 题目传送门 - https://www.n ...
- Codeforces 316G3 Good Substrings 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html 题目传送门 - Codeforces 316G3 题意 给定一个母串$s$,问母串$s$有 ...
- ajax请求后台,response.sendRedirect失效,无法重定向
今天在写项目的时候,想加一个切换用户,需要清除session并且跳转到登录页面,发起一个ajax请求后,执行完发现无法跳转. 原因在于: (从网上摘录) Ajax只是利用脚本访问对应url获取数据而已 ...
- IISExpress运行网站时,Service Fabric报Could not load file or assembly 'Microsoft.ServiceFabric.Data' or one of its dependencies. An attempt was made to load a program with an incorrect format.
打开VS TOOLS > OPTIONS > Projects and Solutions > WEB PROJECTS,选中 "Use the 64 bit vers ...
- 组合数的简单求法(dfs)
组合数的具体应用可以参考这个例子:https://www.cnblogs.com/FengZeng666/p/10496223.html 下面这段代码可以作为求组合数的固定套路或者公式: #inclu ...