-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaux_fct.py
52 lines (44 loc) · 1.42 KB
/
aux_fct.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
### Auxillary Functions ###
import os
import numpy as np
from numpy import exp, shape, reshape, sqrt, median
from numpy.random import permutation,randn
from scipy.io import loadmat
from scipy.spatial.distance import squareform, pdist, cdist
from sklearn.metrics.pairwise import euclidean_distances
# Median Heuristic method for the RBF Kernel
def get_sigma_median_heuristic(X, is_sparse = False):
if is_sparse:
X = X.todense()
n=shape(X)[0]
if n>1000:
X=X[permutation(n)[:1000],:]
dists=squareform(pdist(X, 'euclidean'))
median_dist=median(dists[dists>0])
sigma=median_dist/sqrt(2.)
return sigma
# Median Heuristic method for the RBF Kernel
def median_sqdist(feats, n_sub=1000):
all_Xs = np.concatenate(feats)
N = all_Xs.shape[0]
sub = all_Xs[np.random.choice(N, min(n_sub, N), replace=False)]
D2 = euclidean_distances(sub, squared=True)
return np.median(D2[np.triu_indices_from(D2, k=1)], overwrite_input=True)
# Auxillary Dataset Loading
def load_data(path, random = False, seed = 29):
os.chdir(path)
mat = loadmat('MISR1.mat')
array = mat['MISR1']
x = np.zeros((800, 100, 16))
y = np.zeros((800,1))
for i in range(800):
for j in range(100):
x[i, j ,:] = array[i * 100 + j, 1:17]
y[i,:] = array[i * 100 + j, 17]
if random:
np.random.seed(seed)
permute_seq = np.random.permutation(800)
x = x[permute_seq,:,:]
y = y[permute_seq]
return x, y
return x, y