首页 > 解决方案 > Theil-Sen 回归:平移 x 轴时的不同结果

问题描述

我想在时间序列上拟合 Theil-Sen 回归(使用 scikit-learn)。我尝试了两件事:

在此处输入图像描述

from sklearn.linear_model import TheilSenRegressor, LinearRegression

y = np.array(
    [688., 895., 1673., 1077., 855., 1064., 1226., 3900., 699., 699., 2726., 1383., 1542., 2132., 1275., 969., 2789.,
     2576.])

X = np.arange(len(y)).reshape(-1, 1)

X2 = X + 2002

y_pred2 = TheilSenRegressor(random_state=0).fit(X2, y).predict(X2)
print(y_pred2)

y_pred = TheilSenRegressor(random_state=0).fit(X, y).predict(X)


import matplotlib.pyplot as plt

fig, axarr = plt.subplots(2, 2)


axarr[0, 0].scatter(X, y)
axarr[0, 0].plot(X, y_pred, color='orange')
axarr[0, 0].title.set_text('Theil-Sen: X')

axarr[0, 1].scatter(X2, y)
axarr[0, 1].plot(X2, y_pred2, color='orange')
axarr[0, 1].title.set_text('Theil-Sen: Shifted X')

axarr[1, 0].scatter(X, y)
axarr[1, 0].plot(X, LinearRegression().fit(X, y).predict(X), color='orange')
axarr[1, 0].title.set_text('OLS: X')

axarr[1, 1].scatter(X2, y)
axarr[1, 1].plot(X2, LinearRegression().fit(X2, y).predict(X2), color='orange')
axarr[1, 1].title.set_text('OLS: Shifted X')

标签: pythonscikit-learnregressionlinear-regression

解决方案


推荐阅读