python - Theil-Sen 回归:平移 x 轴时的不同结果
问题描述
我想在时间序列上拟合 Theil-Sen 回归(使用 scikit-learn)。我尝试了两件事:
- 直接在年份上拟合回归量(X = {2002:2019}
- 直接在年份上拟合回归器 - 最小年份(X = {0:18} 我希望结果相同,但它们不同。如果我使用 OLS 回归,它们确实相似。我错过了什么?
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')
解决方案
推荐阅读
- blockchain - 如何从交易 ID 中获取发件人比特币地址?
- c++ - 在 C++ 中初始化构造函数的顺序
- python - min(must_visit, key=lambda x:...) 后面的语法
- visual-studio-code - 如何在 VScode 中设置“Ctrl + PlusSign”快捷键
- c++ - 什么是用于获取组合 NIC 的 Windows C++ API 函数?
- android - 如何将值从 onItemClick RecycleView 发送到其他 java 类
- javascript - 如何不将 jQuery 与 Google Tag Manager 一起使用?
- ios - iOS13:当我尝试以横向模式打开 UIViewController 时,应用程序冻结
- vba - 如何填写 SharePoint 网页上的文本字段?
- r - if_else 在 dplyr::mutate 中没有按预期工作