简单线性回归
来源:互联网 发布:欢聚时代程序员的工资 编辑:程序博客网 时间:2024/05/17 02:21
在统计学中,线性回归是利用称为线性回归方程的最小二乘函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析。这种函数是一个或多个称为回归系数的模型参数的线性组合。一个带有一个自变量的线性回归方程代表一条直线。我们需要对线性回归结果进行统计分析。
通过定义和最小化一个成本函数,简单线性回归可以拟合一条直线。一般用最小二乘法(OLS)来计算系数。
线性回归的公式可以表示为
截距
斜率
推导过程可参看wikipedia
Python语言求截距和斜率的函数
def simple_linear_regression(X, y): ''' X - numpy随机产生的数组,大写X,小写x会转换成int y - numpy随机产生的数组 outputs - floats ''' # initial sums n = float(len(X)) sum_x = X.sum() sum_y = y.sum() sum_xy = (X*y).sum() sum_xx = (X**2).sum() # formula for w0 slope = (sum_xy - (sum_x*sum_y)/n)/(sum_xx - (sum_x*sum_x)/n) # formula for w1 intercept = sum_y/n - slope*(sum_x/n) return (intercept, slope)
测试
import numpy as npimport randomimport matplotlib.pyplot as plt%matplotlib inlinerandom.seed(199)x = np.array(range(10))y = np.array([random.randint(1, 10) for x in range(10)])intercept, slope = simple_linear_regression(x, y)print 'Intercept: %.2f, Slope: %.2f' % (intercept, slope)
画图
def reg_predictions(X, intercept, slope): return ((slope*X) + intercept)line_x = np.array([x/10. for x in range(100)])line_y = reg_predictions(line_x, intercept, slope)plt.plot(X, y, '*', line_x, line_y, '-')
In [51]: random.seed(0)In [52]: random.randint(1,4)Out[52]: 4In [53]: random.seed(0)In [54]: random.randint(1,4)In [47]: numpy.random.seed(0)In [48]: numpy.random.rand(4)Out[48]: array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318])In [49]: numpy.random.seed(0)In [50]: numpy.random.rand(4)Out[50]: array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318])
scala的实现
def simple_linear_regression(x, y) :: : //initial sums n = x.length.toFloat sum_x = x.sum sum_y = y.sum sum_xy = (x,y).zipped.map(_ * _).sum sum_xx = x.map(n=>n*n).sum //formula for w0 slope = (sum_xy - (sum_x*sum_y)/n)/(sum_xx - (sum_x*sum_x)/n) //formula for w1 intercept = sum_y/n - slope*(sum_x/n) return (intercept, slope)
scala.util.Random.setSeed(0)List.fill(10)(scala.util.Random.nextInt(101)
http://charlesfranzen.com/posts/simple-linear-regression-in-python/
http://squall0032.tumblr.com/post/79344039955/machine-learning-linear-regression-with-python
多元函数的图像表示
https://www.zhihu.com/question/31744475http://www.zhihu.com/question/30823702
0 0
- 回归:简单一元线性回归
- 简单线性回归
- 简单的线性回归
- 简单线性回归
- 简单的线性回归
- 简单线性回归
- R---简单线性回归
- 简单一元线性回归
- 简单线性回归
- 简单线性回归
- 机器学习回归篇-简单线性回归
- (十一)简单线性回归
- 简单线性回归分析【笔记】
- 7.1 简单线性回归算法
- 7.2 简单线性回归应用
- 简单线性回归及实现
- python实现简单线性回归
- 简单线性回归(上)
- ios开发根据Label的字体和宽度算出它的高度
- 准备Scala开发环境
- 禅道学习笔记
- sharesdk 各主流平台的步骤和分享注意事项
- Android 属性动画(Property Animation)介绍
- 简单线性回归
- 九度-题目1185--特殊排序
- BZOJ 4276: [ONTAK2015]Bajtman i Okrągły Robin|贪心|匈牙利
- kali linux安装之后要做的事
- 静态分析安全测试(SAST)优缺点探析
- HBase shell使用教程
- Apple Pay接入详细教程
- 【机器学习算法】之KNN算法的实现
- 【Some】【搞逗】如何向外行解释产品经理频繁更改需求为什么会令程序员烦恼?