-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing.py
43 lines (32 loc) · 1.08 KB
/
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
37
38
39
40
41
42
43
import warnings
import numpy as np
from sklearn.metrics import pairwise_kernels, pairwise_distances
warnings.filterwarnings('ignore', category=DeprecationWarning)
warnings.filterwarnings('ignore', category=RuntimeWarning)
def width(Z):
# Computes the median heuristic for the kernel bandwidth
dist_mat = pairwise_distances(Z, metric='euclidean')
width_Z = np.median(dist_mat[dist_mat > 0])
return width_Z
def compute_kernel_n(X):
# data preparation for individual variable matrices
"""
Args:
X: data matrix of shape (n, T)
"""
return pairwise_kernels(X, metric='rbf', gamma=0.5 / (width(X) ** 2))
def centering_kernel(k):
# centering a kernel matrix
"""
Args:
k: kernel matrix of shape (n, n)
"""
n = k.shape[0]
H = np.eye(n) - 1 / n * np.ones(n)
k_centered = H @ k @ H
return k_centered
def centering_kernel_mats(K):
k_list = [K[i] for i in range(K.shape[0])]
return np.stack(list(map(centering_kernel, k_list)))
def compute_kernel_mats(X_list):
return np.stack(list(map(compute_kernel_n, X_list)))