简体   繁体   English

使用python进行多项式回归

[英]polynomial regression using python

From what I understand polynomial regression is a specific type of regression analysis, which is more complicated than linear regression.据我所知,多项式回归是一种特定类型的回归分析,比线性回归更复杂。 Is there a python module which can do this?有没有可以做到这一点的python模块? I have looked in matplotlib, scikit and numpy but can only find linear regression analysis.我查看了 matplotlib、scikit 和 numpy,但只能找到线性回归分析。

And it is possible to work out the correlation coefficient of a non-linear line?并且可以计算出非线性线的相关系数吗?

Have you had a look at NumPy's polyfit ?你看过 NumPy 的polyfit吗? See reference .参考

From their examples:从他们的例子:

>>> import numpy as np
>>> x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
>>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
>>> z = np.polyfit(x, y, 3)
>>> z
[ 0.08703704 -0.81349206  1.69312169 -0.03968254]

scikit supports linear and polynomial regression. scikit 支持线性和多项式回归。

Check the Generalized Linear Models page at section Polynomial regression: extending linear models with basis functions .检查“多项式回归:使用基函数扩展线性模型”部分的“广义线性模型”页面。

Example:例子:

>>> from sklearn.preprocessing import PolynomialFeatures
>>> import numpy as np
>>> X = np.arange(6).reshape(3, 2)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> poly = PolynomialFeatures(degree=2)
>>> poly.fit_transform(X)
array([[ 1,  0,  1,  0,  0,  1],
       [ 1,  2,  3,  4,  6,  9],
       [ 1,  4,  5, 16, 20, 25]])

The features of X have been transformed from [x_1, x_2] to [1, x_1, x_2, x_1^2, x_1 x_2, x_2^2] , and can now be used within any linear model. X 的特征已从[x_1, x_2][1, x_1, x_2, x_1^2, x_1 x_2, x_2^2] ,现在可以在任何线性模型中使用。

This sort of preprocessing can be streamlined with the Pipeline tools.可以使用流水线工具简化这种预处理。 A single object representing a simple polynomial regression can be created and used as follows:可以按如下方式创建和使用表示简单多项式回归的单个对象:

>>> from sklearn.preprocessing import PolynomialFeatures
>>> from sklearn.linear_model import LinearRegression
>>> from sklearn.pipeline import Pipeline
>>> model = Pipeline([('poly', PolynomialFeatures(degree=3)),
...                   ('linear', LinearRegression(fit_intercept=False))])
>>> # fit to an order-3 polynomial data
>>> x = np.arange(5)
>>> y = 3 - 2 * x + x ** 2 - x ** 3
>>> model = model.fit(x[:, np.newaxis], y)
>>> model.named_steps['linear'].coef_
array([ 3., -2.,  1., -1.])

The linear model trained on polynomial features is able to exactly recover the input polynomial coefficients.在多项式特征上训练的线性模型能够准确地恢复输入多项式系数。

In some cases it's not necessary to include higher powers of any single feature, but only the so-called interaction features that multiply together at most d distinct features.在某些情况下,没有必要包括任何单个特征的更高幂,而只包括所谓的交互特征,这些特征最多可以乘以 d 个不同的特征。 These can be gotten from PolynomialFeatures with the setting interaction_only=True .这些可以从PolynomialFeatures获得,设置为interaction_only=True

You can first make your polynomial features using PolynomialFeatures from sklearn and then use your linear model.您可以首先使用 sklearn 的 PolynomialFeatures 制作多项式特征,然后使用您的线性模型。

Function bellow can be used for predictions of a trained model.下面的函数可用于预测训练模型。

from sklearn import linear_model
from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=2)

lm_polyfeats = linear_model.LinearRegression()
lm_polyfeats.fit(poly.fit_transform(array2D),targetArray)

def LM_polynomialFeatures_2Darray(lm_polyfeats,array2D):
    array2D=poly.fit_transform(array2D)
    return(lm.predict(array2D))

p=LM_polynomialFeatures_2Darray(lm_polyfeats,array2D)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM