如何从多项式拟合中提取方程式? [英] How to extract equation from a polynomial fit?

查看:228
本文介绍了如何从多项式拟合中提取方程式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将一些数据拟合到多项式函数,并获得包括拟合参数值的实际方程式。



我改编了



但是,我现在不知道在哪里提取各个拟合的实际方程式和拟合参数值。我在哪里可以访问实际的拟合方程?



编辑:



变量 model 具有以下属性:

  model.decision_function model.fit_transform model.inverse_transform model.predict模型。 Forecast_proba模型.set_params模型。转换
模型.fit模型.get_params模型.named_steps模型.predict_log_proba模型.score模型.steps

model.get_params 不存储所需的参数。

解决方案

线性模型的系数存储在 intercept _ 中, coeff _ 模型的属性。



通过降低正则化和馈入,您可以更清楚地看到这一点。一个已知的模型;例如

 从sklearn.linear_model导入numpy为np 
从sklearn.pipeline导入Ridge
导入make_pipeline $来自sklearn.preprocessing导入多项式特征的b

$ bx = 10 * np.random.random(100)
y = -4 + 2 * x-3 * x ** 2

model = make_pipeline(PolynomialFeatures(2),Ridge(alpha = 1E-8,fit_intercept = False))
model.fit(x [:, None],y)
ridge =模型.named_steps ['ridge']
print(ridge.coef_)
#array([-4。,2.,-3。])

还要注意,默认情况下 PolynomialFeatures 包含偏差项,因此将截距拟合为 Ridge 对于小的 alpha 是多余的。


My goal is to fit some data to a polynomial function and obtain the actual equation including the fitted parameter values.

I adapted this example to my data and the outcome is as expected.

Here is my code:

import numpy as np
import matplotlib.pyplot as plt

from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline


x = np.array([0., 4., 9., 12., 16., 20., 24., 27.])
y = np.array([2.9,4.3,66.7,91.4,109.2,114.8,135.5,134.2])

x_plot = np.linspace(0, max(x), 100)
# create matrix versions of these arrays
X = x[:, np.newaxis]
X_plot = x_plot[:, np.newaxis]

plt.scatter(x, y, label="training points")

for degree in np.arange(3, 6, 1):
    model = make_pipeline(PolynomialFeatures(degree), Ridge())
    model.fit(X, y)
    y_plot = model.predict(X_plot)
    plt.plot(x_plot, y_plot, label="degree %d" % degree)

plt.legend(loc='lower left')

plt.show()

However, I now don't know where to extract the actual equation and fitted parameter values for the respective fits. Where do I access the actual fitted equation?

EDIT:

The variable model has the following attributes:

model.decision_function  model.fit_transform      model.inverse_transform  model.predict            model.predict_proba      model.set_params         model.transform          
model.fit                model.get_params         model.named_steps        model.predict_log_proba  model.score              model.steps

model.get_params does not store the desired parameters.

解决方案

The coefficients of the linear model are stored in the intercept_ and coeff_ attributes of the model.

You can see this more clearly by turning-down the regularization and feeding-in a known model; e.g.

import numpy as np
from sklearn.linear_model import Ridge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures

x = 10 * np.random.random(100)
y = -4 + 2 * x - 3 * x ** 2

model = make_pipeline(PolynomialFeatures(2), Ridge(alpha=1E-8, fit_intercept=False))
model.fit(x[:, None], y)
ridge = model.named_steps['ridge']
print(ridge.coef_)
# array([-4.,  2., -3.])

Also note that the PolynomialFeatures by default includes a bias term, so fitting the intercept in Ridge will be redundant for small alpha.

这篇关于如何从多项式拟合中提取方程式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆