用python的numpy作线性拟合、多项式拟合、对数拟合
转自: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
最新文章
- linux安装mvn后提示权限不够
- Android 防止多次点击事件
- 学习_单片机/嵌入式_的资源链接。——Arvin
- 【转】BLE_CC2540_初学者入门指导
- 4010: [HNOI2015]菜肴制作
- Atom 插件安装
- 怎样在win7上远程连接linux系统
- Spark学习笔记--Transformation 和 action
- C#核编之X++详解
- ngrx/store effects 使用总结1:计数器
- 1077. Kuchiguse (20)
- mysql常用的用户授权语句
- sql相同表不同查询条件合并显示
- 電腦清理緩存bat文件源碼
- sql中join与left-join图解区别
- Universal-ImageLoader,Picasso,Fresco,Glide对比
- Oracle简介及安装
- 【Java并发编程三】闭锁
- docker自动开启端口转发功能
- vsftp服务器同步文件
热门文章
- [BEC][hujiang] Lesson02 Unit1:Working life ---Reading
- js 全选 反选
- Untiy 接入 移动MM 详解
- hdu 4657 Find Permutation
- H264/AVC视频解码时AVC1和H264的区别
- HDU4907——Task schedule(BestCoder Round #3)
- 关于 hot code replace fail 问题 .
- Uploadify 控件上传图片 + 预览
- ASP.NET多次点击提交按钮以及Session超时和丢失过期问题
- poj 1129 Channel Allocation ( dfs )