/
hbos.py
353 lines (279 loc) · 13.4 KB
/
hbos.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
"""Histogram-based Outlier Detection (HBOS)
"""
# Author: Yue Zhao <zhaoy@cmu.edu>
# License: BSD 2 clause
from __future__ import division
from __future__ import print_function
import numpy as np
from numba import njit
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted
from .base import BaseDetector
from ..utils.utility import check_parameter
from ..utils.utility import get_optimal_n_bins
from ..utils.utility import invert_order
class HBOS(BaseDetector):
"""Histogram- based outlier detection (HBOS) is an efficient unsupervised
method. It assumes the feature independence and calculates the degree
of outlyingness by building histograms. See :cite:`goldstein2012histogram`
for details.
Two versions of HBOS are supported:
- Static number of bins: uses a static number of bins for all features.
- Automatic number of bins: every feature uses a number of bins deemed to
be optimal according to the Birge-Rozenblac method
(:cite:`birge2006many`).
Parameters
----------
n_bins : int or string, optional (default=10)
The number of bins. "auto" uses the birge-rozenblac method for
automatic selection of the optimal number of bins for each feature.
alpha : float in (0, 1), optional (default=0.1)
The regularizer for preventing overflow.
tol : float in (0, 1), optional (default=0.5)
The parameter to decide the flexibility while dealing
the samples falling outside the bins.
contamination : float in (0., 0.5), optional (default=0.1)
The amount of contamination of the data set,
i.e. the proportion of outliers in the data set. Used when fitting to
define the threshold on the decision function.
Attributes
----------
bin_edges_ : numpy array of shape (n_bins + 1, n_features )
The edges of the bins.
hist_ : numpy array of shape (n_bins, n_features)
The density of each histogram.
decision_scores_ : numpy array of shape (n_samples,)
The outlier scores of the training data.
The higher, the more abnormal. Outliers tend to have higher
scores. This value is available once the detector is fitted.
threshold_ : float
The threshold is based on ``contamination``. It is the
``n_samples * contamination`` most abnormal samples in
``decision_scores_``. The threshold is calculated for generating
binary outlier labels.
labels_ : int, either 0 or 1
The binary labels of the training data. 0 stands for inliers
and 1 for outliers/anomalies. It is generated by applying
``threshold_`` on ``decision_scores_``.
"""
def __init__(self, n_bins=10, alpha=0.1, tol=0.5, contamination=0.1):
super(HBOS, self).__init__(contamination=contamination)
self.n_bins = n_bins
self.alpha = alpha
self.tol = tol
check_parameter(alpha, 0, 1, param_name='alpha')
check_parameter(tol, 0, 1, param_name='tol')
def fit(self, X, y=None):
"""Fit detector. y is ignored in unsupervised methods.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : Ignored
Not used, present for API consistency by convention.
Returns
-------
self : object
Fitted estimator.
"""
# validate inputs X and y (optional)
X = check_array(X)
self._set_n_classes(y)
_, n_features = X.shape[0], X.shape[1]
if isinstance(self.n_bins, str) and self.n_bins.lower() == "auto":
# Uses the birge rozenblac method for automatic histogram size per feature
self.hist_ = []
self.bin_edges_ = []
# build the histograms for all dimensions
for i in range(n_features):
n_bins = get_optimal_n_bins(X[:, i])
hist, bin_edges = np.histogram(X[:, i], bins=n_bins,
density=True)
self.hist_.append(hist)
self.bin_edges_.append(bin_edges)
# the sum of (width * height) should equal to 1
assert (np.isclose(1, np.sum(
hist * np.diff(bin_edges)), atol=0.1))
outlier_scores = _calculate_outlier_scores_auto(X, self.bin_edges_,
self.hist_,
self.alpha,
self.tol)
elif check_parameter(self.n_bins, low=2, high=np.inf):
self.hist_ = np.zeros([self.n_bins, n_features])
self.bin_edges_ = np.zeros([self.n_bins + 1, n_features])
# build the histograms for all dimensions
for i in range(n_features):
self.hist_[:, i], self.bin_edges_[:, i] = \
np.histogram(X[:, i], bins=self.n_bins, density=True)
# the sum of (width * height) should equal to 1
assert (np.isclose(1, np.sum(
self.hist_[:, i] * np.diff(self.bin_edges_[:, i])),
atol=0.1))
outlier_scores = _calculate_outlier_scores(X, self.bin_edges_,
self.hist_,
self.n_bins,
self.alpha, self.tol)
# invert decision_scores_. Outliers comes with higher outlier scores
self.decision_scores_ = invert_order(np.sum(outlier_scores, axis=1))
self._process_decision_scores()
return self
def decision_function(self, X):
"""Predict raw anomaly score of X using the fitted detector.
The anomaly score of an input sample is computed based on different
detector algorithms. For consistency, outliers are assigned with
larger anomaly scores.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['hist_', 'bin_edges_'])
X = check_array(X)
if isinstance(self.n_bins, str) and self.n_bins.lower() == "auto":
outlier_scores = _calculate_outlier_scores_auto(X, self.bin_edges_,
self.hist_,
self.alpha,
self.tol)
elif check_parameter(self.n_bins, low=2, high=np.inf):
outlier_scores = _calculate_outlier_scores(X, self.bin_edges_,
self.hist_,
self.n_bins,
self.alpha, self.tol)
return invert_order(np.sum(outlier_scores, axis=1))
# @njit #due to variable size of histograms, can no longer naively use numba for jit
def _calculate_outlier_scores_auto(X, bin_edges, hist, alpha,
tol): # pragma: no cover
"""The internal function to calculate the outlier scores based on
the bins and histograms constructed with the training data. The program
is optimized through numba. It is excluded from coverage test for
eliminating the redundancy.
Parameters
----------
X : numpy array of shape (n_samples, n_features
The input samples.
bin_edges : list of length n_features containing numpy arrays
The edges of the bins.
hist : =list of length n_features containing numpy arrays
The density of each histogram.
alpha : float in (0, 1)
The regularizer for preventing overflow.
tol : float in (0, 1)
The parameter to decide the flexibility while dealing
the samples falling outside the bins.
Returns
-------
outlier_scores : numpy array of shape (n_samples, n_features)
Outlier scores on all features (dimensions).
"""
n_samples, n_features = X.shape[0], X.shape[1]
outlier_scores = np.zeros(shape=(n_samples, n_features))
for i in range(n_features):
# Find the indices of the bins to which each value belongs.
# See documentation for np.digitize since it is tricky
# >>> x = np.array([0.2, 6.4, 3.0, 1.6, -1, 100, 10])
# >>> bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
# >>> np.digitize(x, bins, right=True)
# array([1, 4, 3, 2, 0, 5, 4], dtype=int64)
bin_inds = np.digitize(X[:, i], bin_edges[i], right=True)
# Calculate the outlying scores on dimension i
# Add a regularizer for preventing overflow
out_score_i = np.log2(hist[i] + alpha)
optimal_n_bins = get_optimal_n_bins(X[:, i])
for j in range(n_samples):
# If the sample does not belong to any bins
# bin_ind == 0 (fall outside since it is too small)
if bin_inds[j] == 0:
dist = bin_edges[i][0] - X[j, i]
bin_width = bin_edges[i][1] - bin_edges[i][0]
# If it is only slightly lower than the smallest bin edge
# assign it to bin 1
if dist <= bin_width * tol:
outlier_scores[j, i] = out_score_i[0]
else:
outlier_scores[j, i] = np.min(out_score_i)
# If the sample does not belong to any bins
# bin_ind == optimal_n_bins+1 (fall outside since it is too large)
elif bin_inds[j] == optimal_n_bins + 1:
dist = X[j, i] - bin_edges[i][-1]
bin_width = bin_edges[i][-1] - bin_edges[i][-2]
# If it is only slightly larger than the largest bin edge
# assign it to the last bin
if dist <= bin_width * tol:
outlier_scores[j, i] = out_score_i[optimal_n_bins - 1]
else:
outlier_scores[j, i] = np.min(out_score_i)
else:
outlier_scores[j, i] = out_score_i[bin_inds[j] - 1]
return outlier_scores
@njit
def _calculate_outlier_scores(X, bin_edges, hist, n_bins, alpha,
tol): # pragma: no cover
"""The internal function to calculate the outlier scores based on
the bins and histograms constructed with the training data. The program
is optimized through numba. It is excluded from coverage test for
eliminating the redundancy.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
bin_edges : numpy array of shape (n_bins + 1, n_features )
The edges of the bins.
hist : numpy array of shape (n_bins, n_features)
The density of each histogram.
n_bins : int
The number of bins.
alpha : float in (0, 1)
The regularizer for preventing overflow.
tol : float in (0, 1)
The parameter to decide the flexibility while dealing
the samples falling outside the bins.
Returns
-------
outlier_scores : numpy array of shape (n_samples, n_features)
Outlier scores on all features (dimensions).
"""
n_samples, n_features = X.shape[0], X.shape[1]
outlier_scores = np.zeros(shape=(n_samples, n_features))
for i in range(n_features):
# Find the indices of the bins to which each value belongs.
# See documentation for np.digitize since it is tricky
# >>> x = np.array([0.2, 6.4, 3.0, 1.6, -1, 100, 10])
# >>> bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
# >>> np.digitize(x, bins, right=True)
# array([1, 4, 3, 2, 0, 5, 4], dtype=int64)
bin_inds = np.digitize(X[:, i], bin_edges[:, i], right=True)
# Calculate the outlying scores on dimension i
# Add a regularizer for preventing overflow
out_score_i = np.log2(hist[:, i] + alpha)
for j in range(n_samples):
# If the sample does not belong to any bins
# bin_ind == 0 (fall outside since it is too small)
if bin_inds[j] == 0:
dist = bin_edges[0, i] - X[j, i]
bin_width = bin_edges[1, i] - bin_edges[0, i]
# If it is only slightly lower than the smallest bin edge
# assign it to bin 1
if dist <= bin_width * tol:
outlier_scores[j, i] = out_score_i[0]
else:
outlier_scores[j, i] = np.min(out_score_i)
# If the sample does not belong to any bins
# bin_ind == n_bins+1 (fall outside since it is too large)
elif bin_inds[j] == n_bins + 1:
dist = X[j, i] - bin_edges[-1, i]
bin_width = bin_edges[-1, i] - bin_edges[-2, i]
# If it is only slightly larger than the largest bin edge
# assign it to the last bin
if dist <= bin_width * tol:
outlier_scores[j, i] = out_score_i[n_bins - 1]
else:
outlier_scores[j, i] = np.min(out_score_i)
else:
outlier_scores[j, i] = out_score_i[bin_inds[j] - 1]
return outlier_scores