-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
36 lines (26 loc) · 969 Bytes
/
preprocessing.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
"""
Data preprocessing tools.
"""
import numpy as np
class Scaler:
"""
Class for scaling data by standardisation. Includes methods for inverting
the scaling of data and related probability densities, means and
covariances.
"""
def __init__(self, X):
assert len(X.shape) > 1, "X must have dimension greater than 1."
self.mean = X.mean(axis=0)
self.std = X.std(axis=0)
def standardise(self, X):
return (X - self.mean) / self.std
def invert_standardisation(self, X):
return (X * self.std) + self.mean
def invert_standardisation_prob(self, prob):
return prob / self.std.prod()
def invert_standardisation_log_prob(self, prob):
return prob - np.log(self.std.prod())
def invert_standardisation_loc(self, loc):
return self.invert_standardisation(loc)
def invert_standardisation_cov(self, cov):
return cov * (self.std[:, None] @ self.std[None, :])