简单线性回归

来源:互联网 发布:欢聚时代程序员的工资 编辑:程序博客网 时间:2024/05/17 02:21

在统计学中,线性回归是利用称为线性回归方程的最小二乘函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析。这种函数是一个或多个称为回归系数的模型参数的线性组合。一个带有一个自变量的线性回归方程代表一条直线。我们需要对线性回归结果进行统计分析。

通过定义和最小化一个成本函数,简单线性回归可以拟合一条直线。一般用最小二乘法(OLS)来计算系数。

线性回归的公式可以表示为

y=w0+w1x

截距

w0

斜率
w1

推导过程可参看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/31744475

http://www.zhihu.com/question/30823702

0 0