pysad.models.KNNCAD

class pysad.models.KNNCAD(probationary_period)[source]

Conformalized density- and distance-based anomaly detection in time-series data [BBI16], which uses a combination of a feature extraction method, an approach to assess a score whether a new observation differs significantly from a previously observed data, and a probabilistic interpretation of this score based on the conformal paradigm. This method’s implementation is based on NAB-kNNCAD. This model is univariate.

Parameters:

probationary_period (int) – Number of instances in probationary period. Until probationary_period instances are received, the model outputs anomaly score of 0.0.

Methods

__init__(probationary_period)

fit(X[, y])

Fits the model to all instances in order.

fit_partial(X[, y])

Fits the model to next instance.

fit_score(X[, y])

This helper method applies fit_score_partial to all instances in order.

fit_score_partial(X[, y])

Applies fit_partial and score_partial to the next instance, respectively.

score(X)

Scores all instaces via score_partial iteratively.

score_partial(X)

Scores the anomalousness of the next instance.

fit(X, y=None)

Fits the model to all instances in order.

Parameters:
  • X (np.float64 array of shape (num_instances, num_features)) – The instances in order to fit.

  • y (int) – The labels of the instances in order to fit (Optional for unsupervised models, default=None).

Returns:

Fitted model.

Return type:

object

fit_partial(X, y=None)[source]

Fits the model to next instance. Note that this model is univariate.

Parameters:
  • X (np.float64 array of shape (1,)) – The instance to fit.

  • y (int) – Ignored since the model is unsupervised (Default=None).

Returns:

Returns the self.

Return type:

object

fit_score(X, y=None)

This helper method applies fit_score_partial to all instances in order.

Parameters:
  • X (np.float64 array of shape (num_instances, num_features)) – The instances in order to fit.

  • y (np.int32 array of shape (num_instances, )) – The labels of the instances in order to fit (Optional for unsupervised models, default=None).

Returns:

The anomalousness scores of the instances in order.

Return type:

np.float64 array of shape (num_instances,)

fit_score_partial(X, y=None)

Applies fit_partial and score_partial to the next instance, respectively.

Parameters:
  • X (np.float64 array of shape (num_features,)) – The instance to fit and score.

  • y (int) – The label of the instance (Optional for unsupervised models, default=None).

Returns:

The anomalousness score of the input instance.

Return type:

float

score(X)

Scores all instaces via score_partial iteratively.

Parameters:

X (np.float64 array of shape (num_instances, num_features)) – The instances in order to score.

Returns:

The anomalousness scores of the instances in order.

Return type:

np.float64 array of shape (num_instances,)

score_partial(X)[source]

Scores the anomalousness of the next instance.

Parameters:

X (np.float64 array of shape (1,)) – The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.

Returns:

The anomalousness score of the input instance.

Return type:

float