forked from ltcohen43/Basketball_Cohen_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.py
226 lines (171 loc) · 5.96 KB
/
ball.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
# %%
import pandas as pd
import numpy as np
from plotnine import *
import matplotlib as plt
# import os
# os.getcwd()
# %%
ball = pd.read_csv('../data/ball.csv')
ball = ball.drop(columns='Game_Number')
# %%
ball.describe(exclude=[np.number])
ball.describe()
# %%
# histograms for the numerical variables
f1 = (
ggplot(ball, aes(x='TM')) +
geom_histogram(fill = 'orange', color = 'black') +
labs (x = 'Total Points Scored', y = 'Count',
title = 'Figure 1: Histogram for Total Points Scored') +
theme_bw()
)
f1.save("f1_cohen_Python.png", width = 15, height = 6)
f2 = (
ggplot(ball, aes(x='FG')) +
geom_histogram(fill = 'orange', color = 'black') +
labs (x = 'Field Goal Percentage', y = 'Count',
title = 'Figure 2: Histogram for Field Goal Percentage') +
theme_bw()
)
f2.save("f2_cohen_Python.png", width = 15, height = 6)
f3 = (
ggplot(ball, aes(x='TRB')) +
geom_histogram(fill = 'orange', color = 'black') +
labs (x = 'Total Rebounds', y = 'Count',
title = 'Figure 3: Histogram for Total Rebounds') +
theme_bw()
)
f3.save("f3_cohen_Python.png", width = 15, height = 6)
f4 = (
ggplot(ball, aes(x='AST')) +
geom_histogram(fill = 'orange', color = 'black') +
labs (x = 'Total Assists', y = 'Count',
title = 'Figure 4: Histogram for Total Assists') +
theme_bw()
)
f4.save("f4_cohen_Python.png", width = 15, height = 6)
f5 = (
ggplot(ball, aes(x='STL')) +
geom_histogram(fill = 'orange', color = 'black') +
labs (x = 'Total Steals', y = 'Count',
title = 'Figure 5: Histogram for Total Steals') +
theme_bw()
)
f5.save("f5_cohen_Python.png", width = 15, height = 6)
f6 = (
ggplot(ball, aes(x='BLK')) +
geom_histogram(fill = 'orange', color = 'black') +
labs (x = 'Total Blocks', y = 'Count',
title = 'Figure 6: Histogram for Total Blocks') +
theme_bw()
)
f6.save("f6_cohen_Python.png", width = 15, height = 6)
f7 = (
ggplot(ball, aes(x='TOV')) +
geom_histogram(fill = 'orange', color = 'black') +
labs (x = 'Total Turnovers', y = 'Count',
title = 'Figure 7: Histogram for Total Turnovers') +
theme_bw()
)
f7.save("f7_cohen_Python.png", width = 15, height = 6)
# %%
# box plotsthe numerical variables
ball.boxplot() # all the numerical data box plots
f8 = (
ggplot(ball,aes(y='TM',x=0)) +
geom_boxplot(fill = 'orange', color = 'black') +
labs (x = 'Count', y = 'Total Points Scored',
title = 'Figure 8: Boxplot for Total Points Scored') +
theme_bw()
)
f8.save("f8_cohen_Python.png", width = 15, height = 6)
f9 = (
ggplot(ball,aes(y='FG',x=0))+
geom_boxplot(fill = 'orange', color = 'black') +
labs (x = 'Count', y = 'Field Goal Percentage',
title = 'Figure 9: Boxplot for Field Goal Percentage') +
theme_bw()
)
f9.save("f9_cohen_Python.png", width = 15, height = 6)
f10 = (
ggplot(ball,aes(y='TRB',x=0))+
geom_boxplot(fill = 'orange', color = 'black') +
labs (x = 'Count', y = 'Total Rebounds',
title = 'Figure 10: Boxplot for Total Rebounds') +
theme_bw()
)
f10.save("f10_cohen_Python.png", width = 15, height = 6)
f11 = (
ggplot(ball,aes(y='AST',x=0))+
geom_boxplot(fill = 'orange', color = 'black') +
labs (x = 'Count', y = 'Total Assists',
title = 'Figure 11: Boxplot for Total Assists') +
theme_bw()
)
f11.save("f11_cohen_Python.png", width = 15, height = 6)
f12 = (
ggplot(ball,aes(y='STL',x=0))+
geom_boxplot(fill = 'orange', color = 'black') +
labs (x = 'Count', y = 'Total Steals',
title = 'Figure 12: Boxplot for Total Steals') +
theme_bw()
)
f12.save("f12_cohen_Python.png", width = 15, height = 6)
f13 = (
ggplot(ball,aes(y='BLK',x=0))+
geom_boxplot(fill = 'orange', color = 'black') +
labs (x = 'Count', y = 'Total Blocks',
title = 'Figure 13: Boxplot for Total Blocks') +
theme_bw()
)
f13.save("f13_cohen_Python.png", width = 15, height = 6)
f14 = (
ggplot(ball,aes(y='TOV',x=0))+
geom_boxplot(fill = 'orange', color = 'black') +
labs(x = 'Count', y = 'Total Turnovers',
title = 'Figure 14: Boxplot for Total Turnovers') +
theme_bw()
)
f14.save("f14_cohen_Python.png", width = 15, height = 6)
# %%
# creating new categorical variable
ball['avgstl'] = pd.cut(ball['STL'], bins=[0, 5, 10, 14, 99],
labels=['Low', 'Decent', 'Average', 'High'])
# %%
# bar chart of new avgstl variable
f15 = (
ggplot(ball) +
geom_bar(aes(x='avgstl'), fill = 'orange', color = 'black') +
labs(y = 'Count', x = 'Average Steals',
title = 'Figure 15: Bar Chart of Average Steals') +
theme_bw()
)
f15.save("f15_cohen_Python.png", width = 15, height = 6)
# %%
#frequency table of avg counts - no percentages
ball['avgstl'].value_counts()
# %%
# contingency table of location and outcome variables - no percents
ball_loc_out = pd.crosstab(index= ball['Location'], columns = ball['Outcome'], margins = True)
print(ball_loc_out)
# %%
#100% stacked bar chart of location and outcome variables
f16 = (
ggplot(ball, aes('Location', fill='Outcome')) +
geom_bar( position='fill') +
scale_y_continuous(labels=lambda l: ["%d%%" % (v * 100) for v in l]) + #https://stackoverflow.com/questions/52625307/how-to-change-the-y-axis-to-display-percent-in-python-plotnine-barplot
labs(y = 'Percent of Outcome', title = 'Figure 16: Stacked Bar Chart of Location vs. Outcome') +
theme_bw()
)
f16.save("f16_cohen_Python.png", width = 15, height = 6)
# %%
# scatter plot for assists and total points scored
f17 = (
ggplot(ball, aes(x = 'AST', y = 'TM')) +
geom_point() +
labs(title = 'Figure 17: Scatterplot for Assists and Total Points Scored', x = 'Assists', y = 'Total Points Scored') +
theme_bw()
)
f17.save("f17_cohen_Python.png", width = 15, height = 6)
# %%