forked from daviddaiweizhang/istar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce_dim.py
44 lines (37 loc) · 1.22 KB
/
reduce_dim.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
import numpy as np
from sklearn.decomposition import PCA
from umap import UMAP
def reduce_dim(
x, n_components, method='pca',
pre_normalize=False, post_normalize=False):
if n_components >= 1:
n_components = int(n_components)
isfin = np.isfinite(x).all(-1)
if pre_normalize:
x -= x[isfin].mean(0)
x /= x[isfin].std(0)
if method == 'pca':
model = PCA(n_components=n_components)
elif method == 'umap':
model = UMAP(
n_components=n_components, n_neighbors=20, min_dist=0.0,
n_jobs=-1, random_state=0, verbose=True)
else:
raise ValueError(f'Method `{method}` not recognized')
print(x[isfin].shape)
u = model.fit_transform(x[isfin])
print('n_components:', u.shape[-1], '/', x.shape[-1])
if method == 'pca':
print('pve:', model.explained_variance_ratio_.sum())
# order components by variance
order = np.nanvar(u, axis=0).argsort()[::-1]
u = u[:, order]
# make all components have variance == 1
if post_normalize:
u -= u.mean(0)
u /= u.std(0)
z = np.full(
isfin.shape + (u.shape[-1],),
np.nan, dtype=np.float32)
z[isfin] = u
return z, model