-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_prep.py
More file actions
145 lines (93 loc) · 4.34 KB
/
data_prep.py
File metadata and controls
145 lines (93 loc) · 4.34 KB
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
#!/usr/bin/env python
# coding: utf-8
# In[58]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# In[2]:
pd.set_option('display.max_columns', None)
data = pd.read_csv("./train.csv")
data.head()
# In[3]:
data.columns
# In[4]:
data.dtypes
# In[53]:
homeArray = []
awayArray = []
yardsGained = 0
playDirection = ''
count = 0
for index, row in data.iterrows():
if row['PlayId'] == 20170907000118:
if row['Team'] == 'away':
awayArray.append([row['X'], row['Y'], row['PlayerWeight'], row['Orientation'], row['NflId'], row['NflIdRusher']])
else:
homeArray.append([row['X'], row['Y'], row['PlayerWeight'], row['Orientation'], row['NflId'], row['NflIdRusher']])
yardsGained = row['Yards']
playDirection = row['PlayDirection']
else:
break
print(homeArray)
print(awayArray)
homeX, homeY, homeWeight, homeOrientation, homeId, homeRusherId = zip(*homeArray)
awayX, awayY, awayWeight, awayOrientation, awayId, awayRusherId = zip(*awayArray)
fig = plt.figure(figsize=(60,30))
fig.suptitle('test title', fontsize=20)
field = patches.Rectangle((0, 0), 120, 53.3, linewidth=5.5,
edgecolor='black', facecolor='lightgrey', zorder=0)
fig, ax = plt.subplots(1, figsize=(60,30))
ax.add_patch(field)
plt.plot([10, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60, 60, 70, 70, 80,
80, 90, 90, 100, 100, 110, 110, 120, 0, 0, 120, 120],
[0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3,
53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 53.3, 0, 0, 53.3],
color='black')
plt.plot([60, 60], [0, 53.3], color='yellow')
plt.xlim(-10, 120); plt.ylim(0, 53.3)
plt.axis('off')
for i in range(len(homeX)):
if homeId[i] == homeRusherId[i]:
if playDirection == 'left':
plt.plot([homeX[i] - yardsGained, homeX[i] - yardsGained], [0, 58.3],linewidth=5, color='aqua')
if playDirection == 'right':
plt.plot([homeX[i] + yardsGained, homeX[i] + yardsGained], [0, 58.3],linewidth=5, color='aqua')
plt.plot(homeX[i],homeY[i], color='red', markersize=homeWeight[i]/5, marker=(3,0,homeOrientation[i]),linestyle='solid',markeredgecolor="red")
plt.plot(homeX[i],homeY[i], color='red', markersize=homeWeight[i]/5, marker=(2,0,homeOrientation[i]),linestyle='solid',markeredgecolor="red")
elif awayId[i] == awayRusherId[i]:
if playDirection == 'left':
plt.plot([awayX[i] - yardsGained, awayX[i] - yardsGained], [0, 58.3],linewidth=5, color='aqua')
if playDirection == 'right':
plt.plot([awayX[i] + yardsGained, awayX[i] + yardsGained], [0, 58.3],linewidth=5, color='aqua')
plt.plot(awayX[i], awayY[i], color="blue", markersize=awayWeight[i]/5, marker=(2,0,awayOrientation[i]), linestyle='solid',markeredgecolor="blue")
plt.plot(awayX[i], awayY[i], color="blue", markersize=awayWeight[i]/5, marker=(3,0,awayOrientation[i]), linestyle='solid',markeredgecolor="blue")
# else:
plt.plot(homeX[i],homeY[i], color='none', markersize=homeWeight[i]/5, marker=(3,0,homeOrientation[i]),linestyle='solid',markeredgecolor="red")
plt.plot(homeX[i],homeY[i], color='none', markersize=homeWeight[i]/5, marker=(2,0,homeOrientation[i]),linestyle='solid',markeredgecolor="red")
plt.plot(awayX[i], awayY[i], color="none", markersize=awayWeight[i]/5, marker=(2,0,awayOrientation[i]), linestyle='solid',markeredgecolor="blue")
plt.plot(awayX[i], awayY[i], color="none", markersize=awayWeight[i]/5, marker=(3,0,awayOrientation[i]), linestyle='solid',markeredgecolor="blue")
plt.show()
# if row[index]['PlayId'] == 20170907000118:
# print(row[index]['PlayId'])
# print(row[index]['X'])
# array.append((row[3], row[4]))
# print(array)
# x, y = zip(*array)
# plt.scatter(x, y)
# plt.show()
# In[55]:
modDataFrame = data.drop(
["Orientation", "DisplayName", "JerseyNumber", "VisitorScoreBeforePlay", "HomeScoreBeforePlay", "PlayerBirthDate", "VisitorTeamAbbr", "HomeTeamAbbr","Stadium"], axis=1)
print(modDataFrame.columns)
matrix = modDataFrame.values
# In[56]:
print(matrix)
# In[60]:
#Using Pearson Correlation
plt.figure(figsize=(30,25))
cor = modDataFrame.corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.Reds)
plt.show()
# In[ ]: