python最小二乘法线性回归_python 分别用梯度下降法和最小二乘法求线性回归 | 学步园...

#coding=utf-8

import math

import matplotlib.pyplot as plt

import numpy as np

'''

author

date          2013-12-20

description   分别用最小二乘法和梯度下降法求线性回归

'''

'''

梯度下降法

'''

def grediant(xset,yset,alpha,dgree):

theta_0 = 0;

theta_1 = 1;

length = len(xset)

jtheta = 0

total = 0

sum_total = 0

for index in range(0,length):

print index

total = total + theta_0 + theta_1 * xset[index] - yset[index]

tmp0 = theta_0  - alpha/length*(total)

tmp1 = theta_1  -  alpha/length*(total)*xset[index]

theta_0 = tmp0

theta_1 = tmp1

sum_total = sum_total + theta_0 + theta_1 * xset[index] - yset[index]

jtheta_1 = 0.5 * length * math.pow(sum_total,2)

comp = math.fabs(jtheta_1 - jtheta)

print "%10.5f   %10.5f  %10.5f  %10.5f" %(comp,jtheta * dgree,theta_0,theta_1)

if comp < jtheta * dgree:break

jtheta = jtheta_1

return theta_0,theta_1

'''

最小二乘法

'''

def linefit( x,y):

N = len(x)

sx,sy,sxx,syy,sxy=0,0,0,0,0

for i in range(0,N):

sx  += x[i]

sy  += y[i]

sxx += x[i]*x[i]

syy += y[i]*y[i]

sxy += x[i]*y[i]

a = (sy*sx/N -sxy)/( sx*sx/N -sxx)

b = (sy - a*sx)/N

r = abs(sy*sx/N-sxy)/math.sqrt( (sxx-sx*sx/N)*(syy-sy*sy/N))

return a,b,r

X=[1.5,2,1.5,2,3,3,3.5,3.5,4,4,5,5]

Y=[3,3.2,4,4.5,4,5,4.2,4.5,5,5.5,4.8,6.5]

#a,b = grediant(X,Y,0.0005,0.3)

a,b,r = linefit(X,Y)

print "y = %10.5fx + %10.5f" %(b,a)

x = np.linspace(0, 10, 10000)

#y = b * x + a

y = a * x + b

plt.plot(x,y)

plt.scatter(X, Y)

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值