-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore.py
More file actions
217 lines (180 loc) · 7.68 KB
/
explore.py
File metadata and controls
217 lines (180 loc) · 7.68 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
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
import pandas as pd
import numpy as np
import os
from env import host, user, password
import scipy as sp
from env import host, user, password
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, QuantileTransformer, PowerTransformer, RobustScaler, MinMaxScaler
from math import sqrt
from scipy import stats
from sklearn.cluster import KMeans
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
################## Explore ##############################################################################################
def nulls_by_col(df):
'''
Calucluates null by column
'''
num_missing = df.isnull().sum()
rows = df.shape[0]
pct_missing = num_missing / rows
cols_missing = pd.DataFrame({'number_missing_rows': num_missing, 'percent_rows_missing': pct_missing})
return cols_missing
def nulls_by_row(df):
'''
Calculates null by row
'''
num_cols_missing = df.isnull().sum(axis=1)
pct_cols_missing = df.isnull().sum(axis=1)/df.shape[1]*100
rows_missing = pd.DataFrame({'num_cols_missing': num_cols_missing, 'pct_cols_missing': pct_cols_missing}).reset_index().groupby(['num_cols_missing','pct_cols_missing']).count().rename(index=str, columns={'index': 'num_rows'}).reset_index()
return rows_missing
def df_summary(df):
'''
This function returns all the summary information of the dataframe
'''
print('The shape of the df:')
print(df.shape) # df shape (row/column)
print('\n')
print('Columns, Non-Null Count, Data Type:')
print(df.info()) # Column, Non Null Count, Data Type
print('\n')
print('Summary statistics for the df:')
print(df.describe()) # Summary Statistics on Numeric Data Types
print('\n')
print('Number of NaN values per column:')
print(df.isna().sum()) # NaN by column
print('\n')
print('Number of NaN values per row:')
print(df.isnull().sum(axis=1)) # NaN by row
for col in df.columns:
print('-' * 40 + col + '-' * 40 , end=' - ')
display(df[col].value_counts(dropna=False).head(10))
#display(df_resp[col].value_counts()) # Displays all Values, not just First 10
# df_summary(df) | To call function
################## Outliers and IQR ####################
def get_upper_outliers(s, k):
'''
Given a series and a cutoff value, k, returns the upper outliers for the
series.
The values returned will be either 0 (if the point is not an outlier), or a
number that indicates how far away from the upper bound the observation is.
'''
q1, q3 = s.quantile([.25, .75])
iqr = q3 - q1
upper_bound = q3 + k * iqr
return s.apply(lambda x: max([x - upper_bound, 0]))
def add_upper_outlier_columns(df, k): # Call This Function First
'''
Add a column with the suffix _outliers for all the numeric columns
in the given dataframe.
'''
# outlier_cols = {col + '_outliers': get_upper_outliers(df[col], k)
# for col in df.select_dtypes('number')}
# return df.assign(**outlier_cols)
for col in df.select_dtypes('number'):
df[col + '_outliers'] = get_upper_outliers(df[col], k)
return df
###################################### STEP #1
# In the next cell type the following
'''
'''
#This text prints information regrding the outlier columns created
'''
wrangle.add_upper_outlier_columns(df, k=3)
outlier_cols = [col for col in df if col.endswith('_outliers')]
for col in outlier_cols:
print('~~~\n' + col)
data = df[col][df[col] > 0]
print(data.describe())
'''
###################################### STEP #2
# Print this code to remove colums in dataframe
'''
X_train_explore.drop([x for x in df if x.endswith('_outliers')], 1, inplace = True)
'''
##################################################################################################################
def elbow_plot(X_train_scaled, cluster_vars):
'''
Given X_train and cluster variables plots an elbow_plot
'''
# elbow method to identify good k for us
ks = range(1,10)
# empty list to hold inertia (sum of squares)
sse = []
# loop through each k, fit kmeans, get inertia
for k in ks:
kmeans = KMeans(n_clusters=k)
kmeans.fit(X_train_scaled[cluster_vars])
# inertia
sse.append(kmeans.inertia_)
print(pd.DataFrame(dict(k=ks, sse=sse)))
# plot k with inertia
plt.plot(ks, sse, 'bx-')
plt.xlabel('k')
plt.ylabel('SSE')
plt.title('Elbow method to find optimal k')
plt.show()
##################################################################################################################
def run_kmeans(X_train, X_train_scaled, k, cluster_vars, cluster_col_name):
'''
Creates a kemeans object and creates a dataframe with cluster information
'''
# create kmeans object
kmeans = KMeans(n_clusters = k, random_state = 13)
kmeans.fit(X_train_scaled[cluster_vars])
# predict and create a dataframe with cluster per observation
train_clusters = \
pd.DataFrame(kmeans.predict(X_train_scaled[cluster_vars]),
columns=[cluster_col_name],
index=X_train.index)
return train_clusters, kmeans
##################################################################################################################
def kmeans_transform(X_scaled, kmeans, cluster_vars, cluster_col_name):
'''
Takes in a dataframe and returns custers that have been predicted on that dataframe
'''
kmeans.transform(X_scaled[cluster_vars])
trans_clusters = \
pd.DataFrame(kmeans.predict(X_scaled[cluster_vars]),
columns=[cluster_col_name],
index=X_scaled.index)
return trans_clusters
##################################################################################################################
def get_centroids(cluster_vars, cluster_col_name, kmeans):
'''
Takes in kmeans and cluster variables to produce centroids
'''
centroid_col_names = ['centroid_' + i for i in cluster_vars]
centroids = pd.DataFrame(kmeans.cluster_centers_,
columns=centroid_col_names).reset_index().rename(columns={'index': cluster_col_name})
return centroids
##################################################################################################################
def add_to_train(train_clusters, centroids, X_train, X_train_scaled, cluster_col_name):
'''
Takes in a datafrme, clusters, centroids and returns a new dataframe with all information concated together
'''
# concatenate cluster id
X_train2 = pd.concat([X_train, train_clusters], axis=1)
# join on clusterid to get centroids
X_train2 = X_train2.merge(centroids, how='left',
on=cluster_col_name).\
set_index(X_train.index)
# concatenate cluster id
X_train_scaled2 = pd.concat([X_train_scaled, train_clusters],
axis=1)
# join on clusterid to get centroids
X_train_scaled2 = X_train_scaled2.merge(centroids, how='left',
on=cluster_col_name).\
set_index(X_train.index)
return X_train2, X_train_scaled2
##################################################################################################################
def r2(x, y):
'''
Takes in x and y and returns pearsons correlation coefficent
'''
return stats.pearsonr(x, y)[0] ** 2
##################################################################################################################