-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathR_to_python.Rmd
More file actions
743 lines (511 loc) · 21.4 KB
/
R_to_python.Rmd
File metadata and controls
743 lines (511 loc) · 21.4 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
---
title: "R to Python for Data Analysis"
author: "Leonardo Uchoa"
date: "3/31/2020"
output:
pdf_document:
toc: true
number_sections: true
header-includes:
- \usepackage{amsbsy}
- \usepackage{amsmath}
- \usepackage{float}
- \usepackage{graphicx}
---
\newpage
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# The tables
That's my R to python port. It's intended to make my approach to learning python faster and its mostly composed of data wrangling routine tools. Many of those are already listed in other sources.
<!-- -------Data wrangling------- -->
\begin{table}[H]
\centering
\caption{Data Wrangling}
\begin{tabular}{lr}
R & Python \\
\hline
dim & df.shape (pd) \\
stop & raise ValueError \\
str & df.dtypes / df.info (pd) \\
unique & np.unique (np) \\
sort & np.sort (np) \\
rbind & np.hstack (np) \\
summary & df.describe (pd) \\
\texttt{group\_by} & df.groupby (pd) \\
count & \verb|df.value_count| (pd) \\
table & np.bincount (np array) \\
apply & df.apply (pd) \\
if.else & df.where[case,true,false] (pd) \\
table & pd.crosstab \\
corr & np.corrcoef (np) \\
mutate(df, c=a-b) & df.assign(c=df['a']-df['b']) (pd) \\
colSums(is.na()) & df.isnull().sum() (pd) \\
na.omit & df.dropna(axis=X) (pd) \\
*imputation* & df.fillna(df.mean()) (pd) \\
colnames() <- & df.colnames (pd) \\
\%in\% & df.isin(list_of_elements) (pd)
\end{tabular}
\end{table}
<!-- -------Plotting------- -->
\begin{table}[H]
\centering
\caption{Plotting facilities}
\begin{tabular}{lr}
base R & matplotlib \\
\hline \\
pairs & scatterplotmatrix (mlext subm) \\
heatmap & heatmap (mlext subm)\\
image & np.imshow(img,cmap = "Color") (plt)\\
\end{tabular}
\end{table}
<!-- -------Linear Algebra------- -->
\begin{table}[H]
\centering
\caption{Linear Algebra}
\begin{tabular}{lr}
R & Python \\
\hline
eigen & np.linalg.eig (np) \\
\%*\% & np.dot \\
----- & np.matmul \\
\end{tabular}
\end{table}
<!-- -------Abreviations------- -->
\begin{table}[H]
\centering
\caption{Abreviations}
\begin{tabular}{lr}
Module Abreviation & Module \\
\hline
pd & Pandas \\
np & Numpy \\
plt & matplotlib \\
---- & mlextend \\
\end{tabular}
\end{table}
\newpage
\begin{center}
\section*{Usefull examples}
\end{center}
\addtocounter{section}{0}
These are convertions of the commands I use the most and some other ( because they're different from what I'm used to do in R) when analysing data in R.
I wrote this document using Rstudio and Rmarkdown. So in order to load python within R the thing to do was to use `reticulate`. But I've also installed all my python packages using anaconda because it handles compatability between libraries more efficiently. In that case you can load `reticulate` and set it's path with `use_python` for it to load the packages installed via anaconda \footnote{The paths are \textbf{not} the same}.
```{r}
library(reticulate)
use_python("/home/leonardo/anaconda3/bin/python")
```
```{python}
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
```
\begin{center}
\section{Numpy Broadcasting}
\end{center}
From [numpy's manual](https://numpy.org/doc/stable/user/basics.broadcasting.html) broadcasting in python is a way to "describe how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is 'broadcast' across the larger array so that they have compatible shapes".
- The good thing is that it allows for internal C looping instead of running in python, which is more efficient.
- The bad thing is that sometimes it leads to some inefficiencies
But it's different from what happens in R in the sense that's less harmfull. In we complete the smaller vector and can (and wp1 will) eventually cause some disasters. But in after some update at least we receive a warning message, so stay alert!
```{r}
a = 1:10
b = 1:5
a + b
```
In python receive an error message instead of a warning, which is better.
```{python}
a = np.array([1,2,3,4,5])
b = np.array([1,2])
print(a + b)
print(a * b)
# but adding a scalar to an array is cool and allowed
print(a + 100)
```
But the uncool thing still remais and some random NaNs may appear.
```{r}
B = pd.DataFrame([[1,2,3],[1,2,3]])
C = pd.DataFrame([100,200])
print(B + C)
print(C + B)
print(B + C.T)
```
Even so broadcasting in python allows us to do some cool things, like computing row/column-wise percentages. For example:
```{python}
A = pd.DataFrame([[np.random.rand() for _ in range(5)] for _ in range(5)])
rowwise_total = A.sum(axis = 0)
print(100 * A/rowwise_total)
```
As some general rules if A is (m,n) and B is (1,n) or (m,1) then `A +,-,*,/ B` will make the result be of dimension (m,n)
\begin{center}
\section{Counting per column}
\end{center}
\textbf{\underline{Source}:} StackOverflow.
```{python}
df = pd.DataFrame(np.random.randint(0, 2, (10, 4)), columns=list('abcd'))
df.apply(pd.Series.value_counts)
```
\begin{center}
\section{Multi argument iteration with zip}
\end{center}
Zip allows us to construct tuples for iterating over multiple arguments.
```{python}
players = [ "Sachin", "Sehwag", "Gambhir", "Dravid", "Raina" ]
scores = [100, 15, 17, 28, 43 ]
# Lets see how it constructs the tuples
print(tuple(zip(players, scores)))
# Now we just need to iterate over them
for pl, sc in zip(players, scores):
print ("Player : %s Score : %d" %(pl, sc))
```
\begin{center}
\section{Categorical data encoding}
\end{center}
\textbf{\underline{Source}:} Chapter 4 of Python Machine Learning [2].
For this section we're working the toy data bellow
```{python}
df = pd.DataFrame([
['green', 'M', 10.1, 'class2'],
['red', 'L', 13.5, 'class1'],
['blue', 'XL', 15.3, 'class2']])
df.columns = ['color', 'size', 'price', 'classlabel']
df
```
In both approaches bellow we use a dictionary the create the mapping identifier for the `map` method. Remember that according to [w3schools](https://www.w3schools.com/python/python_dictionaries.asp) a dictionary is
>*A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.*
## Encoding ordinals - create labels manually
```{python}
#create the dict mapping from ordinal to integer
size_mapping = {'XL': 3,'L': 2,'M': 1}
#use map to in the desired column get the mapped values
df['size'] = df['size'].map(size_mapping)
```
## Encoding nominals - creating labels automatically
```{python}
class_mapping = {label: idx for idx, label in enumerate(np.unique(df['classlabel']))}
```
Now what that command is doing is looping through the iterators `idx` and `label` (created by the `enumerate` function) in the unique values of the `classlabel` column and assigning both to `label` and `idx`. Let's see
```{python}
print(list(
enumerate(np.unique(df['classlabel']))
))
```
So iterating through the list we get to assign "class1"/"class2" to `label` and 0/1 to `idx`\footnote{Note the inversion in `label: idx for idx, label` }. Finally the last step to map
```{python}
df['classlabel'] = df['classlabel'].map(class_mapping)
df
```
Want to get the mapping backwards? Access the `items` method in the `class_mapping` object and loop again
```{python}
inv_class_mapping = {a:b for b,a in class_mapping.items()}
df['classlabel'] = df['classlabel'].map(inv_class_mapping)
df
```
**Ps.: There's also an object in `skitlearn` module `preprocessing` that does this: `LabelEncoder`**
## Creating Dummies in Design Matrix
\underline{First way: Pandas }
Now to create dummy variables for the design matrix, there's a simple way using pandas
```{python}
df_dm = pd.get_dummies(df[['price', 'color', 'size']], drop_first = True)
df_dm
```
The `drop_first = True` is important. Otherwise we woud get another column name "color_blue" with an $(0,0,1)^{T}$ entry which we do not need because when the other 2 columns are 0 we already encode the blue color.
\underline{Second way: sklearns' OneHotEncoder}
The OneHotEncoder function arguments are (name, transformer,
columns) tuples
```{python}
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
#creat the object
color_ohe = OneHotEncoder(categories='auto', drop='first')
c_transf = ColumnTransformer([
('onehot', color_ohe, [0]), # respectively 'transformer_name',transformer_object,column
('nothing', 'passthrough', [1, 2]) # respectively 'action_1','action_2', columns for action_1/2
])
#note that the function input is an np array
c_transf.fit_transform(df[['color', 'size', 'price']].values).astype(float)
```
\newpage
\begin{center}
\section{Train-test Split and basic pre-process}
\end{center}
\underline{Source:} Chapter 4 of Python Machine Learning [2].
## Train-test Split
```{python}
from sklearn import datasets
from sklearn.model_selection import train_test_split
wine = datasets.load_wine()
wine.data.shape
wine.target.shape
X = wine.data
y = wine.target
X_train, X_test, y_train, y_test = train_test_split(X,y,
test_size=0.3, # split
random_state=0, # set.seed
stratify=y) #stratification of data based on target frequencies
```
## Basic Continuous pre-process
```{python, eval = FALSE}
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
stdsc = StandardScaler()
X_train_std = stdsc.fit_transform(X_train)
X_test_std = stdsc.transform(X_test)
mms = MinMaxScaler()
X_train_mms = mms.fit_transform(X_train)
X_test_mms = mms.transform(X_test)
pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train_std)
X_test_pca = pca.transform(X_test_std)
lda = LDA(n_components=2)
X_train_lda = lda.fit_transform(X_train_std, y_train)
```
\newpage
\begin{center}
\section{Basic Pipeline}
\end{center}
## Cross Validation
```{python, eval = FALSE}
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
pipe_lr = make_pipeline(StandardScaler(),
PCA(n_components=2),
LogisticRegression(random_state=1,
solver='lbfgs'))
# First way
from sklearn.model_selection import StratifiedKFold
kfold = StratifiedKFold(n_splits=10).split(X_train, y_train)
scores = []
for k, (train, test) in enumerate(kfold):
pipe_lr.fit(X_train[train], y_train[train])
score = pipe_lr.score(X_train[test], y_train[test])
scores.append(score)
print('Fold: %2d, Class dist.: %s, Acc: %.3f' % (k+1,
np.bincount(y_train[train]), score))
# Second and Better way
from sklearn.model_selection import cross_val_score
scores = cross_val_score(estimator=pipe_lr,
X=X_train,y=y_train,cv=10,n_jobs=1)
print('CV accuracy scores: %s' % scores)
```
## Sample Size Learning Curves
One thing to note here is, according to [2]
>Note that we passed max_iter=10000 as an additional argument when instantiating the LogisticRegression object (which uses 1,000 iterations as a default) to avoid convergence issues for the smaller dataset sizes or extreme regularization parameter
values.
Which can be valuable for other algorithms too. Note also that stratified k-fold CV is the default routine.
```{python, eval = FALSE}
import matplotlib.pyplot as plt
from sklearn.model_selection import learning_curve
pipe_lr = make_pipeline(StandardScaler(),
LogisticRegression(penalty='l2',
random_state=1,
solver='lbfgs',
max_iter=10000))
train_sizes, train_scores, test_scores =\ learning_curve(estimator=pipe_lr,
X=X_train,
y=y_train,
train_sizes=np.linspace(
0.1, 1.0, 10),
cv=10,
n_jobs=1)
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
plt.plot(train_sizes, train_mean, color='blue', marker='o',
markersize=5, label='Training accuracy')
plt.fill_between(train_sizes,train_mean + train_std, train_mean - train_std,alpha=0.15, color='blue')
plt.plot(train_sizes, test_mean,color='green', linestyle='--', marker='s', markersize=5,label='Validation accuracy')
plt.fill_between(train_sizes,
test_mean + test_std,
test_mean - test_std,
alpha=0.15, color='green')
plt.grid()
plt.xlabel('Number of training examples')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.ylim([0.8, 1.03])
plt.show()
```
## Validation Curves
Here stratified k-fold CV is the default routine.
```{python, eval = FALSE}
from sklearn.model_selection import validation_curve
param_range = [0.001, 0.01, 0.1, 1.0, 10.0, 100.0]
train_scores, test_scores = validation_curve(
estimator=pipe_lr,
X=X_train,
y=y_train,
param_name='logisticregression__C',
param_range=param_range,
cv=10)
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
plt.plot(param_range, train_mean,
color='blue', marker='o',
markersize=5, label='Training accuracy')
plt.fill_between(param_range, train_mean + train_std,
train_mean - train_std, alpha=0.15,
color='blue')
plt.plot(param_range, test_mean,
color='green', linestyle='--',
marker='s', markersize=5,
label='Validation accuracy')
plt.fill_between(param_range,
test_mean + test_std,
test_mean - test_std,
alpha=0.15, color='green')
plt.grid()
plt.xscale('log')
plt.legend(loc='lower right')
plt.xlabel('Parameter C')
plt.ylabel('Accuracy')
plt.ylim([0.8, 1.0])
plt.show()
```
## Tuning hyperparameters via grid search
Here we're grid searching hyperparameters for an SVM with two different kernel functions and their respective parameters\footnote{Using RandomizedSearchCV in scikit-learn, we can perform Randomized Grid Search. See [3]}. Thats is: for the linear basis function we search for the range and for the rbf we search the range and gamma.
Here we have two dictionaries:
- `{'svc__C': param_range,'svc__kernel': ['linear']}` and
- `{'svc__C': param_range,'svc__gamma': param_range,'svc__kernel': ['rbf']}`
who compose our grids.
```{python, eval = FALSE}
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
pipe_svc = make_pipeline(StandardScaler(),SVC(random_state=1))
param_range = [0.0001, 0.001, 0.01, 0.1,1.0, 10.0, 100.0, 1000.0]
param_grid = [{'svc__C': param_range,'svc__kernel': ['linear']},
{'svc__C': param_range,'svc__gamma': param_range,
'svc__kernel': ['rbf']}]
gs = GridSearchCV(estimator=pipe_svc,param_grid=param_grid
scoring='accuracy',cv=10,refit=True,n_jobs=-1)
gs = gs.fit(X_train, y_train)
clf = gs.best_estimator_
clf.fit(X_train, y_train)
print('Test accuracy: %.3f' % clf.score(X_test, y_test))
```
As note in [2] a great thing to keep in mind is
> Please note that fitting a model with the best settings ( gs.best_estimator_ ) on the training set manually via clf.fit(X_train, y_train) after completing the grid search is not necessary. The GridSearchCV class has a refit parameter, which
will refit the gs.best_estimator_ to the whole training set automatically if we set refit=True (default).
## Nested Cross Validation
See [4]. Following [2], chapter 6, let's compare a decision tree and an svc.
```{python, eval= FALSE}
from sklearn.tree import DecisionTreeClassifier
gs_svc = GridSearchCV(estimator=pipe_svc,param_grid=param_grid,scoring='accuracy',cv=2)
scores_svc = cross_val_score(gs_svc, X_train, y_train,scoring='accuracy', cv=5)
gs_tree = GridSearchCV(estimator=DecisionTreeClassifier(random_state=0),
param_grid=[{'max_depth': [1, 2, 3,4, 5, 6,7, None]}],scoring='accuracy',cv=2)
scores_tree = cross_val_score(gs_tree, X_train, y_train,scoring='accuracy',cv=5)
```
\newpage
\begin{center}
\section{Performance Evaluation Metrics}
\end{center}
```{python, eval= FALSE}
from sklearn.metrics import confusion_matrix
y_pred = pipe_svc.predict(X_test)
confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)
```
```{python, eval= FALSE}
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score, f1_score
from sklearn.metrics import roc_curve, auc, roc_auc_score
precision_score(y_true=y_test, y_pred=y_pred)
f1_score(y_true=y_test, y_pred=y_pred)
```
## Making our own scorer
```{python, eval= FALSE}
pre_scorer = make_scorer(score_func=precision_score,pos_label=1, greater_is_better=True,average='micro')
```
## Resampling for Class Imbalances
```{python, eval= FALSE}
from sklearn.utils import resample
X_upsampled, y_upsampled = resample(X_imb[y_imb == 1],y_imb[y_imb == 1],replace=True,n_samples=X_imb[y_imb == 0].shape[0],random_state=123)
X_bal = np.vstack((X[y == 0], X_upsampled))
y_bal = np.hstack((y[y == 0], y_upsampled))
```
Alternatives:
- SMOTE;
- `imbalanced-learn` - see [5]
\newpage
\begin{center}
\section{Misc}
\end{center}
### Unzipping tarballs within Python and Working with the `with` expression
The `with` expressin is a really nice feature of python as it is the responsible for setting things up for task you want to do and then clean things down. A classic and very used example of it is to open a file and do some things.
In order to open a file we must first to establish a connection with it. After this, we do what we gotta do and then must close the file. It looks like this
```{python, eval=FALSE}
file = open("file_name")
do something #eg file.read()
file.close()
```
Now using `with` it becomes
```{python, eval=FALSE}
with open("file_name") as file:
do something
```
The `with` expression sets the connection, waits for you to do your task and even close it for you. Another example is extracting tarballs, is illustrated as follows.
```{python, eval = FALSE}
import tarfile
with tarfile.open('file_name.tar.gz', 'r:gz') as tar:
tar.extractall()
```
But how does it works? It's very well explained in [7] and I recommend it.
## Sourcing Python Files
\textbf{\underline{Source}:} StackOverflow See [6]
```{python, eval = FALSE}
exec(open("NeuralNetMLP.py").read())
os.system("file.py") # need to import os
```
## Serializing Objects
The mainstream way is to use `pickle`. It can be done with `joblib`, but it is best suited for scheduling tasks, actually.
```{python, eval = FALSE}
import pickle
filename = 'object.sav'
pickle.dump(nn, open(filename, 'wb'))
loaded_object = pickle.load(open(filename, 'rb'))
loaded_object
import joblib
filename = 'object.sav'
joblib.dump(model, filename)
loaded_object = joblib.load(filename)
```
## Plotting with Seaborn in the Console
In order to display seaborn plots without some IDE that automatically handles plotting, you need to also call the matplotlib's pyplot submodule function `show`, as shown below.
```{python}
import seaborn as sb
import matplotlib.pyplot as plt
iris = sb.load_dataset('iris')
sb.relplot(data='iris',x='sepal_length',y='petal_length')
plt.show()
```
## Modules, projects, packages
- **Module**: is just a file ending with a `.py` extension
- **Package**: usually is a directory containing at aleast a module *and* a file called __init__.py.
- When the __init__.py file is imported, the code within it will be run. So it is used to import other files (modules)
- Specifying the __init__.py file: simply put you're importing modules, so the standard *import foo from asd as bla* works well. But you can also upgrade your init file to support more complex tasks, like unit testing. For example see the scikit-learn [linear model submodule](https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/linear_model/__init__.py).
- **Projects**: is a collection of packages
**Important**: If you're creating a python library, you **must** specify the __init__ files because they are the reference to what's being imported. Otherwise when importing the lib, python won't find the modules you want. See [this](https://stackoverflow.com/a/14132912)
## Python Programming nuggets
- [Assertions vs Exceptions](https://stefan.sofa-rockers.org/2018/04/16/assertions-and-exceptions/)
- [Solved!! UnboundLocalError: local variable referenced before assignment](https://careerkarma.com/blog/python-local-variable-referenced-before-assignment/)
- Python Packages [good overview](https://python-packaging-tutorial.readthedocs.io/en/latest/setup_py.html), a [video](https://www.youtube.com/watch?v=wCGsLqHOT2I), a [simplified version](https://oneraynyday.github.io/dev/2017/08/02/Python-File-Structure/) and more [nice explanations](https://docs.python-guide.org/writing/structure/)
- [Using wheel to create packages](https://hynek.me/articles/sharing-your-labor-of-love-pypi-quick-and-dirty/)
- [Automated Jyputers as altenative do automated Rmds](https://medium.com/capital-fund-management/automated-reports-with-jupyter-notebooks-using-jupytext-and-papermill-619e60c37330)
- [Kickstarting projects with cookiecutter](https://sourcery.ai/blog/python-best-practices/)
- [parsing dates with pandas](https://towardsdatascience.com/4-tricks-you-should-know-to-parse-date-columns-with-pandas-read-csv-27355bb2ad0e)
- [god bless `.format`!](https://pyformat.info/)
- [arrow function???? `def myfn() -> ??` not really, just documentation](https://medium.com/@thomas_k_r/whats-this-weird-arrow-notation-in-python-53d9e293113)
\section{References}
[1]. Pandas: https://pandas.pydata.org/pandas-docs/stable/getting_started/comparison/comparison_with_r.html#quick-reference
[2]. Raschka, S. and Mirjalili, V., 2019. Python Machine Learning. Birmingham: Packt Publishing, Limited.
[3]. Random search for hyper-parameter optimization. Bergstra J, Bengio Y. Journal of Machine Learning Research. pp. 281-305, 2012
[4]. Bias in Error Estimation When Using Cross-Validation for Model Selection, BMC Bioinformatics, S. Varma and R. Simon, 7(1): 91, 2006
[5]. https://github.com/scikit-learn-contrib/imbalanced-learn .
[6]. https://stackoverflow.com/a/6357529
[7]. https://effbot.org/zone/python-with-statement.htm