Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
62 lines (45 loc) · 1.69 KB
/
Copy pathutils.py
File metadata and controls
62 lines (45 loc) · 1.69 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
import numpy as np
def rx(x: np.ndarray):
rows, cols, bands = x.shape
x_2d = x.reshape((rows * cols, bands))
inv_Sig = np.linalg.inv(np.cov(x_2d.T))
mu = np.mean(x_2d, axis=0, keepdims=True)
get_M_dis = lambda _x: (_x - mu) @ inv_Sig @ (_x - mu).T
dm = np.array([get_M_dis(_x) for _x in x_2d])
dm = dm.reshape((rows, cols))
return dm
class MinMaxNorm:
def __init__(self, feature_range=(0, 1)):
self.feature_range = feature_range
self.min = None
self.max = None
def fit(self, x):
self.min = x.min()
self.max = x.max()
return self
def transform(self, x):
x_std = (x - self.min) / (self.max - self.min)
x_norm = x_std * (self.feature_range[1] - self.feature_range[0]) + self.feature_range[0]
return x_norm
def inverse_transform(self, x_norm):
x_std = (x_norm - self.feature_range[0]) / (self.feature_range[1] - self.feature_range[0])
x = x_std * (self.max - self.min) + self.min
return x
class ZScoreNorm:
def __init__(self):
self.means = None
self.stds = None
def fit(self, x):
self.means = np.mean(x, axis=(0, 1))
self.stds = np.std(x, axis=(0, 1))
return self
def transform(self, x):
x_norm = np.zeros_like(x)
for i in range(x.shape[2]):
x_norm[:, :, i] = (x[:, :, i] - self.means[i]) / self.stds[i]
return x_norm
def inverse_transform(self, x_norm):
x = np.zeros_like(x_norm)
for i in range(x_norm.shape[2]):
x[:, :, i] = x_norm[:, :, i] * self.stds[i] + self.means[i]
return x