Using Python's numpy for linear fitting, polynomial fitting, logarithmic fitting

Source: Internet
Author: User

Transferred from: http://blog.itpub.net/12199764/viewspace-1743145/

There are work on the trend forecast in the project, and the 3 kinds of fitting methods are sorted out:
1. Linear fit-using 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 ("fit result: Y =%10.5f x +%10.5f, r=%10.5f"% (a,b,r))
#结果为: y = 0.97222 x + 1.59056, r= 0.98591


1. Linear fit-using 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) #一次多项式拟合, equivalent to linear fit
P1 = np.poly1d (z1)
Print Z1 #[1. 1.49333333]
Print P1 # 1 x + 1.493


2, two polynomial fitting
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, logarithmic function fitting-this is the most difficult, Baidu can not find, Google took half a day to find. Exponent, power number fitting What, use this, rewrite the 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

Use Python's numpy for linear fitting, polynomial fitting, and logarithmic fitting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.