繁体   English   中英

如何使用PolynomialFeatures选择最合适的度数参数?

[英]How do I select the most appropriate degree param with PolynomialFeatures?

我有产生线性和二次趋势的时间序列代码。 我对为degree参数选择什么感到困惑。 我看到以下定义:

Within scikit-learn's PolynomialFeatures, when the argument degree is passed, all terms up to that degree are created.

我只是不理解那个定义。 有没有使用简单数学的解释? 如何确保我使用的学位最好?

这是我的代码,如果您想要一个示例。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm                                                                                                                          

import statsmodels.formula.api as smf                                                                                                                 

import statsmodels.tsa.api as smt
import random
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline


y = [5*np.random.normal() for j in range(50)] + [30 + 5 * np.random.normal() for j in range(50)] +  [50 + 5 * np.random.normal() for j in range(50)] +  [20 + 5 * np.random.normal() for j in range(50)]
X = [x for x in range(len(y))]
X = np.reshape(X, (len(X), 1))

model = LinearRegression()
model.fit(X, y)
trend = model.predict(X)

model = make_pipeline(PolynomialFeatures(2), Ridge())
model.fit(X, y)
quadratic = model.predict(X)

fig = plt.figure(1, figsize=(15, 9))
ax = fig.add_subplot(111)
ax.plot(trend, label="Linear Trend")
ax.plot(quadratic, label="Quadratic Trend")
ax.plot(X, y, label='Time Series')
ax.legend()
plt.show()

您使用2作为学位; 线性分量将包含在平方中。 例如,如果您计算出的线性分量是2x - 5 ,而二次数是3x^2 + x + 1 ,那么从函数中得到的就是总和3x^2 + 3x + 4

暂无
暂无

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

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