This repository has been archived by the owner on Oct 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorms.py
76 lines (61 loc) · 2.3 KB
/
norms.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
71
72
73
74
75
76
import numpy as np
from sklearn.metrics import euclidean_distances
from sklearn.base import BaseEstimator, TransformerMixin
from convert import convert
class OrigN(BaseEstimator, TransformerMixin):
def __init__(self):
self.func = 0
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X_new = X['data'].copy()
return X_new
class OrigS(BaseEstimator, TransformerMixin):
def __init__(self):
self.func = 0
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X_new = X
return X_new
class SpectralNorm(BaseEstimator, TransformerMixin):
def __init__(self, func=0):
self.func = func
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X_new = X.copy()
[np.fill_diagonal(X_new[i], 0) for i in range(X_new.shape[0])]
degrees = np.array([np.diag(1 / np.sqrt(np.nansum(X_new[i], axis=1))) for i in range(X_new.shape[0])])
normed_X = np.array([degrees[i].dot(X_new[i]).dot(degrees[i]) for i in range(X_new.shape[0])])
return normed_X
class BinarNorm(BaseEstimator, TransformerMixin):
def __init__(self, func=0):
self.func = func
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
bin_X = X['data'].copy()
bin_X[bin_X > 0] = 1
return bin_X
class WbysqDist(BaseEstimator, TransformerMixin):
def __init__(self, func=0):
self.func = func
def fit(self, X, y=None):
return self
def distance(self, d):
if len(d.shape) == 2:
dist = euclidean_distances(d)
np.fill_diagonal(dist, 1)
else:
dist = np.array([euclidean_distances(d[i]) for i in range(d.shape[0])])
[np.fill_diagonal(dist[i],1) for i in range(d.shape[0])]
return dist
def transform(self, X, y=None):
dist = self.distance(X['dist'])
if len(dist.shape) == 2:
weighted_X = np.array([X['data'][i] / dist ** 2 for i in range(X['data'].shape[0])])
else:
weighted_X = np.array([X['data'][i] / dist[i] ** 2 for i in range(X['data'].shape[0])])
[np.fill_diagonal(weighted_X[i], 0) for i in range(X['data'].shape[0])]
return weighted_X