# coding: utf-8 # # Baseline model and assessment - Uses 100 days rather than cumsum versus unadjusted popularity # In[20]: import pandas as pd import numpy as np get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt # In[13]: df = pd.read_feather('../data/basemodel.feather') df.head() # In[48]: from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import make_pipeline from sklearn.grid_search import GridSearchCV from sklearn.metrics import r2_score from sklearn.metrics import mean_squared_error model = LinearRegression(fit_intercept = True) x = df[['popularity']] y = df[['log_all_streams']] def PolynomialRegression(degree=2, **kwargs): return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs)) param_grid = {'polynomialfeatures__degree': np.arange(3), 'linearregression__normalize': [True, False]} grid = GridSearchCV(PolynomialRegression(), param_grid, cv = 5, error_score=mean_squared_error) grid.fit(x,y) # In[49]: model = grid.best_estimator_ y_pred = model.predict(x) plt.scatter(y_pred, y_pred - y) plt.hlines(y = 0, xmin = 6, xmax = 10) rmse = np.sqrt(np.sum((y_pred - y)**2)) print(f'RMSE: {rmse}') print(f'Rsq: {r2_score(y,y_pred)}') # In[50]: plt.scatter(x,y) lim = plt.axis() plt.scatter(x,y_pred, c = 'red') # ## Next iteration # * Evaluate outlier at 0 popularity. It seems to skew the results. It looks like linear would be a good fit here actually # In[52]: df.sort_values('popularity')