-
Notifications
You must be signed in to change notification settings - Fork 2
/
Scaler.py
70 lines (59 loc) · 2.04 KB
/
Scaler.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
# ============================================================
# ================== Abstract Scaler =========================
# ============================================================
from abc import ABC, abstractmethod
import numpy as np
class Scaler(ABC):
"""
Abstract class that definies a scaler : a function that
'normalises' the data according to some function.
"""
@abstractmethod
def fit(self,x):
"""
Function that computes the paramters of the normalisa-
tion process and stores them into the instance of the
object.
It is then supposed to call the method : transform and
return the result.
@params x : (numpy array)
@return x_normalised : after after applying the norma-
lisation process
"""
pass
@abstractmethod
def transform(self,x):
"""
Performs the actual normalisation with the help of the
parameters defined by the previous call to : fit
/!\ fit should be called before transform /!\
@params x : (numpy array)
@return x_normalised : after after applying the norma-
lisation process
"""
pass
# ============================================================
# =================== Sub Classes ===========================
# ============================================================
class StandardScaler(Scaler):
"""
Standard Scaler that returns a matrix with 0-mean columns
and 1-std columns.
"""
def fit(self,x):
self.mean = np.mean(x,axis=0)
self.std = np.std(x,axis=0)
return self.transform(x)
def transform(self, x):
return (x-self.mean)/self.std
class MinMaxScaler(Scaler):
"""
Standard Scaler that returns a matrix scaler according to
the formula : (x-min)/(max-min)
"""
def fit(self,x):
self.min = np.min(x,axis=0)
self.max = np.max(x,axis=0)
return self.transform(x)
def transform(self, x):
return (x-self.min)/(self.max-self.min)