0
我有生成线性和二次趋势的时间序列代码。我很困惑要为degree
参数选择什么。我看到了如下定义:如何使用PolynomialFeatures选择最合适的度数参数?
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()
将度数改为3或4会有什么影响?或者你是说它应该始终是2? – MBeale
要确定这一点,您必须尝试不同程度以获得最适合您的目的。如果您的数据完全符合二次方程,那么较高的度数将使您只有** x **的较高度数的零系数。如果你的数据符合二次*非常好,那么较高的度数会给你非常小的系数。 – Prune
您想要避免的情况(通过实验)是您*知道您的数据相对平滑的地方,但有一些异常值会严重地改变较高等级的拟合。您可以在数据的“主要频道”之外获得狂野的摇摆,试图包含那些任性的点。 – Prune