We will create a class for using a linear regression model to fit and predict values. This class also serves as a base class for implementing other models in this chapter. The following steps illustrates this process.
- Declare a class named LinearRegressionModel as follows:
from sklearn.linear_model import LinearRegressionclass LinearRegressionModel(object): def __init__(self): self.df_result = pd.DataFrame(columns=['Actual', 'Predicted']) def get_model(self): return LinearRegression(fit_intercept=False) def get_prices_since(self, df, date_since, lookback): index = df.index.get_loc(date_since) return df.iloc[index-lookback:index]
In the constructor of our new class, we declare a pandas DataFrame called ...