Skip to main content
Version: 0.5.x

RNSA

Real-Valued Negative Selection Algorithm (RNSA).

Inheritance

This class extends BaseClassifier

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


Overview

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

note

This algorithm has two different versions: one based on the canonical version 1 and another with variable radius detectors 2. Both are adapted to work with multiple classes and have methods for predicting data present in the non-self region of all detectors and classes.

warning

The parameters r and r_s can prevent the generation of valid detectors. A very small r value can limit coverage, while a very high one can hinder the generation of valid detectors. Similarly, a high r_s can restrict detector creation. Thus, proper adjustment of r and r_s is essential to ensure good model performance.


Example

import numpy as np
from aisp.nsa import RNSA

np.random.seed(1)
class_a = np.random.uniform(high=0.5, size=(50, 2))
class_b = np.random.uniform(low=0.51, size=(50, 2))

Example 1: Multiclass classification (RNSA supports two or more classes)

x_train = np.vstack((class_a, class_b))
y_train = ['a'] * 50 + ['b'] * 50
rnsa = RNSA(N=150, r=0.3, seed=1)
rnsa = rnsa.fit(x_train, y_train, verbose=False)
x_test = [
[0.15, 0.45], # Expected: Class 'a'
[0.85, 0.65], # Expected: Class 'b'
]
y_pred = rnsa.predict(x_test)
print(y_pred)

Output

['a' 'b']

Example 2: Anomaly Detection (self/non-self)

rnsa = RNSA(N=150, r=0.3, seed=1)
rnsa = rnsa.fit(X=class_a, y=np.array(['self'] * 50), verbose=False)
y_pred = rnsa.predict(class_b[:5])
print(y_pred)

Output

['non-self' 'non-self' 'non-self' 'non-self' 'non-self']

Constructor Parameters

NameTypeDefaultDescription
Nint100Number of detectors.
rfloat0.05Radius of the detector.
r_sfloat0.0001rₛ Radius of the X own samples.
kint1Number of neighbors near the randomly generated detectors to perform the distance average calculation.
metric{"euclidean", "minkowski", "manhattan"}'euclidean'Distance metric used to compute the distance between the detector and the sample.
max_discardsint1000This parameter indicates the maximum number of consecutive detector discards, aimed at preventing a possible infinite loop in case a radius is defined that cannot generate non-self detectors.
seedOptional[int]NoneSeed for the random generation of values in the detectors.
algorithm{"default-NSA", "V-detector"}'default-NSA'Set the algorithm version
non_self_labelstr'non-self'This variable stores the label that will be assigned when the data has only one output class, and the sample is classified as not belonging to that class.
cell_boundsboolFalseIf set to True, this option limits the generation of detectors to the space within the plane between 0 and 1. This means that any detector whose radius exceeds this limit is discarded, this variable is only used in the V-detector algorithm.
pfloat2.0This parameter stores the value of p used in the Minkowski distance.

Attributes

NameTypeDefaultDescription
detectorsOptional[Dict[str | int, list[Detector]]]-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:
...

Perform training according to X and y, using the negative selection method (NegativeSelect).

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 fall outside the interval (0.0, 1.0).
MaxDiscardsReachedErrorThe 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 X fall outside the interval (0.0, 1.0).
FeatureDimensionMismatchIf the number of features 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. BRABAZON, Anthony; O'NEILL, Michael; MCGARRAGHY, Seán. Natural Computing Algorithms. [S. l.]: Springer Berlin Heidelberg, 2015. DOI 10.1007/978-3-662-43631-8. Disponível em: https://dx.doi.org/10.1007/978-3-662-43631-8.

  2. Ji, Z.; Dasgupta, D. (2004). Real-Valued Negative Selection Algorithm with Variable-Sized Detectors. In Lecture Notes in Computer Science, vol. 3025. https://doi.org/10.1007/978-3-540-24854-5_30