-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathfeature_scaling.py
More file actions
70 lines (39 loc) · 1.09 KB
/
feature_scaling.py
File metadata and controls
70 lines (39 loc) · 1.09 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
# coding: utf-8
"""
Created on Mon May 17 00:00:00 2017
@author: DIP
"""
# # Import necessary dependencies and settings
# In[1]:
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
import numpy as np
import pandas as pd
np.set_printoptions(suppress=True)
# # Load sample data of video views
# In[2]:
views = pd.DataFrame([1295., 25., 19000., 5., 1., 300.], columns=['views'])
views
# # Standard Scaler $\frac{x_i - \mu}{\sigma}$
# In[3]:
ss = StandardScaler()
views['zscore'] = ss.fit_transform(views[['views']])
views
# In[4]:
vw = np.array(views['views'])
(vw[0] - np.mean(vw)) / np.std(vw)
# # Min-Max Scaler $\frac{x_i - min(x)}{max(x) - min(x)}$
# In[5]:
mms = MinMaxScaler()
views['minmax'] = mms.fit_transform(views[['views']])
views
# In[6]:
(vw[0] - np.min(vw)) / (np.max(vw) - np.min(vw))
# # Robust Scaler $\frac{x_i - median(x)}{IQR_{(1,3)}(x)}$
# In[7]:
rs = RobustScaler()
views['robust'] = rs.fit_transform(views[['views']])
views
# In[8]:
quartiles = np.percentile(vw, (25., 75.))
iqr = quartiles[1] - quartiles[0]
(vw[0] - np.median(vw)) / iqr