ast*_*ris 9 python numpy scikit-learn
据我所知,多项式回归是一种特定类型的回归分析,它比线性回归更复杂.有没有python模块可以做到这一点?我看过matplotlib,scikitand numpy但只能找到线性回归分析.
并且可以计算出非线性线的相关系数?
ffe*_*rri 12
scikit支持线性和多项式回归.
在" 多项式回归"一节中检查" 广义线性模型"页面:使用基函数扩展线性模型.
例:
>>> 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]])
Run Code Online (Sandbox Code Playgroud)
X的特征已经转换[x_1, x_2]
为[1, x_1, x_2, x_1^2, x_1 x_2, x_2^2]
,现在可以在任何线性模型中使用.
使用Pipeline工具可以简化这种预处理.可以创建表示简单多项式回归的单个对象,如下所示:
>>> 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.])
Run Code Online (Sandbox Code Playgroud)
在多项式特征上训练的线性模型能够精确地恢复输入多项式系数.
在某些情况下,没有必要包括任何单个特征的更高权力,而只需要包含最多d个不同特征的所谓交互特征.这些可以从PolynomialFeatures
设置中获得interaction_only=True
.
adr*_*nus 11
你看过NumPy了polyfit
吗?见参考文献.
从他们的例子:
>>> 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]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27297 次 |
最近记录: |