-
Notifications
You must be signed in to change notification settings - Fork 0
/
explore.py
364 lines (294 loc) · 12.9 KB
/
explore.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
pd.set_option('display.max_columns', None)
plt.rcParams.update({'font.size': 15})
from math import sqrt
from scipy import stats
plt.figure(figsize=(16,8))
alpha = 1 - .99
# Explore Visuals
def get_age_visual(df):
plt.figure(figsize=(20,8))
sns.swarmplot(x="root_cause", y="age", data=df)
plt.ylabel("Age of Sewer")
plt.xlabel("Root Cause of SSO Event")
plt.show()
def get_rainfall_visual(df):
plt.figure(figsize=(20,8))
sns.swarmplot(x="root_cause", y="precipitation", data=df)
plt.ylabel("Precipitation")
plt.xlabel("Root Cause of SSO Event")
plt.show()
def get_rain_visual(df):
plt.figure(figsize=(20,8))
sns.catplot(x='precipitation', y='root_cause', hue="rain", data=df)
plt.ylabel("Root Cause of SSO Event")
plt.xlabel("Precipitation")
plt.show()
def get_max_temp_visual(df):
plt.figure(figsize=(20,8))
sns.swarmplot(x="root_cause", y="max_temp", data=df)
plt.xlabel("Root Cause of SSO Event")
plt.ylabel("Max Temperature F°")
plt.show()
def get_min_temp_visual(df):
plt.figure(figsize=(20,8))
sns.swarmplot(x="root_cause", y="min_temp", data=df)
plt.xlabel("Root Cause of SSO Event")
plt.ylabel("Minimum Temperature in F°")
plt.show()
def get_avg_temp_visual(df):
plt.figure(figsize=(20,8))
sns.swarmplot(x="root_cause", y="avg_temp", data=df)
plt.xlabel("Root Cause of SSO Event")
plt.ylabel("Average Temperature in F°")
plt.show()
# Explore stats
# Age of Sewer
def age_stats(df, alpha):
root_cause_values = df.root_cause.value_counts()
root_cause_list = root_cause_values.index.to_list()
overall_age = df.age.mean()
print('\n\nHypothesis Testing:')
print(f'H_null: The age of the sewer is not correlated as the cause of the pipe damage involving a root_cause.')
print(f'H_alt: The age of the sewer is correlated as the cause of the pipe damage involving a root cause.')
root_causes = ''
for l in root_cause_list:
root_causes += f'{l}, '
print(f'\nRoot Causes: \n\t{root_causes}\n')
related_list = []
not_related_list = []
plot_list = []
related_list.append("We reject the null hypothesis that the age of the sewer is not correlated as the cause of the pipe damage invloving: \n")
not_related_list.append("We fail to reject the null hypothesis that the age of the sewer is not correlated as the cause of the pipe damage invloving: \n")
for l in root_cause_list:
if l != 'by pass pump leak':
i = df[df.root_cause == l].age
t, p = stats.ttest_1samp(i, overall_age)
if p < alpha:
related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
plot_list.append()
else:
not_related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
for r in related_list:
print(r)
print('-'*100)
for n in not_related_list:
print(n)
# Rainfall Stats
def rainfall_stats(df, alpha):
root_cause_values = df.root_cause.value_counts()
root_cause_list = root_cause_values.index.to_list()
overall_rainfall = df.precipitation.mean()
print('\n\nHypothesis Testing:')
print(f'H_null: The amount of rainfall is not correlated as the cause of the pipe damage involving a root_cause.')
print(f'H_alt: The amount of rainfall is correlated as the cause of the pipe damage involving a root cause.')
root_causes = ''
for l in root_cause_list:
root_causes += f'{l}, '
print(f'\nRoot Causes: \n\t{root_causes}\n')
related_list = []
not_related_list = []
related_list.append("We reject the null hypothesis that the amount of rainfall is not correlated as the cause of the pipe damage invloving: \n")
not_related_list.append("We fail to reject the null hypothesis that the amount of rainfall is not correlated as the cause of the pipe damage invloving: \n")
for l in root_cause_list:
if l != 'by pass pump leak':
i = df[df.root_cause == l].precipitation
t, p = stats.ttest_1samp(i, overall_rainfall)
if p < alpha:
related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
else:
not_related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
for r in related_list:
print(r)
print('-'*100)
for n in not_related_list:
print(n)
def rain_stats_overview(df, alpha):
contingency_table = pd.crosstab(df.root_cause, df.rain)
test_results = stats.chi2_contingency(contingency_table)
_, p, _, expected = test_results
print("Contingency Table")
print(contingency_table)
print("*****************************************************************")
# 99% confidence level
alpha = 1 - .99
print(f'H_null: Rain is not correlated with the cause of the pipe damage.')
print(f'H_alt: Rain is correlated with the cause of the pipe damage.')
print(' with an alpha of {:.2f} and a p-value of {}:\n'.format(alpha, p))
if p < alpha:
print(f"We reject the null hypothesis. ")
print(f"Rain is correlated with the cause of the pipe damage")
else:
print(f"We fail to reject the null hypothesis.")
print(f"Rain is not correlated with the cause of the pipe damage involving {l}\n")
# Rain Stats
def rain_stats(df, alpha):
root_cause_values = df.root_cause.value_counts()
root_cause_list = root_cause_values.index.to_list()
overall_rainfall = df.precipitation.mean()
print('\n\nHypothesis Testing:')
print(f'H_null: Rain is not correlated as the cause of the pipe damage involving a root_cause.')
print(f'H_alt: Rain is correlated as the cause of the pipe damage involving a root cause.')
root_causes = ''
for l in root_cause_list:
root_causes += f'{l}, '
print(f'\nRoot Causes: \n\t{root_causes}\n')
rain = df.rain
related_list = []
not_related_list = []
related_list.append("We reject the null hypothesis that rain is not correlated as the cause of the pipe damage invloving: \n")
not_related_list.append("We fail to reject the null hypothesis that rain is not correlated as the cause of the pipe damage invloving: \n")
for l in root_cause_list:
if l != 'by pass pump leak':
i = df[df.root_cause == l].rain
contingency_table = pd.crosstab(i, rain)
test_results = stats.chi2_contingency(contingency_table)
_, p, _, expected = test_results
if p < alpha:
related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
else:
not_related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
for r in related_list:
print(r)
print('-'*100)
for n in not_related_list:
print(n)
# Max Temp Stats
def max_temp_stats(df, alpha):
root_cause_values = df.root_cause.value_counts()
root_cause_list = root_cause_values.index.to_list()
overall_max_temp = df.max_temp.mean()
print('\n\nHypothesis Testing:')
print(f'H_null: Max tempurature is not correlated as the cause of the pipe damage involving a root_cause.')
print(f'H_alt: Max tempurature is correlated as the cause of the pipe damage involving a root cause.')
root_causes = ''
for l in root_cause_list:
root_causes += f'{l}, '
print(f'\nRoot Causes: \n\t{root_causes}\n')
related_list = []
not_related_list = []
related_list.append("We reject the null hypothesis that max tempurature is not correlated as the cause of the pipe damage invloving: \n")
not_related_list.append("We fail to reject the null hypothesis that max tempurature is not correlated as the cause of the pipe damage invloving: \n")
for l in root_cause_list:
if l != 'by pass pump leak':
i = df[df.root_cause == l].max_temp
t, p = stats.ttest_1samp(i, overall_max_temp)
if p < alpha:
related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
else:
not_related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
for r in related_list:
print(r)
print('-'*100)
for n in not_related_list:
print(n)
# Min Temp Stats
def min_temp_stats(df, alpha):
root_cause_values = df.root_cause.value_counts()
root_cause_list = root_cause_values.index.to_list()
overall_low_temp = df.min_temp.mean()
print('\n\nHypothesis Testing:')
print(f'H_null: Minimum temperature is not correlated as the cause of the pipe damage involving a root_cause.')
print(f'H_alt: Minimum temperature is correlated as the cause of the pipe damage involving a root cause.')
root_causes = ''
for l in root_cause_list:
root_causes += f'{l}, '
print(f'\nRoot Causes: \n\t{root_causes}\n')
related_list = []
not_related_list = []
related_list.append("We reject the null hypothesis that minimum temperature is not correlated as the cause of the pipe damage invloving: \n")
not_related_list.append("We fail to reject the null hypothesis that minimum temperature is not correlated as the cause of the pipe damage invloving: \n")
for l in root_cause_list:
if l != 'by pass pump leak':
i = df[df.root_cause == l].max_temp
t, p = stats.ttest_1samp(i, overall_low_temp)
if p < alpha:
related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
else:
not_related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
for r in related_list:
print(r)
print('-'*100)
for n in not_related_list:
print(n)
# Avgerage Temp Stats
def avg_temp_stats(df, alpha):
root_cause_values = df.root_cause.value_counts()
root_cause_list = root_cause_values.index.to_list()
overall_avg_temp = df.avg_temp.mean()
print('\n\nHypothesis Testing:')
print(f'H_null: The average temperature is not correlated as the cause of the pipe damage involving a root_cause.')
print(f'H_alt: The average temperature is correlated as the cause of the pipe damage involving a root cause.')
root_causes = ''
for l in root_cause_list:
root_causes += f'{l}, '
print(f'\nRoot Causes: \n\t{root_causes}\n')
related_list = []
not_related_list = []
related_list.append("We reject the null hypothesis that the average temperature is not correlated as the cause of the pipe damage invloving: \n")
not_related_list.append("We fail to reject the null hypothesis that the average temperature is not correlated as the cause of the pipe damage invloving: \n")
for l in root_cause_list:
if l != 'by pass pump leak':
i = df[df.root_cause == l].avg_temp
t, p = stats.ttest_1samp(i, overall_avg_temp)
if p < alpha:
related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
else:
not_related_list.append(' {} with an alpha of {:.2f} and a p-value of {}:'.format(l, alpha, p))
for r in related_list:
print(r)
print('-'*100)
for n in not_related_list:
print(n)
# function for calling each explore
def explore_age(df):
'''
Displays the information of age vs root causes
- displays
'''
get_age_visual(df)
age_stats(df, alpha)
def explore_rainfall(df):
'''
Displays the information of rainfall vs root causes
-
'''
df = df[df.precipitation != 'unknown']
get_rainfall_visual(df)
rainfall_stats(df, alpha)
def explore_rain(df):
'''
Displays the information of rain vs root causes
-
'''
df = df[df.rain != 'unknown']
rain_stats_overview(df, alpha)
get_rain_visual(df)
rain_stats(df, alpha)
def explore_max_temp(df):
'''
Displays the information of max temp vs root causes
-
'''
df = df[df.max_temp != 'unknown']
get_max_temp_visual(df)
max_temp_stats(df, alpha)
def explore_min_temp(df):
'''
Displays the information of min temp vs root causes
-
'''
df = df[df.min_temp != 'unknown']
get_min_temp_visual(df)
min_temp_stats(df, alpha)
def explore_avg_temp(df):
'''
Displays the information of avg temp vs root causes
-
'''
df = df[df.avg_temp != 'unknown']
get_avg_temp_visual(df)
avg_temp_stats(df, alpha)