-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiple Linear Regression NFL.py
220 lines (134 loc) · 5.19 KB
/
Multiple Linear Regression NFL.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
# coding: utf-8
# In[ ]:
#***Single LINEAR REGRESSION***#
# In[157]:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.formula.api as sm
import statsmodels.api as stats
from sklearn.linear_model import LinearRegression
from mpl_toolkits.mplot3d import Axes3D
get_ipython().magic('matplotlib inline')
# In[158]:
df1 = pd.read_excel("Master NFL File.xlsx")
# In[159]:
Temp_Season = df1[df1.Season < 2014]
df = Temp_Season[['Win_Percentage', 'Offensive_Yards_Gained']]
# In[141]:
# Normaliztion
# df = df.apply(lambda x: (x - np.mean(x)) / (np.std(x)))
# In[142]:
df.head()
# In[160]:
df.corr()
# In[144]:
def computewinpercentage(X, y, theta):
inner = np.power(((X * theta.T) - y), 2)
return np.sum(inner) / (2 * len(X))
# In[145]:
df.insert(0, 'Ones', 1)
# In[146]:
cols = df.shape[1]
X = df.iloc[:,[0,2]]
y = df.iloc[:,[1]]
# In[147]:
X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))
# In[148]:
X.shape, theta.shape, y.shape
# In[149]:
computewinpercentage(X, y, theta)
# In[150]:
def gradientDescent(X, y, theta, alpha, iterations):
temp = np.matrix(np.zeros(theta.shape))
parameters = int(theta.ravel().shape[1])
win = np.zeros(iterations)
for i in range(iterations):
error = (X * theta.T) - y
for j in range(parameters):
term = np.multiply(error, X[:,j])
temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))
theta = temp
win[i] = computewinpercentage(X, y, theta)
return theta, win
# In[151]:
alpha = 0.01
iterations = 1000
# In[152]:
g, win = gradientDescent(X, y, theta, alpha, iterations)
g
# In[153]:
computewinpercentage(X, y, g)
# In[161]:
result = sm.ols(formula="Win_Percentage ~ Offensive_Yards_Gained", data=df).fit()
print result.summary()
# In[155]:
x = np.linspace(df['Win_Percentage'].min(), df['Win_Percentage'].max(), 100)
f = g[0, 0] + (g[0, 1] * x)
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(x, f, 'r', label='Prediction')
ax.scatter(df['Win_Percentage'], df.Offensive_Yards_Gained, label='Traning Data')
ax.legend(loc=2)
ax.set_xlabel('Win Percentage')
ax.set_ylabel('Offensive Yards')
ax.set_title('Win Percentage vs. Offensive Yards')
# In[156]:
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(np.arange(iterations), win, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Wins')
ax.set_title('Error vs. Training Epoch')
# In[ ]:
# Multiple Linear Regression Stats Package
# In[4]:
data2 = Temp_Season.drop(['Teams', 'Season','Away_Ties', 'Total_Ties', 'Home_Ties'], 1)
# In[12]:
df1 = pd.read_excel("Master NFL File (1).xlsx")
Temp_Season = df1[df1.Season < 2014]
result_test = sm.ols(formula="Win_Percentage ~ Sacks_Allowed + Offensive_Yards_Gained + Defensive_Yards_Allowed + Points_For + Points_Against + Avg_Margin_of_Victory + Turnover_Differential + Opponent_Win_Percentage + Time_Of_Possession", data=Temp_Season).fit()
print result_test.summary()
# In[ ]:
# Multiple Linear Regression Stats Package Gradient Descent
data3 = data2[['Offensive_Yards_Gained', 'Points_Against', 'Win_Percentage']]
# Normalize
data3 = data3.apply(lambda x: (x - np.mean(x)) / (np.std(x)))
# Insert Ones
data3.insert(0, 'Ones', 1)
data3.head()
# Stats Regression Analysis
result = sm.ols(formula="Win_Percentage ~ Offensive_Yards_Gained + Points_Against", data=data3).fit()
print result.summary()
# set X (training data) and y (target variable)
cols = data3.shape[1]
X2 = data3.iloc[:,0:cols-1]
y2 = data3.iloc[:,cols-1:cols]
# convert to matrices and initialize theta
X2 = np.matrix(X2.values)
y2 = np.matrix(y2.values)
theta2 = np.matrix(np.array([0,0,0]))
# Type 1 error and Iterations
alpha = 0.01
iterations = 1000
# perform linear regression on the data set
g2, win2 = gradientDescent(X2, y2, theta2, alpha, iterations)
# get the cost (error) of the model
computewinpercentage(X2, y2, g2)
# Error
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(np.arange(iterations), win2, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Error')
ax.set_title('Error vs. Training Epoch')
Prediction_Data = pd.read_excel('Test_2016.xlsx')
def Super_Bowl_Odds(Current_Data):
pass
win_percentage=1.2395+(-0.0014*Current_Data.loc[i,'Sacks_Allowed'])+(-.00001363*Current_Data.loc[i,'Offensive_Yards_Gained'])+(.000005341*Current_Data.loc[i,'Defensive_Yards_Allowed'])+(0.0019*Current_Data.loc[i,'Points_For'])+(-0.0020*Current_Data.loc[i,'Points_Against'])+(-0.0117*Current_Data.loc[i,'Avg_Margin_of_Victory']) + (.0002*Current_Data.loc[i,'Turnover_Differential']) + (-.6961*Current_Data.loc[i,'Opponent_Win_Percentage'])+ (-0.0043*Current_Data.loc[i,'Time_Of_Possession'])
return win_percentage
Results = pd.DataFrame(index=['ARI', 'ATL','BAL','BUF','CAR','CHI','CIN','CLE','DAL','DEN','DET','GB','HOU','IND','JAX','KC','LAC','LAR','MIA','MIN','NE','NO','NYG','NYJ','OAK','PHI','PIT','SEA','SF','TB','TEN','WAS'], columns=['Odds_of_Winning_the_Super_Bowl'])
for i in Prediction_Data.index:
Results.loc[Current_Data.loc[i,'Teams'],'Odds_of_Winning_the_Super_Bowl'] = Super_Bowl_Odds(Prediction_Data)
Results.to_excel('Prediction_Data_Results.xlsx')