【问题标题】:Theil-Sen Regression: different results when translating x-axisTheil-Sen 回归:平移 x 轴时的不同结果
【发布时间】:2022-03-08 07:15:17
【问题描述】:

我想在时间序列上拟合 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')

【问题讨论】:

    标签: python scikit-learn regression linear-regression


    【解决方案1】:

    我不确定它为什么会产生这个结果。如果您乐于使用 scipy.stats.mstats.theilslopes 代替,它将产生预期的结果:

    import numpy as np
    from scipy.stats.mstats import theilslopes, linregress
    import matplotlib.pyplot as plt
    
    Y = np.array(
        [688., 895., 1673., 1077., 855., 1064., 1226., 3900., 699., 699., 2726., 1383., 1542., 2132., 1275., 969., 2789.,
         2576.])
    
    X1 = np.arange(len(y)).reshape(-1, 1)
    
    X2 = X + 2002
    
    model1 = theilslopes(Y, X1)
    model2 = theilslopes(Y, X2)
    
    Y1_pred = model1[1] + model1[0] * X1
    Y2_pred = model2[1] + model2[0] * X2
    
    model1lr = linregress(X1, Y)
    model2lr = linregress(X2, Y)
    
    Y1lr_pred = model1lr[1] + model1lr[0] * X1
    Y2lr_pred = model2lr[1] + model2lr[0] * X2
    
    fig, axarr = plt.subplots(2, 2)
    
    axarr[0, 0].scatter(X1, Y)
    axarr[0, 0].plot(X1, Y1_pred, color='orange')
    axarr[0, 0].title.set_text('Theil-Sen: X')
    
    axarr[0, 1].scatter(X2, Y)
    axarr[0, 1].plot(X2, Y2_pred, color='orange')
    axarr[0, 1].title.set_text('Theil-Sen: Shifted X')
    
    axarr[1, 0].scatter(X, Y)
    axarr[1, 0].plot(X, Y1lr_pred, color='orange')
    axarr[1, 0].title.set_text('OLS: X')
    
    axarr[1, 1].scatter(X2, Y)
    axarr[1, 1].plot(X2, Y2lr_pred, color='orange')
    axarr[1, 1].title.set_text('OLS: Shifted X')
    
    plt.pause(1)
    plt.show(block=True)
    

    我希望这会有所帮助,但我们仍然需要弄清楚 scikit-learn 中发生了什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2018-03-04
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多