import pandas as pd
import numpy as np
%matplotlib inline
%pylab inline
pylab.rcParams['figure.figsize'] = (12, 7)
import matplotlib.pyplot as plt
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
df = pd.read_feather('../data/basemodel_cumsum.feather')
model = LinearRegression(fit_intercept = True)
x = df[['popularity']]
y = df[['log_all_streams_cumsum']]
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)
def fitAndEvaluate(x,y,grid):
grid.fit(x,y)
model = grid.best_estimator_
print(f'Best model: {model}')
y_pred = model.predict(x)
plt.subplot(1,2,1)
plt.scatter(y_pred, y_pred - y)
plt.hlines(y = 0, xmin = min(y_pred), xmax = max(y_pred))
plt.title('Residual Errors')
plt.subplot(1,2,2)
plt.scatter(x,y)
lim = plt.axis()
plt.scatter(x,y_pred, c = 'red')
plt.title('Prediction versus actual')
rmse = np.sqrt(np.sum((y_pred - y)**2))[0]
print(f'RMSE: {rmse}')
print(f'Rsq: {r2_score(y,y_pred)}')
fitAndEvaluate(x,y,grid)
df.sort_values('popularity').head()
There is an outlier that has massive impact on the curve with 0 popularity, ,this could be some corner case but also some data error. Look into this explicitly.
UPDATE this was caused by an erratic jump to 0 and back
df = pd.read_feather('../data/basemodel_cumsum_outlier_removed.feather')
x = df[['popularity']]
y = df[['log_all_streams_cumsum']]
fitAndEvaluate(x,y,grid)
df = pd.read_feather('../data/basemodel_cumsum_outlier_removed_days_after_release.feather')
x = df[['popularity']]
y = df[['log_all_streams_cumsum']]
fitAndEvaluate(x,y,grid)