Skip to content
João Paulo edited this page Oct 25, 2025 · 1 revision

English

AIRS (Artificial Immune Recognition System)

AIRS Constructor:

The AIRS class aims to perform classification using metaphors of selection and clonal expansion.

This implementation is inspired by AIRS2, a simplified version of the original AIRS algorithm. Introducing adaptations to handle continuous and binary datasets.

Based on Algorithm 16.5 from Brabazon et al. 1.

Related and noteworthy works: access here 2.

Attributes:

  • n_resources (float): Total amount of available resources. Defaults to 10.

  • rate_clonal (float): Maximum number of possible clones of a class. This quantity is multiplied by (cell stimulus * rate_hypermutation) to define the number of clones. Defaults to 10.

  • rate_hypermutation (int): The rate of mutated clones derived from rate_clonal as a scalar factor. Defaults to 0.75.

  • affinity_threshold_scalar (float): Normalized affinity threshold. Defaults to 0.75.

  • k (int): The number of K nearest neighbors that will be used to choose a label in the prediction. Defaults to 10.

  • max_iters (int): Maximum number of interactions in the refinement process of the ARB set exposed to aᵢ. Defaults to 100.

  • resource_amplified (float): Resource consumption amplifier is multiplied with the incentive to subtract resources. Defaults to 1.0 without amplification.

  • metric (Literal["manhattan", "minkowski", "euclidean"]): Way to calculate the distance between the detector and the sample:

    • 'Euclidean' ➜ The calculation of the distance is given by the expression:
      √( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²).
    • 'minkowski' ➜ The calculation of the distance is given by the expression:
      ( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ.
    • 'manhattan' ➜ The calculation of the distance is given by the expression:
      ( |x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|).
      Defaults to "Euclidean".
  • seed (int): Seed for the random generation of detector values. Defaults to None.

  • **kwargs:
    • p (float): This parameter stores the value of p used in the Minkowski distance. The default is 2, which means normalized Euclidean distance. Different values of p lead to different variants of the Minkowski distance. Learn more.

Other initialized variables:

  • cells_memory (dict): This variable stores a list of memory cells by class.
  • affinity_threshold (dict): Defines the affinity threshold between antigens.
  • classes (npt.NDArray): List of output classes.

Public Methods

Function fit(...)

The fit(...) function generates detectors for the non-owners relative to the samples:

def fit(self, X: npt.NDArray, y: npt.NDArray):

It performs the training according to X and y, using the method Artificial Immune Recognition System (AIRS).

Input parameters:

  • X: Array with sample features, with N samples (rows) and N features (columns), normalized to values between [0, 1].
  • y: Array with output classes corresponding to N samples related to X.
  • verbose: Boolean, default True, determines if the feedback from the detector generation will be printed.

Returns the class instance.


Function predict(...)

The predict(...) function performs class prediction using the generated detectors:

def predict(self, X: npt.NDArray) -> npt.NDArray:

Input parameter:

  • X: Array with the features for prediction, with N samples (rows) and N columns.

Returns:

  • C: An array of predictions with the output classes for the given features.
  • None: If there are no detectors.

Function score(...):

The score(...) function calculates the accuracy of the trained model by making predictions and calculating the accuracy.

def score(self, X: npt.NDArray, y: list) -> float:

Returns accuracy as a float.


Private Methods

Function _refinement_arb(...):

The function "_refinement_arb(...)" refines the ARB set until the average stimulation value exceeds the defined threshold (affinity_threshold_scalar).

Parameters:

  • c_match (Cell): Cell with the highest stimulation relative to aᵢ.
  • arb_list (List[_ARB]): ARB set.
def _refinement_arb(self, ai: npt.NDArray, c_match: Cell, arb_list: List[_ARB]) -> _ARB:

Returns the cell (_ARB) with the highest ARB stimulation.


Function _cells_affinity_threshold(...):

The function "_cells_affinity_threshold(...)" calculates the affinity threshold based on the average affinity between training instances, where aᵢ and aⱼ are a pair of antigens, and affinity is measured by distance (Euclidean, Manhattan, Minkowski, Hamming).
Following the formula:

$$ \text{affinity}_{\text{threshold}} = \frac{ \sum_{i=1}^{n-1} \sum_{j=i+1}^{n} \text{affinity}(a_i, a_j)}{n(n-1)/2} $$

Parameters:

  • antigens_list (NDArray): List of training antigens.
def _cells_affinity_threshold(self, antigens_list: npt.NDArray):

Function _affinity(...):

The function "_affinity(...)" calculates the stimulus between two vectors using metrics.

Parameters:

  • u (npt.NDArray): Coordinates of the first point.
  • v (npt.NDArray): Coordinates of the second point.
def _affinity(self, u: npt.NDArray, v: npt.NDArray) -> float:

Returns the stimulus rate between the vectors.


Function _init_memory_c(...):

The function "_init_memory_c(...)" initializes memory cells by randomly selecting n_antigens_selected from the list of training antigens.

Parameters:

  • antigens_list (NDArray): List of training antigens.
def _init_memory_c(self, antigens_list: npt.NDArray) -> List[Cell]:

Function __slice_index_list_by_class(...):

The function __slice_index_list_by_class(...) separates the indices of the rows according to the output class, to iterate over the sample array, only at the positions where the output corresponds to the class being trained:

def __slice_index_list_by_class(self, y: npt.NDArray) -> dict:

Returns a dictionary with classes as keys and indices in X of the samples.


Auxiliary Classes:


_ARB Class (Inherits from Cell)

Constructor:

Parameters:

  • vector (npt.NDArray): A feature vector of the cell. Defaults to None.

Function consume_resource(...):

Parameters:

  • n_resource (float) : The initial amount of resources.
  • amplified (float): Amplifier for resource consumption by the cell. It is multiplied by the cell's stimulus. The default value is 1.
def consume_resource(self, n_resource: float, amplified: float = 1) -> float:

Returns the remaining amount of resources after consumption.


References


  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: http://dx.doi.org/10.1007/978-3-662-43631-8.

  1. AZZOUG, Aghiles. Artificial Immune Recognition System V2. Available at: https://github.com/AghilesAzzoug/Artificial-Immune-System

Português

AIRS (Sistema de Reconhecimento Imune Artificial)

Construtor AIRS:

A classe AIRS tem como objetivo realizar classificação utilizando metáforas de seleção e expansão clonal.

Esta implementação é inspirada no AIRS2, uma versão simplificada do algoritmo AIRS original, introduzindo adaptações para lidar com conjuntos de dados contínuos e binários.

Baseado no Algoritmo 16.5 de Brabazon et al. 1.

Estudos relacionados de destaque: 2.

Atributos:

  • n_resources (float): Quantidade total de recursos disponíveis. O padrão é 10.

  • rate_clonal (float): Número máximo de clones possíveis de uma classe. Esta quantidade é multiplicada por (estímulo da célula * taxa de hipermutação) para definir o número de clones. O padrão é 10.

  • rate_hypermutation (int): Taxa de clones mutados derivada de rate_clonal como um fator escalar. O padrão é 0,75.

  • affinity_threshold_scalar (float): Limiar de afinidade normalizado. O padrão é 0,75.

  • k (int): Número de vizinhos mais próximos (k-NN) que será usado para escolher um rótulo na predição. O padrão é 10.

  • max_iters (int): Número máximo de interações no processo de refinamento do conjunto ARB exposto a aᵢ. O padrão é 100.

  • resource_amplified (float): Amplificador de consumo de recursos, multiplicado com o estímulo para subtrair recursos. O padrão é 1.0 (sem amplificação).

  • metric (Literal["manhattan", "minkowski", "euclidean"]): Forma de calcular a distância entre o detector e a amostra:

    • 'euclidean' ➜ O cálculo da distância é dado pela expressão: √( (x₁ - x₂)² + (y₁ - y₂)² + ... + (nₙ - nₙ)² ).
    • 'minkowski' ➜ O cálculo da distância é dado pela expressão: ( |X₁ - Y₁|ᵖ + |X₂ - Y₂|ᵖ + ... + |Xₙ - Yₙ|ᵖ )¹/ᵖ.
    • 'manhattan' ➜ O cálculo da distância é dado pela expressão: ( |x₁ - x₂| + |y₁ - y₂| + ... + |nₙ - nₙ| ). O padrão é "euclidean".
  • seed (int): Semente para geração aleatória de valores dos detectores. O padrão é None.

  • **kwargs:

    • p (float): Este parâmetro armazena o valor de p usado na distância de Minkowski. O padrão é 2, que corresponde à distância euclidiana normalizada. Diferentes valores de p resultam em variantes distintas da distância de Minkowski. Saiba mais.

Outras variáveis inicializadas:

  • cells_memory (dict): Armazena uma lista de células de memória por classe.
  • affinity_threshold (dict): Define o limiar de afinidade entre antígenos.
  • classes (npt.NDArray): Lista de classes de saída.

Métodos Públicos

Método fit(...)

A função fit(...) gera detectores para os não-pertencentes em relação às amostras:

def fit(self, X: npt.NDArray, y: npt.NDArray):

Realiza o treinamento conforme X e y, utilizando o método Sistema de Reconhecimento Imune Artificial (AIRS).

Parâmetros de entrada:

  • X: Array com as características das amostras, com N amostras (linhas) e N características (colunas), normalizado para valores entre [0, 1].
  • y: Array com as classes de saída correspondentes às N amostras relacionadas a X.
  • verbose: Booleano, padrão True, determina se o feedback da geração dos detectores será impresso.

Retorna a instância da classe.


Método predict(...)

A função predict(...) realiza a predição de classes utilizando os detectores gerados:

def predict(self, X: npt.NDArray) -> npt.NDArray:

Parâmetro de entrada:

  • X: Array com as características para predição, com N amostras (linhas) e N colunas.

Retorna:

  • C: Um array de predições com as classes de saída para as características fornecidas.
  • None: Se não houver detectores.

Método score(...):

A função score(...) calcula a acurácia do modelo treinado realizando predições e calculando a precisão.

def score(self, X: npt.NDArray, y: list) -> float:

Retorna a acurácia como um float.


Métodos Privados

Método _refinement_arb(...):

A função "_refinement_arb(...)" refina o conjunto ARB até que o valor médio de estímulo ultrapasse o limiar definido (affinity_threshold_scalar).

Parâmetros:

  • c_match (Cell): Célula com o maior estímulo em relação a aᵢ.
  • arb_list (List[_ARB]): Conjunto ARB.
def _refinement_arb(self, ai: npt.NDArray, c_match: Cell, arb_list: List[_ARB]) -> _ARB:

Retorna a célula (_ARB) com o maior estímulo ARB.


Método _cells_affinity_threshold(...):

A função "_cells_affinity_threshold(...)" calcula o limiar de afinidade com base na afinidade média entre instâncias de treinamento, onde aᵢ e aⱼ são um par de antígenos, e a afinidade é medida pela distância (Euclidiana, Manhattan, Minkowski, Hamming). Seguindo a fórmula:

$$ \text{affinity}_{\text{threshold}} = \frac{ \sum_{i=1}^{n-1} \sum_{j=i+1}^{n} \text{affinity}(a_i, a_j)}{n(n-1)/2} $$

Parâmetros:

  • antigens_list (NDArray): Lista de antígenos de treinamento.
def _cells_affinity_threshold(self, antigens_list: npt.NDArray):

Método _affinity(...):

A função "_affinity(...)" calcula o estímulo entre dois vetores usando métricas.

Parâmetros:

  • u (npt.NDArray): Coordenadas do primeiro ponto.
  • v (npt.NDArray): Coordenadas do segundo ponto.
def _affinity(self, u: npt.NDArray, v: npt.NDArray) -> float:

Retorna a taxa de estímulo entre os vetores.


Método _init_memory_c(...):

A função "_init_memory_c(...)" inicializa células de memória selecionando aleatoriamente n_antigens_selected da lista de antígenos de treinamento.

Parâmetros:

  • antigens_list (NDArray): Lista de antígenos de treinamento.
def _init_memory_c(self, antigens_list: npt.NDArray) -> List[Cell]:

Classes Auxiliares:


Classe _ARB (Herdada de Cell)

Construtor:

Parâmetros:

  • vector (npt.NDArray): Vetor de características da célula. Padrão é None.

Método consume_resource(...):

Parâmetros:

  • n_resource (float) : A quantidade inicial de recursos.
  • amplified (float): Amplificador para o consumo de recursos pela célula. É multiplicado pelo estímulo da célula. O padrão é 1.
def consume_resource(self, n_resource: float, amplified: float = 1) -> float:

Retorna a quantidade restante de recursos após o consumo.


Referências


  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: http://dx.doi.org/10.1007/978-3-662-43631-8.

  1. AZZOUG, Aghiles. Artificial Immune Recognition System V2. Disponível em: https://github.com/AghilesAzzoug/Artificial-Immune-System