python进行多元多项式拟合

网址:python实现多元线性拟合、一元多项式拟合、多元多项式拟合 - 百度文库https://wenku.baidu.com/view/bced623dfc00bed5b9f3f90f76c66137ee064f0c.html

import pandas as pd
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model
filename = "E:/data.csv"
df= pd.read_csv(filename)
x = np.array(df.iloc[:,0:4].values)
y = np.array(df.iloc[:,5].values)
poly_reg =PolynomialFeatures(degree=2) #三次多项式
X_ploy =poly_reg.fit_transform(x)
lin_reg_2=linear_model.LinearRegression()
lin_reg_2.fit(X_ploy,y)
predict_y =  lin_reg_2.predict(X_ploy)
strError = stdError_func(predict_y, y)
R2_1 = R2_1_func(predict_y, y)
R2_2 = R2_2_func(predict_y, y)
score = lin_reg_2.score(X_ploy, y) ##sklearn中⾃带的模型评估,与R2_1逻辑相同
print("coefficients", lin_reg_2.coef_)
print("intercept", lin_reg_2.intercept_)
print('degree={}: strError={:.2f}, R2_1={:.2f},  R2_2={:.2f}, clf.score={:.2f}'.format(
    3, strError,R2_1,R2_2,score))

函数输出结果为:
coefficients [ 0. 332.28129937 -19.9240981 -9.10607925
-191.05593023 -287.93919929 -912.11402936 -1230.21922184
-207.90033986 99.03441748 190.26204994 433.25169929
273.13674555 257.66550523 344.92652936]
intercept 4.35175537840722
degree=3: strError=0.23, R2_1=0.97, R2_2=0.82, clf.score=0.97
代码中输⼊的⾃变量是⼀个包含四个变量的输⼊, 对应coefficients输出的是长度为15的向量, 其中对应到的变量分别为 variable_X = [1, x1,
x2, x3, x4, x1∗x1, x1∗x2, x1∗x3, x1∗x4, x2∗x2, x2∗x3, x2∗x4, x3∗x3, x3∗x4, x4∗x4]
对应的⽅程式为: intercept+coefficients∗variableX.T
intercept+coefficients∗variableX.T
代码中涉及到的数据集如下:

a,b,c,d,e
0.06,0.2,0.02,0.1,0.340 
0.1,0.28,0.02,0.14,0.370 
0.12,0.32,0.02,0.16,0.377 
0.08,0.24,0.02,0.12,0.383 
0.08,0.32,0.04,0.1,0.383 
0.12,0.28,0.03,0.1,0.393 
0.1,0.24,0.05,0.1,0.385 
0.06,0.32,0.05,0.14,0.362 
0.12,0.2,0.05,0.12,0.320 
0.06,0.28,0.04,0.12,0.393 
0.08,0.28,0.05,0.16,0.402 
0.08,0.2,0.03,0.14,0.349 
0.1,0.2,0.04,0.16,0.335 
0.1,0.32,0.03,0.12,0.387 
0.12,0.24,0.04,0.14,0.390 
0.06,0.24,0.03,0.16,0.315
--------------------------------------------------------
作者:暴风小静13
链接:https://wenku.baidu.com/view/bced623dfc00bed5b9f3f90f76c66137ee064f0c.html
来源:百度文库
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值