-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparse_dfrocc.py
267 lines (217 loc) · 8 KB
/
sparse_dfrocc.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from operator import pos
from typing import Type
import numpy as np
from numpy.core.fromnumeric import shape
from sklearn.base import BaseEstimator, OutlierMixin
from sklearn.preprocessing import MinMaxScaler
from time import time
import scipy
class SDFROCC(BaseEstimator, OutlierMixin):
"""FROCC classifier
Parameters
----------
num_clf_dim : int, optional
number of random classification directions, by default 10
epsilon : float, optional
sepratation margin as a fraction of range, by default 0.1
threshold : float, optional
agreement threshold, by default 1
bin_factor : int, optional
discretization parameter, by default 2
kernel : callable, optional
kernel function, by default dot
precision : type, optional
floating point precision to use, by default np.float16
Examples
---------
>>> import frocc, datasets
>>> x, y, _, _ = datasets.gaussian()
>>> clf = FROCC()
>>> clf.fit(x)
>>> preds = clf.predict(x)
"""
def __init__(
self,
num_clf_dim: int = 10,
epsilon: float = 0.1,
threshold: float = 1,
bin_factor: int = 2,
kernel: Type[np.dot] = lambda x, y: x.dot(y.T),
precision: type = np.float32,
):
self.num_clf_dim = num_clf_dim
self.precision = precision
self.epsilon = epsilon
self.threshold = threshold
self.kernel = kernel
self.clf_dirs = None
self.bin_factor = bin_factor
self.num_bins = int(bin_factor / epsilon)
# Last entry is a dummy, hence + 2
self.right_intervals = np.zeros(
(self.num_clf_dim, self.num_bins + 2), dtype=np.ubyte
)
self.left_intervals = np.zeros(
(self.num_clf_dim, self.num_bins + 2), dtype=np.ubyte
)
self.scalars = []
def _achlioptas_dist(self, shape, density):
s = 1 / density
n_components = shape[0]
v = np.array([-1, 0, 1])
p = [1 / (2 * s), 1 - 1 / s, 1 / (2 * s)]
rv = scipy.stats.rv_discrete(values=(v, p))
return (np.sqrt(s) / np.sqrt(n_components)) * rv.rvs(size=shape)
def get_intervals(self, projections):
"""Compute epsilon separated interval matrix from projection
Parameters
----------
projection : 1-d array
Projection array of points on a vector
Returns
-------
Interval Matrix : 2-d array
Matrix denoting filled intervals
"""
bin_ids = (projections * self.num_bins).astype(np.int)
I = np.arange(self.num_clf_dim)
for k in range(self.bin_factor):
B = bin_ids[:, I] + k
B[B >= self.num_bins + 1] = self.num_bins + 1 # store in the dummy entry
self.right_intervals[I, B] = np.maximum(
self.right_intervals[I, B], self.bin_factor - k
)
B = bin_ids[:, I] - k
B[B <= 0] = self.num_bins + 1 # store in the dummy entry
self.left_intervals[I, B] = np.maximum(
self.left_intervals[I, B], self.bin_factor - k
)
def get_scalars(self, projections):
min_mat = np.amin(projections, axis=0).reshape(1, -1)
max_mat = np.amax(projections, axis=0).reshape(1, -1)
return min_mat, max_mat
def scale(self, projections, min_mat, max_mat):
projections = (projections - min_mat) / (max_mat - min_mat)
return projections
def fit(self, x, y=None):
"""Train FROCC
Parameters
----------
x : ndarray
Training points
y : 1d-array, optional
For compatibility, by default None
Returns
-------
self
Fitted classifier
"""
# x = self.precision(x)
self.feature_len = x.shape[1]
self.clf_dirs = scipy.sparse.csc_matrix((self.num_clf_dim, self.feature_len))
# self.clf_dirs = np.zeros((self.num_clf_dim, self.feature_len))
# clf_dirs = np.random.standard_normal(size=(self.num_clf_dim, self.feature_len))
non_zero_dims = np.where(x.getnnz(axis=0) != 0)[0]
n_non_zero = non_zero_dims.shape[0]
t = np.random.standard_normal(size=(self.num_clf_dim, n_non_zero))
self.clf_dirs[:, non_zero_dims] = t
projections = self.kernel(
x, self.clf_dirs, dense_output=True
) # shape should be NxD
min_mat, max_mat = self.get_scalars(projections)
self.min_mat, self.max_mat = min_mat, max_mat
projections = self.scale(projections, min_mat, max_mat)
self.get_intervals(projections)
self.is_fitted_ = True
return self
def clip(self, projections):
"""
Clip projections to 0-1 range for the test-set
"""
projections[projections < 0] = 0
projections[projections > 1] = 1
return projections
def decision_function(self, x):
"""Returns agreement fraction for points in a test set
Parameters
----------
x : ndarray
Test set
Returns
-------
1d-array - float
Agreement fraction of points in x
"""
# x = self.precision(x)
non_zero_dims = np.where(x.getnnz(axis=0) != 0)[0]
non_zero_dims = non_zero_dims[
np.where(self.clf_dirs[:, non_zero_dims].getnnz(axis=0) == 0)[0]
]
# non_zero_dims = non_zero_dims[np.where(np.count_nonzero(self.clf_dirs[:, non_zero_dims], axis=0)==0) [0]]
n_non_zero = non_zero_dims.shape[0]
self.clf_dirs[:, non_zero_dims] = np.random.standard_normal(
size=(self.num_clf_dim, n_non_zero)
)
projections = self.kernel(x, self.clf_dirs)
projections = self.scale(projections, self.min_mat, self.max_mat)
# Mask to compensate for out-of-range projections
# Observe that the bins corresponding to the projections 0 and 1 are always occupied
# because they correspond to the min and max values actually observed
# Therefore, we can safely remove all occurrences of <0 and >1 projections
mask = np.logical_or(projections < 0, projections > 1)
projections = self.clip(projections)
bin_ids = (projections * self.num_bins).astype(np.int)
scores = np.zeros((x.shape[0],))
I = np.arange(self.num_clf_dim)
scores = (
np.sum(
(
self.left_intervals[I, bin_ids[:, I]]
+ self.right_intervals[I, bin_ids[:, I]]
>= self.bin_factor
).astype(np.int)
- mask[:, I],
axis=1,
)
/ self.num_clf_dim
)
return scores
def predict(self, x):
"""Predictions of FROCC on test set x
Parameters
----------
x : ndarray
Test set
Returns
-------
1d-array - bool
Prediction on Test set. False means outlier.
"""
scores = self.decision_function(x)
return scores >= self.threshold
def fit_predict(self, x, y=None):
"""Perform fit on x and returns labels for x.
Parameters
----------
x : ndarray
Input data.
y : ignored, optional
Not used, present for API consistency by convention.
Returns
-------
1-d array - bool
Predition on x. False means outlier.
"""
return super().fit_predict(x, y=y)
def size(self):
"""Returns storage size required for classifier
Returns
-------
int
Total size to store random vectors and intervals
"""
clf_dir_size = self.clf_dirs.nbytes
bitmap_size = (self.num_clf_dim * 2 / self.epsilon) / 8 # in bytes
return clf_dir_size + bitmap_size
def __sizeof__(self):
return self.size()