多元多项式拟合--Python

利用Python中的sklearn函数库的LinearRegression和PolynomialFeatures进行函数拟合

具体程序如下:

import matplotlib.pyplot as plt
import pandas as pd 
import numpy as np 
from sklearn.linear_model import LinearRegression #导入线性回归模块
from sklearn.preprocessing import PolynomialFeatures


x=[[-12,	2.54],[-12,	2.19],[-20,	1.75],[-20,	1.85],[-5,	3.58],[-7,	2.44],[7,		3.20],[25,	2.57],[12,	1.77],[20,	2.77],[15,	3.25],[-20,	2.34],[-30,	1.57],[-20,	1.52],[15,	2.57],[10,	3.75],[15,	1.26],[-12,	2.54],[-5.87,	2.57],[0.08,	2.88],[-11.9,	2.32],[6.1,	1.55],[-0.15,	3.16],[-5.26,	3.82],[-10.29,1.07], [-39.92,1.90],]

y = [41,50,50,50,50,48,44,60,46,60,55,35,35,45,50,60,40,41,31.02,20.98,30.97,11.05,31.16,20.92,21.18,31.09,]
print("==================================================================")
for index in range(1,100):
	data=pd.DataFrame({'IN':x, 'OUT':y})
	data_train=np.array(data['IN']).reshape(data['IN'].shape[0],1)				
	data_test=data['OUT']
	
	poly_reg =PolynomialFeatures(degree = index) 
	X_ploy =poly_reg.fit_transform(x)
	regr=LinearRegression() 													
	regr.fit(X_ploy,data_test)													
	print("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
	print("degree = ", index)
	print("coefficients = ", regr.coef_)
	print("intercept = ", regr.intercept_)
	print("R^2 = ",regr.score(X_ploy,data_test))								
	print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
	if(regr.score(X_ploy,data_test) >= 0.99):
		break
print("==================================================================")

计算结果如下:

==================================================================
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
degree =  1
coefficients =  [0.         0.23417502 2.49000913]
intercept =  35.655517534736624
R^2 =  0.13062845852051208
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
degree =  2
coefficients =  [ 0.00000000e+00 -8.67998076e-01  2.85867915e+01  5.05996188e-03
  5.41937770e-01 -5.16031113e+00]
intercept =  2.8917936495706513
R^2 =  0.36680282974834966
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
degree =  3
coefficients =  [ 0.00000000e+00  2.32337035e+00  8.62837626e+01  7.32845678e-02
 -2.76400095e+00 -3.16485345e+01  1.25383404e-03 -1.86885458e-02
  7.02626264e-01  3.89746542e+00]
intercept =  -43.565823068235204
R^2 =  0.595036736861785
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
degree =  4
coefficients =  [ 0.00000000e+00 -9.27359226e+00  7.50519878e+02  7.95413552e-01
  8.23353562e+00 -4.11409690e+02  1.71490391e-02 -6.59155370e-01
 -2.36676840e+00  9.91069353e+01  5.28441826e-05 -7.07256550e-03
  1.39851401e-01  2.41607515e-01 -8.88958626e+00]
intercept =  -469.6580685131609
R^2 =  0.6830703676187968
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
degree =  5
coefficients =  [ 0.00000000e+00  9.28501268e+01 -1.34960520e+04 -4.52985493e+00
 -9.95780624e+01  1.14314413e+04 -2.77506890e-01  8.06281464e+00
  3.22856832e+01 -4.68204475e+03 -4.20565631e-03  2.50969496e-01
 -4.00613710e+00 -2.50785085e+00  9.33885800e+02 -2.17313708e-05
  1.67482250e-03 -5.36753698e-02  6.08933509e-01 -2.07588174e-01
 -7.28846679e+01]
intercept =  6113.3609609005725
R^2 =  0.9155071848237998
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
degree =  6
coefficients =  [ 1.49368226e-01  1.32073179e+03 -1.45048362e+02 -2.26176086e+02
 -1.25241633e+03 -7.13337162e+02 -1.02801495e+01  5.40473325e+02
 -6.11134917e+02 -6.92340948e+02 -2.88372238e-01  1.72578578e+01
 -4.38748168e+02  9.77187097e+02  1.17156755e+03 -3.37666499e-03
  2.83940755e-01 -8.79360290e+00  1.47098644e+02 -3.39152229e+02
 -4.67229808e+02 -1.24293556e-05  1.53024369e-03 -6.66028716e-02
  1.40134218e+00 -1.74726208e+01  3.71705693e+01  5.87943145e+01]
intercept =  1316.821170263324
R^2 =  0.9999999999995062
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
==================================================================

在多项式最高阶数达到6阶后,其R^2超过99%。

生成多项式如下:

二元二阶:

X1:[x_{1}^{0}, x_{1}^{1}, x_{1}^{2}]

X2:[x_{2}^{0}, x_{2}^{1}, x_{2}^{2}]

 生成矩阵为:

\begin{pmatrix} x_{1}^{0}x_{2}^{0}, x_{1}^{1}x_{2}^{0}, x_{1}^{2}x_{2}^{0}, & & \\ x_{1}^{0}x_{2}^{1}, x_{1}^{1}x_{2}^{1}, x_{1}^{2}x_{2}^{1}, & & \\ x_{1}^{0}x_{2}^{2}, x_{1}^{1}x_{2}^{2}, x_{1}^{2}x_{2}^{2}, & & \end{pmatrix}

则多元多项式为:

Y = intercept + coefficient[0] * x_{1}^{0} x_{2}^{0} + coefficient[1] * x_{1}^{1} x_{2}^{0} + coefficient[2] * x_{1}^{0} x_{2}^{1} + coefficient[3] * x_{1}^{2} x_{2}^{0} + coefficient[4] * x_{1}^{1} x_{2}^{1} + coefficient[5] * x_{1}^{0} x_{2}^{1}

从多项式可以看出,整个矩阵没有全部使用,具体规则如下:

1         2         4

3         5         0

6         0         0

其中1标识的是x_{1}^{0}x_{2}^{0},其他的以此类推。0代表没有使用。

二元三阶的也按同样的方式即可,如下:

1        2        4        7

3        5        8        0

6        9        0        0

10        0        0        0

你可能感兴趣的:(Python,python,sklearn,机器学习)