-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkernel_function.py
executable file
·139 lines (102 loc) · 3.2 KB
/
kernel_function.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
from scipy.spatial.distance import euclidean
from scipy.sparse import coo_matrix
from sklearn.metrics.pairwise import rbf_kernel
from tqdm import trange
from sklearn.feature_extraction.text import TfidfTransformer
from scipy.sparse import lil_matrix
import traceback
_divider_ = [':', '_', '/', '\\', '-->', '->']
def is_pairwise(kernel):
try:
X = lil_matrix(np.ones((2, 1)))
kernel(X, X)
return True
except Exception:
return False
def is_absolute(kernel):
try:
X = lil_matrix(np.ones((2, 2)))
kernel(X)
return True
except Exception as e:
return False
def split_string(S):
divider = None
for d in _divider_:
if d in S:
divider = d
break
if divider is None:
return [S]
return S.split(divider)
def make_chain(preprocessors, kernel):
def chain(X):
for P in preprocessors:
X = P(X)
return kernel(X)
return chain
def select_kernel(kernel_type):
if kernel_type == 'euclidean':
return euclidean
elif kernel_type == 'jaccard':
return jaccard_kernel
elif kernel_type == 'rbf':
return rbf_kernel
elif kernel_type == 'linear':
return linear_kernel
else:
raise ValueError('Unknown kernel %s' % kernel_type)
def select_preprocessor(preprocessor_type):
if preprocessor_type == 'tfidf':
return tfidf_preprocessor
elif preprocessor_type == 'cov':
return cov_preprocessor
else:
raise ValueError('Unknown preprocessor %s' % preprocessor_type)
def select_full(full_type):
types = split_string(full_type)
kernel = select_kernel(types[-1])
if is_pairwise(kernel) and len(types) > 1:
raise ValueError('Preprocessor are not appliable with pairwise kernels')
preprocessors = []
while len(types) > 1:
act = types[0]
types = types[1:]
preprocessors.append(select_preprocessor(act))
return make_chain(preprocessors, kernel)
def linear_kernel(X):
return X.dot(X.transpose())
def _jaccard_between(X, Y):
min_sum = X.minimum(Y).sum(axis=1, dtype=np.float64)
max_sum = X.maximum(Y).sum(axis=1, dtype=np.float64)
return min_sum / max_sum
def jaccard_kernel(X):
row = list(range(X.shape[0]))
columns = list(range(X.shape[0]))
data = [1] * X.shape[0]
for i in trange(1, X.shape[0]):
X_index = np.arange(i, X.shape[0])
Y_index = np.arange(0, X.shape[0] - i)
X_slice = X[X_index, :]
Y_slice = X[Y_index, :]
S = _jaccard_between(X_slice, Y_slice)
S = list(S.flat)
row.extend(X_index.data)
columns.extend(Y_index.data)
data.extend(S)
row.extend(Y_index.data)
columns.extend(X_index.data)
data.extend(S)
return coo_matrix((data, (row, columns))).toarray()
def tfidf_preprocessor(X):
return TfidfTransformer().fit_transform(X)
def cov_preprocessor(X):
X = X.tocsc()
feat = X.get_shape()[1]
cov = X.getnnz(axis=0) / X.get_shape()[1]
threshold = 0.001
condition = np.where(cov >= threshold)
X = X[:, condition[0]]
print('%d / %d' % (X.get_shape()[1], feat))
return X.tocsr()