-
Notifications
You must be signed in to change notification settings - Fork 1
/
movie_linear_regression.py
executable file
·252 lines (216 loc) · 7.78 KB
/
movie_linear_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Cleaning All Data scraped from boxofficemojo.com
Created on Mon Oct 5 10:32:50 2020
@author: andrew
"""
import pandas as pd
import numpy as np
import pickle
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, KFold, cross_val_score, cross_validate
from sklearn.model_selection import TimeSeriesSplit
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.linear_model import LinearRegression, Lasso, LassoCV, Ridge, RidgeCV
from sklearn import metrics
from sklearn.cluster import MeanShift, estimate_bandwidth
import statsmodels.api as sm
import statsmodels.formula.api as smf
import scipy.stats as stats
import sys
movie_df = pickle.load(open('src/processed_movie_df.pickle', 'rb'))
columns_to_drop = ['link_stub', 'desc', 'distr', 'opening', 'release',
'runtime', 'genres', 'gross_dom', 'gross_inter',
'gross_world', 'Director', 'Writer', 'Producer', 'Composer',
'Cinematographer', 'Editor', 'Production Designer',
'Actors', 'Action', 'Adventure', 'Music', 'Thriller',
'mpaa_R', 'Producer_max_dom',
'distr_max_dom', 'Actors_max_dom', 'Animation', 'Crime',
'mpaa_G', 'Horror','holiday_distance',
'Editor_max_budg', 'Production Designer_mean_budg',
'Biography', 'Musical', 'average_row_budget',
'Composer_max_budg', 'mpaa_PG-13', 'budget_cat_low',
'distr_max_budg', 'runtime_cat_long', 'Fantasy',
'Cinematographer_max_budg', 'Writer_mean_budg',
'Documentary', 'Mystery', 'seasons_spring', 'Comedy',
'Drama', 'Family', 'History', 'Romance', 'Sci-Fi', 'Sport',
'War', 'Western', 'Director_mean_dom', 'Composer_max_dom',
'Cinematographer_max_dom', 'Production Designer_mean_dom',
'Producer_max_budg', 'Director_max_budg', 'Editor_max_dom',
'Writer_max_budg', 'Writer_max_dom', 'Writer_mean_dom',
'seasons_summer', 'theaters']
"""
'War',
'Crime', 'Drama', 'Sci-Fi', 'mpaa_PG-13', 'seasons_summer',
'budget_Composer', 'Animation', 'Romance', 'seasons_winter',
'runtime_cat_long', 'average_row_budget']
"""
movie_df.sort_values(by='release', inplace=True)
mask = movie_df # "movie_df.gross_dom<6e8"
X = movie_df.drop(columns=columns_to_drop)
y = movie_df.gross_dom
tss = TimeSeriesSplit(n_splits=3)
# split train test split
for train_index, test_index in tss.split(X):
X_train, X_holdout = X.iloc[train_index,:], X.iloc[test_index,:]
y_train, y_holdout = y.iloc[train_index], y.iloc[test_index]
tss2 = TimeSeriesSplit(n_splits=3)
for train_index, test_index in tss2.split(X_train):
X_train2, X_test = X_train.iloc[train_index, :], X_train.iloc[test_index, :]
y_train2, y_test = y_train.iloc[train_index], y_train.iloc[test_index]
def Kfold_LR(X, y, n, rs=None):
"""
Performs a Linear Regression with a Kfold CV
Parameters
----------
X : (DataFrame)
Features.
y : (Series)
Target Variable.
n : (int)
Number of folds for Kfold.
rs : (int)
Random State.
Returns
-------
(DataFrame)
cross_validate Dataframe.
"""
scoring_metrics = ['neg_mean_absolute_error',
'neg_mean_squared_error',
'neg_root_mean_squared_error',
'r2']
lr = LinearRegression()
# kf = KFold(n_splits=n, shuffle=True, random_state=rs)
tss2 = TimeSeriesSplit(n_splits=n)
result = cross_validate(lr, X, y, cv=tss2, scoring=scoring_metrics,
return_train_score=True, return_estimator=True)
return pd.DataFrame(result), result['estimator'][-1]
def get_scores(y_true, y_pred):
MAE = metrics.mean_absolute_error(y_true, y_pred)
MSE = metrics.mean_squared_error(y_true, y_pred)
RMSE = np.sqrt(MSE)
print('MAE: {}'.format(MAE))
print('MSE: {}'.format(MSE))
print('RMSE: {}'.format(RMSE))
def sm_LR(X, y):
'''
Linear Regression using Stats Models
Parameters
----------
X : (DataFrame)
Features.
y : (Series)
Target Variable.
Returns
-------
fit
Retrusn fit model.
'''
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.33)
model = sm.OLS(y_train, X_train)
fit = model.fit()
return fit.summary()
fit_sum = sm_LR(X_train, y_train)
fit_sum
def Kfold_Ridge(X, y, n, rs=None):
# scale data first
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.33,
random_state=42)
scaler = StandardScaler()
X_tr = scaler.fit_transform(X_train.values)
X_te = scaler.transform(X_test.values)
alphavec = 10**np.linspace(-2, 2, 200)
kf = KFold(n_splits=n, shuffle=True, random_state=rs)
ridge_model = RidgeCV(alphas = alphavec, cv=kf)
ridge_model.fit(X_tr, y_train)
return ridge_model
def Kfold_Lasso(X, y, n, rs=None):
# scale data first
scaler = StandardScaler()
X_tr = scaler.fit_transform(X)
# X_te = scaler.transform(X_test.values)
alphavec = 10**np.linspace(-2,2,200)
tss = TimeSeriesSplit(n_splits=5)
lasso_model = LassoCV(alphas = alphavec, cv=tss, tol=0.00001, max_iter=10000)
lasso_model.fit(X_tr, y)
return lasso_model
def show_normal_qq(resid):
'''
Show QQ plot based on residuals
Parameters
----------
resid : (array)
Array with residuals.
Returns
-------
None.
'''
stats.probplot(resid, dist="norm", plot=plt)
plt.title("Normal Q-Q plot")
plt.show()
_, lr = Kfold_LR(X_train, y_train, 5)
rdg = Kfold_Ridge(X_train, y_train, 5)
las = Kfold_Lasso(X_train, y_train, 5)
lr_preds = lr.predict(X_holdout)
lr_resids = lr_preds - y_holdout
scaler = StandardScaler()
X_h = scaler.fit_transform(X_holdout)
rdg_preds = rdg.predict(X_h)
rdg_resids = rdg_preds - y_holdout
lasso_preds = las.predict(X_h)
lasso_resids = lasso_preds - y_holdout
print('Linear Regression Scores')
get_scores(y_holdout, lr_preds)
print('\nRidgeCV Scores')
get_scores(y_holdout, rdg_preds)
print('\nLassoCV Scores')
get_scores(y_holdout, lasso_preds)
plt.figure()
sns.distplot(lr_resids)
plt.xlabel('Residuals')
plt.title('Linear Regression Residuals')
plt.xlim(-7e8, 2e8)
plt.ylim(0,1.8e-8)
x0, x1 = -5e8, -2.5e8
y0 = 0.2e-8
h = 0.1e-8
plt.plot([x0, x0, x1, x1],[y0-h, y0, y0, y0-h])
plt.text((x0+x1)/2, y0+0.5e-9, 'Blockbuster Movies', ha='center' )
plt.figure()
ax = sns.distplot(rdg_resids)
plt.xlabel('Residuals')
plt.title('Ridge Residuals')
plt.xlim(-7e8, 2e8)
plt.ylim(0,1.8e-8)
ax.annotate('Under Predicts',
xy=(-1.5e8, 0.1e-8),
xytext=(-5e8, 1e-8),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.figure()
ax = sns.distplot(lasso_resids)
plt.xlabel('Residuals')
plt.title('Lasso Residuals')
plt.xlim(-7e8, 2e8)
plt.ylim(0,1.8e-8)
ax.annotate('Higher Bump in Tail',
xy=(-1.5e8, 0.1e-8),
xytext=(-5e8, 1e-8),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.figure()
plt.title('Linear Regression Residuals')
plt.ylabel('Residuals')
plt.xlabel('Predictions')
sns.scatterplot(x=lr_preds, y=lr_resids)
plt.figure()
plt.xlabel('True Domestic Gross')
plt.ylabel('Predicted Domestic Gross')
plt.plot([0,8e8],[0,8e8], color='r')
plt.scatter(x=y_holdout, y=lr_preds, linewidths=(0.5), edgecolors=('black'))
plt.title('Fitted Relationship')