转自:http://blog.itpub.net/12199764/viewspace-1743145/

项目中有涉及趋势预测的工作,整理一下这3种拟合方法:
1、线性拟合-使用math
import math
def linefit(x , y):
    N = float(len(x))
    sx,sy,sxx,syy,sxy=0,0,0,0,0
    for i in range(0,int(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

if __name__ == '__main__':
    X=[ 1 ,2  ,3 ,4 ,5 ,6]
    Y=[ 2.5 ,3.51 ,4.45 ,5.52 ,6.47 ,7.51]
    a,b,r=linefit(X,Y)
    print("X=",X)
    print("Y=",Y)
    print("拟合结果: y = %10.5f x + %10.5f , r=%10.5f" % (a,b,r) )
#结果为:y =    0.97222 x +    1.59056 , r=   0.98591

1、线性拟合-使用numpy
import numpy as np
X=[ 1 ,2  ,3 ,4 ,5 ,6]
Y=[ 2.5 ,3.51 ,4.45 ,5.52 ,6.47 ,7.51]
z1 = np.polyfit(X, Y, 1)  #一次多项式拟合,相当于线性拟合
p1 = np.poly1d(z1)
print z1  #[ 1.          1.49333333]
print p1  # 1 x + 1.493

2、二次多项式拟合
import numpy

def polyfit(x, y, degree):
    results = {}
    coeffs = numpy.polyfit(x, y, degree)
    results['polynomial'] = coeffs.tolist()

# r-squared
    p = numpy.poly1d(coeffs)
    # fit values, and mean
    yhat = p(x)                         # or [p(z) for z in x]
    ybar = numpy.sum(y)/len(y)          # or sum(y)/len(y)
    ssreg = numpy.sum((yhat-ybar)**2)   # or sum([ (yihat - ybar)**2 for yihat in yhat])
    sstot = numpy.sum((y - ybar)**2)    # or sum([ (yi - ybar)**2 for yi in y])
    results['determination'] = ssreg / sstot #准确率
    return results

x=[ 1 ,2  ,3 ,4 ,5 ,6]
y=[ 2.5 ,3.51 ,4.45 ,5.52 ,6.47 ,7.2]
z1 = polyfit(x, y, 2)
print z1

3、对数函数拟合-这个是最难的,baidu上都找不到,google了半天才找到的。指数、幂数拟合啥的,都用这个,把func改写一下就行
from scipy import log as log print pcov
import numpy
from scipy import log
from scipy.optimize import curve_fit

def func(x, a, b):
    y = a * log(x) + b
    return y

def polyfit(x, y, degree):
    results = {}
    #coeffs = numpy.polyfit(x, y, degree)
    popt, pcov = curve_fit(func, x, y)
    results['polynomial'] = popt

# r-squared
    yhat = func(x ,popt[0] ,popt[1] )                         # or [p(z) for z in x]
    ybar = numpy.sum(y)/len(y)          # or sum(y)/len(y)
    ssreg = numpy.sum((yhat-ybar)**2)   # or sum([ (yihat - ybar)**2 for yihat in yhat])
    sstot = numpy.sum((y - ybar)**2)    # or sum([ (yi - ybar)**2 for yi in y])
    results['determination'] = ssreg / sstot

return results

x=[ 1 ,2  ,3 ,4 ,5 ,6]
y=[ 2.5 ,3.51 ,4.45 ,5.52 ,6.47 ,7.51]
z1 = polyfit(x, y, 2)
print z1

最新文章

  1. linux安装mvn后提示权限不够
  2. Android 防止多次点击事件
  3. 学习_单片机/嵌入式_的资源链接。——Arvin
  4. 【转】BLE_CC2540_初学者入门指导
  5. 4010: [HNOI2015]菜肴制作
  6. Atom 插件安装
  7. 怎样在win7上远程连接linux系统
  8. Spark学习笔记--Transformation 和 action
  9. C#核编之X++详解
  10. ngrx/store effects 使用总结1:计数器
  11. 1077. Kuchiguse (20)
  12. mysql常用的用户授权语句
  13. sql相同表不同查询条件合并显示
  14. 電腦清理緩存bat文件源碼
  15. sql中join与left-join图解区别
  16. Universal-ImageLoader,Picasso,Fresco,Glide对比
  17. Oracle简介及安装
  18. 【Java并发编程三】闭锁
  19. docker自动开启端口转发功能
  20. vsftp服务器同步文件

热门文章

  1. [BEC][hujiang] Lesson02 Unit1:Working life ---Reading
  2. js 全选 反选
  3. Untiy 接入 移动MM 详解
  4. hdu 4657 Find Permutation
  5. H264/AVC视频解码时AVC1和H264的区别
  6. HDU4907——Task schedule(BestCoder Round #3)
  7. 关于 hot code replace fail 问题 .
  8. Uploadify 控件上传图片 + 预览
  9. ASP.NET多次点击提交按钮以及Session超时和丢失过期问题
  10. poj 1129 Channel Allocation ( dfs )