Skip to main content
Version: 0.5.x

BNSA

Binary Negative Selection Algorithm (BNSA).

Inheritance

This class extends BaseClassifier

Module: aisp.nsa
Import: from aisp.nsa import BNSA


Overview

Algorithm for classification and anomaly detection Based on self or not self discrimination, inspired by Negative Selection Algorithm.

note

The Binary Negative Selection Algorithm (BNSA) is based on the original proposal by Forrest et al. (1994) 1, originally developed for computer security. In the adaptation, the algorithm use bits arrays, and it has support for multiclass classification.

warning

High aff_thresh values may prevent the generation of valid non-self detectors


Example

from aisp.nsa import BNSA
# Binary 'self' samples
x_train = [
[0, 0, 1, 0, 1],
[0, 1, 1, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 1, 1, 0, 1],
[0, 1, 0, 1, 0]
]
y_train = ['self', 'self', 'self', 'self', 'self', 'self']
bnsa = BNSA(aff_thresh=0.55, seed=1)
bnsa = bnsa.fit(x_train, y_train, verbose=False)
# samples for testing
x_test = [
... [1, 1, 1, 1, 1], # Sample of Anomaly
... [0, 1, 0, 1, 0], # self sample
... ]
y_prev = bnsa.predict(X=x_test)
print(y_prev)

Output

['non-self' 'self']

Constructor Parameters

NameTypeDefaultDescription
Nint100Number of detectors.
aff_threshfloat0.1The variable represents the percentage of similarity between the T cell and the own samples. The default value is 10% (0.1), while a value of 1.0 represents 100% similarity.
max_discardsint1000This parameter indicates the maximum number of detector discards in sequence, which aims to avoid a possible infinite loop if a radius is defined that it is not possible to generate non-self detectors.
seedOptional[int]NoneSeed for the random generation of values in the detectors.
no_label_sample_selectionstr'max_average_difference'Method for selecting labels for samples designated as non-self by all detectors.

Attributes

NameTypeDefaultDescription
detectorsOptional[Dict[str | int, npt.NDArray[np.bool_]]]-The trained detectors, organized by class.

Public Methods

fit

def fit(
self,
X: Union[npt.NDArray, list],
y: Union[npt.NDArray, list],
verbose: bool = True,
) -> BNSA:
...

Training according to X and y, using the method negative selection method.

Parameters

NameTypeDefaultDescription
XUnion[npt.NDArray, list]-Training input samples. Each row corresponds to a samples and column to feature.
yUnion[npt.NDArray, list]-Target vector of shape (n_samples,). Must contain the same number of samples as X.
verboseboolTrueIf True, prints training progress information.

Returns

TypeDescription
SelfReturns the instance itself.

Raises

ExceptionDescription
TypeErrorIf X or y are not ndarrays or have incompatible shapes.
ValueErrorIf the array X contains any values other than (0 and 1) or (True and False).
MaxDiscardsReachedErrorIf the maximum number of detector discards was reached during maturation. Check the defined radius value and consider reducing it.

predict

def predict(self, X: Union[npt.NDArray, list]) -> npt.NDArray:
...

Prediction of classes based on detectors created after training.

Parameters

NameTypeDefaultDescription
XUnion[npt.NDArray, list]-Input samples. Must have the same number of features used during training.

Returns

TypeDescription
npt.NDArrayAn ndarray of the form C (n_samples), containing the predicted classes for X.

Raises

ExceptionDescription
TypeErrorIf X is not a ndarray or list.
ValueErrorIf the array contains values other than 0 and 1.
FeatureDimensionMismatchIf the number of features (columns) in X does not match the expected number.
ModelNotFittedErrorIf the mode has not yet been adjusted and does not have defined detectors or classes, it is not able to predictions

Extended Example

Complete usage examples are available in the Jupyter Notebooks:


References

Footnotes

  1. S. Forrest, A. S. Perelson, L. Allen and R. Cherukuri, "Self-nonself discrimination in a computer," Proceedings of 1994 IEEE Computer Society Symposium on Research in Security and Privacy, Oakland, CA, USA, 1994, pp. 202-212, doi: https://dx.doi.org/10.1109/RISP.1994.296580.