使用python进行多项式回归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31406975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:57:08  来源:igfitidea点击:

polynomial regression using python

pythonnumpyscikit-learn

提问by astrochris

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? I have looked in matplotlib ,scikitand numpy but can only find linear regression analysis.

据我所知,多项式回归是一种特定类型的回归分析,它比线性回归更复杂。有没有可以做到这一点的python模块?我查看了 matplotlib 、scikit 和 numpy,但只能找到线性回归分析。

And it is possible to work out the correlation coefficient of a none linear line?

并且可以计算出非线性线的相关系数吗?

回答by fferri

scikit supports linear and polynomial regression.

scikit 支持线性和多项式回归。

Check the Generalized Linear Modelspage 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. These can be gotten from PolynomialFeatureswith the setting interaction_only=True.

在某些情况下,没有必要包含任何单个特征的更高幂,而只包含至多 d 个不同特征相乘的所谓交互特征。这些可以通过PolynomialFeatures设置获得interaction_only=True

回答by adrianus

Have you had a look at NumPy's polyfit? See reference.

你看过 NumPy 的polyfit吗?见参考

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]

回答by Ioannis Nasios

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)