Writing the linear regression model

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.

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

Get Mastering Python for Finance - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.