牛顿迭代法和最小二乘法直线拟合代码

最近感觉啥都不会了,忘光了

#coding:utf-8

def df(func,x):#求导
    eps = 1.0e-4
    return (func(x + eps) - func(x))/eps


def customFunc(x):
    return pow(x,4) + 3 * pow(x,3) + 1.5 * pow(x, 2) - 4

def newton(x, p, m):
    x0 = x
    k = 0
    while (k < m):
        k += 1
        if df(customFunc, x0) == 0.0:
            return 0
        x1 = x0 - customFunc(x0)/df(customFunc, x0)
        if abs(x1-x0) < p or abs(customFunc(x1)) < p:
            print x1
            return 1
        else:
            x0 = x1

    return 0

newton(2.0, 0.01, 100)

def linematch(xl,yl):
    sumx = sum(xl)
    sumy = sum(yl)
    sumx2 = sum([ j*j for j in xl])
    sumxy = sum([ t[0] * t[1] for t in zip(xl, yl) ])
    ncount = len(xl)
    
    b = (sumx2 * sumy - sumx * sumxy) / (ncount * sumx2 - sumx * sumx)
    k = (ncount * sumxy - sumx * sumy) / (ncount * sumx2 - sumx * sumx)
    print 'function is y = %sx + %s'%(k, b)

    k1 = (sumy * sumx - sumxy * ncount) / (sumx * sumx - sumx2 * ncount)
    b1 = (sumy - sumx * k1)/ncount
    print 'k1 = %s, b1 = %s'%(k1, b1)
    print '##################'


linematch([2,3,4,5], [1,2,3,4])
linematch([2,3,4,5, 7], [5, 7, 9, 11, 15])
linematch([2,3,4,5, 7, 9 , 11], [6, 9, 12 , 15 , 21, 27, 33])

公式推理:http://blog.csdn.net/ice_fire3/article/details/6709929

最小二乘法求多项式系数还没写出来,快哭了

2014.4.18ps:现在才知道最小二乘法的原理是统计里面的,基础不劳靠记公式总是记不住,理解之后整个就好推导出来


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值