skmultiflow.anomaly_detection.
HalfSpaceTrees
Half–Space Trees.
Implementation of the Streaming Half–Space–Trees (HS–Trees) [1], a fast one-class anomaly detector for evolving data streams. It requires only normal data for training and works well when anomalous data are rare. The model features an ensemble of random HS–Trees, and the tree structure is constructed without any data. This makes the method highly efficient because it requires no model restructuring when adapting to evolving data streams.
Number of trees in the ensemble. ‘t’ in the original paper.
The window size of the stream. ‘Psi’ in the original paper.
The maximum depth of the trees in the ensemble. ‘maxDepth’ in the original paper.
The minimum mass required in a node (as a fraction of the window size) to calculate the anomaly score. ‘sizeLimit’ in the original paper. A good setting is 0.1 * window_size
0.1 * window_size
The threshold for declaring anomalies. Any instance prediction probability above this threshold will be declared as an anomaly.
If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
References
S.C.Tan, K.M.Ting, and T.F.Liu, “Fast anomaly detection for streaming data,” in IJCAI Proceedings - International Joint Conference on Artificial Intelligence, 2011, vol. 22, no. 1, pp. 1511–1516.
Examples
>>> # Imports >>> from skmultiflow.data import AnomalySineGenerator >>> from skmultiflow.anomaly_detection import HalfSpaceTrees >>> # Setup a data stream >>> stream = AnomalySineGenerator(random_state=1, n_samples=1000, n_anomalies=250) >>> # Setup Half-Space Trees estimator >>> half_space_trees = HalfSpaceTrees(random_state=1) >>> # Setup variables to control loop and track performance >>> max_samples = 1000 >>> n_samples = 0 >>> true_positives = 0 >>> detected_anomalies = 0 >>> # Train the estimator(s) with the samples provided by the data stream >>> while n_samples < max_samples and stream.has_more_samples(): >>> X, y = stream.next_sample() >>> y_pred = half_space_trees.predict(X) >>> if y[0] == 1: >>> true_positives += 1 >>> if y_pred[0] == 1: >>> detected_anomalies += 1 >>> half_space_trees.partial_fit(X, y) >>> n_samples += 1 >>> print('{} samples analyzed.'.format(n_samples)) >>> print('Half-Space Trees correctly detected {} out of {} anomalies'. >>> format(detected_anomalies, true_positives))
Methods
build_trees(self)
build_trees
Initialises ensemble.
fit(self, X, y[, classes, sample_weight])
fit
Fit the model.
get_info(self)
get_info
Collects and returns the information about the configuration of the estimator
get_params(self[, deep])
get_params
Get parameters for this estimator.
initialise_work_space(self)
initialise_work_space
Initialises work spaces.
partial_fit(self, X[, y, classes, sample_weight])
partial_fit
Partially (incrementally) fit the model.
predict(self, X)
predict
Predict classes for the passed data.
predict_proba(self, X)
predict_proba
Estimate the probability of a sample being normal or abnormal.
reset(self)
reset
Resets the estimator to its initial state.
score(self, X, y[, sample_weight])
score
Returns the mean accuracy on the given test data and labels.
set_is_learning_phase_on(self, boolean)
set_is_learning_phase_on
Sets learning phase in each tree defined in the ensemble.
set_params(self, **params)
set_params
Set the parameters of this estimator.
update_mass(self, X, boolean)
update_mass
Populates mass profiles for every tree defined in the ensemble.
update_models(self)
update_models
Updates the mass profile of every tree in the ensemble.
The features to train the model.
An array-like with the class labels of all samples in X.
Contains all possible/known class labels. Usage varies depending on the learning method.
Samples weight. If not provided, uniform weights are assumed. Usage varies depending on the learning method.
Configuration of the estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
For every dimension in the feature space, creates a minimum and a maximum work range.
Kept in the signature for compatibility with parent class.
Not used by this method.
The set of data samples to predict the class labels for.
Class probabilities are calculated as the mean predicted class probabilities per base estimator.
Samples for which we want to predict the class probabilities.
Predicted class probabilities for all instances in X. Class probabilities for a sample shall sum to 1 as long as at least one estimators has non-zero predictions. If no estimator can predict probabilities, probabilities of 0 are returned.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
<component>__<parameter>
Instance attributes.
True to update reference mass, False to update latest mass