-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore.py
More file actions
159 lines (130 loc) · 4.54 KB
/
explore.py
File metadata and controls
159 lines (130 loc) · 4.54 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pandas as pd
from sklearn.model_selection import train_test_split
import scipy.stats as stats
import seaborn as sns
import matplotlib.pyplot as plt
def split_data(df):
'''
Take in a DataFrame and perform a train-test split with a 75/25 ratio.
Return train and test DataFrames.
'''
train, test = train_test_split(df, test_size=0.25, random_state=123)
return train, test
def plot_ford_target(train):
"""
Visualize the target variable.
Parameters:
-----------
train: pandas DataFrame
Returns:
--------
histogram of revenue in the train data set
"""
sns.histplot(data=train, x='adjusted_revenue_B')
plt.title('Ford revenue in Billions')
plt.xlabel('Revenue in Billions')
plt.ylabel('Quarters')
plt.show
return
def check_ford_normalcy(train):
'''
Check a data distribution for normalcy
'''
#check target for normalcy
statistic, p_value = stats.shapiro(train.adjusted_revenue_B)
# Print the test results
print("Shapiro-Wilk Test")
print("Statistic:", statistic)
print("p-value:", p_value)
def plot_att_target(train):
"""
Visualize the target variable.
Parameters:
-----------
train: pandas DataFrame
Returns:
--------
histogram of revenue in the train data set
"""
sns.histplot(data=train, x='adjusted_revenue_A')
plt.title('ATT revenue in Billions')
plt.xlabel('Revenue in Billions')
plt.ylabel('Quarters')
plt.show
return
def check_att_normalcy(train):
'''
Check a data distribution for normalcy
'''
#check target for normalcy
statistic, p_value = stats.shapiro(train.adjusted_revenue_A)
# Print the test results
print("Shapiro-Wilk Test")
print("Statistic:", statistic)
print("p-value:", p_value)
def plot_starbucks_target(train):
"""
Visualize the target variable.
Parameters:
-----------
train: pandas DataFrame
Returns:
--------
histogram of revenue in the train data set
"""
sns.histplot(data=train, x='adjusted_revenue_S')
plt.title('Starbucks revenue in Billions')
plt.xlabel('Revenue in Billions')
plt.ylabel('Quarters')
plt.show
return
def check_starbucks_normalcy(train):
'''
Check a data distribution for normalcy
'''
#check target for normalcy
statistic, p_value = stats.shapiro(train.adjusted_revenue_S)
# Print the test results
print("Shapiro-Wilk Test")
print("Statistic:", statistic)
print("p-value:", p_value)
def spearman_test(df, target_variable, exclude_columns=[], alpha=0.05):
'''
Take in a dataframe and run spearmans rank correlation test against a target variable.
There is an exclude column option, this allows the user the ability to omit any columns
not needing to be tested.
'''
results = []
for column in df.columns:
if column == target_variable or column in exclude_columns:
continue
target_values = df[target_variable]
column_values = df[column]
correlation, p_value = stats.spearmanr(target_values, column_values)
if p_value <= alpha:
result = "Reject the null hypothesis"
else:
result = "Fail to reject the null hypothesis"
results.append({'Variable': column, 'P-Value': p_value, 'Result': result})
return pd.DataFrame(results)
def run_starbucks_stats(train):
'''
Run the stats function on Starbucks revenue
'''
# run spearman test on target
starbucks_stats = spearman_test(train, target_variable='adjusted_revenue_S', exclude_columns=['adjusted_revenue_A','adjusted_revenue_B','p_election', 'midterm_election'], alpha = 0.05)
return starbucks_stats
def run_ford_stats(train):
'''
Run the stats function on Ford's revenue
'''
# Run Spearmanr function on Ford target + other continuous variables in the dataframe (excluding other targets)
ford_stats = spearman_test(train, target_variable='adjusted_revenue_B', exclude_columns=['adjusted_revenue_S','adjusted_revenue_A','p_election', 'midterm_election'], alpha = 0.05)
return ford_stats
def run_att_stats(train):
'''
Run the stats function on ATTs revenue
'''
# Run Spearmanr function on ATT target + other continuous variables in the dataframe (excluding other targets)
att_stats = spearman_test(train, target_variable='adjusted_revenue_A', exclude_columns=['adjusted_revenue_S','adjusted_revenue_B','p_election', 'midterm_election'], alpha = 0.05)
return att_stats