Use Scikit Learn to do linear regression on a time series pandas data frame

前端 未结 1 1892
自闭症患者
自闭症患者 2020-12-16 16:37

I\'m trying to do a simple linear regression on a pandas data frame using scikit learn linear regressor. My data is a time series, and the pandas data frame has a datetime i

相关标签:
1条回答
  • 2020-12-16 17:03

    You probably want something like the number of days since the start to be your predictor here. Assuming everything is sorted:

    In [36]: X = (df.index -  df.index[0]).days.reshape(-1, 1)
    
    In [37]: y = df['value'].values
    
    In [38]: linear_model.LinearRegression().fit(X, y)
    Out[38]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
    

    The exact units you use for the predictor don't really matter, it could be days or months. The coefficients and interpretation will change so that everything works out to the same result. Also, notice that we needed a reshape(-1, 1) so that the X is in the expected format.

    0 讨论(0)
提交回复
热议问题