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
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.