zoukankan      html  css  js  c++  java
  • 用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

  • 相关阅读:
    MySql事务试什么?具有哪些特性
    MySQL的前缀索引你是如何使用的
    你知道联合索引吗?它的最左原则是什么?
    你来说一下 Mysql 索引有几种类型呢?分别是什么?
    什么是Mysql的回表查询?聊一聊
    JavaScript中使用typeof运算符需要注意的几个坑
    prototype和__proto__
    JavaScript中的普通函数与构造函数比较
    原型和原型链
    document.querySelector和querySelectorAll方法
  • 原文地址:https://www.cnblogs.com/gslyyq/p/5043847.html
Copyright © 2011-2022 走看看