-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
348 lines (252 loc) · 9.84 KB
/
utils.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
import numpy as np
import pandas as pd
from pathlib import Path
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
from patsy import dmatrices
DATAPATH = './data/'
class Datasets():
'Helper class to load data sets'
## Loading functions
@classmethod
def dax_monthly(self, dtype="numpy"):
'Loads monthly dax data'
path = DATAPATH + "Dax_monthly_prices.csv"
df = pd.read_csv(path,index_col="Date")
if dtype.lower() == "numpy":
return df["Price"].values
elif dtype.lower() == "pandas":
return df
else:
raise AttributeError("dtype should be 'numpy' or 'pandas'")
@classmethod
def dax_daily(self, dtype="numpy"):
path = DATAPATH + "Dax_daily.csv"
df = pd.read_csv(path,index_col="Date")
if dtype.lower() == "numpy":
return df["Adj Close"].dropna().values
elif dtype.lower() == "pandas":
return df
else:
raise AttributeError("dtype should be 'numpy' or 'pandas'")
@classmethod
def salaries(self):
# TODO!
'Loads salaries data as numpy'
path = DATAPATH + "salaries.csv"
df = pd.read_csv(path)
data = [list(df.to_numpy()[:,i]) for i in range(df.shape[1])] # convert to list of len 3
low, medium, high = data[0], data[1], data[2]
return low, medium, high
@classmethod
def online_spent(self, col=["spend"], dtype="numpy"):
'''Loads online spend data (source: https://raw.githubusercontent.com/TaddyLab/MBAcourse/master/examples/web-browsers.csv)
Columns:
- id
- anychildren
- broadband
- hispanic
- race
- region
- spend
'''
path = DATAPATH + "online_spent.csv"
df = pd.read_csv(path)
if not isinstance(col,list): col = [col]
if not col == ["all"]: df = df[col]
if dtype.lower() == "numpy":
return df.values
elif dtype.lower() == "pandas":
return df
else:
raise AttributeError("dtype should be 'numpy' or 'pandas'")
@classmethod
def fake_spent_children(self, n=5_000):
'''Create fake spent data
Input:
n = size of sample
Output:
data = (n,2) array, with
sales = fake online spent data
children = fake has children data (1 = yes, 0=no)
'''
np.random.seed(123875)
p = 0.36
sales = np.random.choice(np.arange(10000,50001), n)
children = np.random.choice([0,1], n, p=[p,1-p])
data = np.vstack((sales, children)).T
return data
@classmethod
def credit_card_debt(self, n=70):
'Creates fake credit card debt data (n=70)'
np.random.seed(1235)
return random_normal_clip(10000,4000,n,98,50000)
@classmethod
def advertising(self, dtype="numpy", col=["TV", "sales"]):
'Loads advertising data set from ISLR'
path = DATAPATH + "advertising.csv"
df = pd.read_csv(path)
if not isinstance(col,list): col = [col]
if not col == ["all"]: df = df[col]
if dtype.lower() == "numpy":
return df.values
elif dtype.lower() == "pandas":
return df
else:
raise AttributeError("dtype should be 'numpy' or 'pandas'")
@classmethod
def cars(self, dtype="numpy", col=["mpg", "horsepower"]):
path = DATAPATH + "Auto.csv"
df = pd.read_csv(path)
if not isinstance(col, list): col = [col]
if not col == ["all"]: df = df[col]
if dtype.lower() == "numpy":
return df.values
elif dtype.lower() == "pandas":
return df
else:
raise AttributeError("dtype should be 'numpy' or 'pandas'")
@classmethod
def fashion(self):
path = DATAPATH + "Fashion.csv"
df = pd.read_csv(path)
return df
###### Helper functions
def random_normal_clip(loc,std,size, low,high):
'''
Draw random samples from a (clipped) normal (Gaussian) distribution.
- low = minimal value for draw
- high = maximum value for draw
(if low = -np.inf and high=np.inf normal distribution is returned)
Draw is repeated until sample size is reached (with low/high condition)
'''
data = []
while len(data) != size:
v = np.random.normal(loc,std)
if (v > low) & (v < high): data.append(v)
return data
##### PLOTS
def _plot_formatter(ax, xlabel, ylabel, title):
if xlabel: ax.set_xlabel(xlabel)
if ylabel: ax.set_ylabel(ylabel)
if title: ax.set_title(title)
for s in ["top","right"]:
ax.spines[s].set_visible(False)
return ax
def plot_line(x,y,xlabel=None, ylabel=None, title=None,zero_origin=True):
'''
Plots simple line chart
INPUT:
x = data for x-axis
y = data for y-axis; if list line is drawn per list element
xlabel, ylabel, title = string with labels
zero_origin = if True x-axis goes through 0
'''
fig, ax = plt.subplots(figsize=(9,7))
ax = _plot_formatter(ax,xlabel,ylabel, title)
if (not isinstance(y, list)): y = [y]
for series in y:
ax.plot(x,series)
if zero_origin:
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
return fig, ax
def plot_scatter(x,y,xlabel=None, ylabel=None, title=None,zero_origin=True):
'''
Plots simple scatter
INPUT:
x = data for x-axis
y = data for y-axis; if list line is drawn per list element
xlabel, ylabel, title = string with labels
zero_origin = if True x-axis goes through 0
'''
fig, ax = plt.subplots(figsize=(9,7))
ax = _plot_formatter(ax,xlabel,ylabel, title)
if (not isinstance(y, list)): y = [y]
for series in y:
ax.scatter(x,series)
if zero_origin:
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
return fig, ax
def plot_step(x,y,xlabel=None, ylabel=None, title=None, exclusion_points=True):
'''
Plots step chart (for discrete cdf)
INPUT:
x = data for x-axis
y = data for y-axis
xlabel, ylabel, title = string with labels
exclusion_points = if True point until valid is marked
'''
fig, ax = plt.subplots(figsize=(9,7))
ax = _plot_formatter(ax,xlabel,ylabel, title)
ax.hlines(y,xmin=x, xmax=x+1,color="darkblue")
if exclusion_points:
sc = ax.scatter(x+1,y,color="black")
sc.set_facecolor("none")
return fig, ax
def plot_bar(x,y,xlabel=None, ylabel=None, title=None, zero_origin=True):
'''
Plots simple bar chart
INPUT:
x = data for x-axis
y = data for height; if list line is drawn per list element
xlabel, ylabel, title = string with labels
zero_origin = if True x-axis goes through 0
'''
fig, ax = plt.subplots(figsize=(9,7))
ax = _plot_formatter(ax,xlabel,ylabel, title)
if not isinstance(y, list): y = [y]
for series in y:
ax.bar(x,series)
if zero_origin: ax.spines['bottom'].set_position('zero')
return fig, ax
def plot_hist(data, show_prob=False, xlabel=None, ylabel=None, title=None):
fig, ax = plt.subplots(figsize=(9,7))
ax = _plot_formatter(ax,xlabel,ylabel, title)
values, height = np.unique(data,return_counts=True)
if show_prob: height = height/len(data)
ax.bar(values,height)
return fig, ax
def plot_density(data,kde=True,xlabel=None, ylabel=None, title=None):
fig, ax = plt.subplots(figsize=(9,7))
ax = _plot_formatter(ax,xlabel,ylabel, title)
if not isinstance(data, list): data = [data]
for d in data:
sns.distplot(d,ax=ax, kde=kde)
return fig, ax
def lreg_summary(X,y, make_intercept=True):
if make_intercept: X = sm.add_constant(X)
return sm.OLS(y,X).fit().summary()
def make_y_X(s: str, data:pd.DataFrame) -> (pd.DataFrame, pd.DataFrame):
'''Generates two dataframes based on model formulation
Function is essentially a wrapper around patsy.dmatrices
INPUT:
s = string representing the model (patsy style)
data = pandas dataframe holding the variables
OUTPUT:
y = dataframe holding dependent variable
X = dataframe holding independent variable(s)
'''
y, X = dmatrices(s, data=data, return_type="dataframe")
return y, X
# NOTE: code taken from https://scikit-learn.org/stable/auto_examples/cluster/plot_agglomerative_dendrogram.html
from scipy.cluster.hierarchy import dendrogram
def plot_dendrogram(model, **kwargs):
# Create linkage matrix and then plot the dendrogram
# create the counts of samples under each node
counts = np.zeros(model.children_.shape[0])
n_samples = len(model.labels_)
for i, merge in enumerate(model.children_):
current_count = 0
for child_idx in merge:
if child_idx < n_samples:
current_count += 1 # leaf node
else:
current_count += counts[child_idx - n_samples]
counts[i] = current_count
linkage_matrix = np.column_stack([model.children_, model.distances_,
counts]).astype(float)
# Plot the corresponding dendrogram
dendrogram(linkage_matrix, **kwargs)