-
Notifications
You must be signed in to change notification settings - Fork 5
RNSA
The RNSA class has the purpose of classifying and identifying anomalies through the self and not self methods.
Attributes:
-
N (
int): Number of detectors. Defaults to100. -
r (
float): Radius of the detector. Defaults to0.05. -
k (
int): Number of neighbors near the randomly generated detectors to perform the distance average calculation. Defaults to1. -
metric (
str): 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) ¹/ₚ , In this projectp == 2. -
'manhattan'➜ The calculation of the distance is given by the expression: ( |X₁ – X₂| + |Y₁ – Y₂| + ...+ |Yn – Yn₂|) .
Defaults to
'euclidean'. -
-
max_discards (
int): This 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. -
seed (
int): Seed for the random generation of values in the detectors. Defaults toNone. -
algorithm (
str), Set the algorithm version:-
'default-NSA': Default algorithm with fixed radius. -
'V-detector': This algorithm is based on the article "Real-Valued Negative Selection Algorithm with Variable-Sized Detectors", by Ji, Z., Dasgupta, D. (2004), and uses a variable radius for anomaly detection in feature spaces.
Defaults to
'default-NSA'. -
-
r_s (
float): rₛ Radius of theXown samples. -
**kwargs:-
non_self_label (
str): 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. Defaults to'non-self'. -
cell_bounds (
bool): If set toTrue, 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 theV-detectoralgorithm. Defaults toFalse.
-
non_self_label (
Other variables initiated:
-
detectors (
dict): This variable stores a list of detectors by class. -
classes (
npt.NDArray): list of output classes.
The fit(...) function generates the detectors for non-fits with respect to the samples:
def fit(self, X: npt.NDArray, y: npt.NDArray):In it, training is performed according to X and y, using the negative selection method(NegativeSelect).
The input parameters are:
-
X: array with the characteristics of the samples with N samples (rows) and N characteristics (columns). -
y: array with the output classes arranged in N samples that are related toX. -
verbose: boolean with default valueTrue, determines if the feedback from the detector generation will be printed.
Returns the instance of the class.
The predict(...) function performs class prediction using the generated detectors:
def predict(self, X: npt.NDArray) -> npt.NDArray:The input parameter is:
-
X: array with the characteristics for the prediction, with N samples (Rows) and N columns.
Returns:
-
C: prediction array, with the output classes for the given characteristics. -
None: if there are no detectors.
The function score(...) calculates the accuracy of the trained model by making predictions and computing accuracy.
def score(self, X: npt.NDArray, y: list) -> float:It returns the accuracy as a float type.
The def __checks_valid_detector(...) function checks if the detector has a valid r radius for the non-self of the class:
def __checks_valid_detector(self, X: npt.NDArray, vector_x: npt.NDArray, samplesIndexClass: npt.NDArray) -> bool:The input parameters are:
-
X: array with sample characteristics with N samples (rows) and N characteristics (columns), normalized to values between [0, 1]. -
vector_x: Randomly generated candidate detector. -
samplesIndexClass: Array with the indexes of a class.
Returns: True for detectors that do not have samples inside or False if they do.
The def __compare_KnearestNeighbors_List(...) function compares the distance of the k-nearest neighbors, so if the distance of the new sample is smaller, replaces k-1 and sorts in ascending order:
def __compare_KnearestNeighbors_List(self, knn: npt.NDArray, distance: float) -> npt.NDArray:Returns a list of k-nearest neighbor distances.
Function to compare a sample with the detectors, verifying if the sample is proper. In this function, when there is class ambiguity, it returns the class that has the greatest average distance between the detectors.
def __compare_sample_to_detectors(self, line):The input parameters are:
- line: vector with N-features
Returns: The predicted class with the detectors or None if the sample does not qualify for any class.
Check if the distance between the detector and the samples, minus the radius of the samples, is greater than the minimum radius.
def __detector_is_valid_to_Vdetector(self, distance, vector_x):The input parameters are:
- distance (
float): minimum distance calculated between all samples. - vector_x (
numpy.ndarray): randomly generated candidate detector vector x with values between 0 and 1.
Returns:
-
False: if the calculated radius is smaller than the minimum distance or exceeds the edge of the space, if this option is enabled. -
Trueand the distance minus the radius of the samples, if the radius is valid.`
The function def __distance(...) calculates the distance between two points using the technique defined in metric, which are: 'euclidean', 'norm_euclidean', or 'manhattan'
def __distance(self, u: npt.NDArray, v: npt.NDArray):The input parameters are u and v NDArrays, with the coordinates for the points.
Returns: the distance (double) between the two points.
The function __slice_index_list_by_class(...), separates the indices of the lines according to the output class, to go through the sample array, only in the positions that the output is the class that is being trained:
def __slice_index_list_by_class(self, y: npt.NDArray) -> dict:Returns a dictionary with the classes as key and the indices in X of the samples.
A classe RNSA tem a finalidade de classificação e identificação de anomalias através do método self e not self .
Attributes:
-
N (
int): Quantidade de detectores. Defaults to100. -
r (
float): Raio do detector. Defaults to0.05. -
k (
int): Quantidade de vizinhos próximos dos detectores gerados aleatoriamente para efetuar o cálculo da média da distância. Defaults to1. -
metric (
str): Forma para se calcular a distância entre o detector e a amostra:-
'euclidiana'➜ O cálculo da distância dá-se pela expressão: √( (X₁ – X₂)² + (Y₁ – Y₂)² + ... + (Yn – Yn)²). -
'minkowski'➜ O cálculo da distância dá-se pela expressão: ( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ , Neste projetop == 2. -
'manhattan'➜ O cálculo da distância dá-se pela expressão: ( |X₁ – X₂| + |Y₁ – Y₂| + ... + |Yn – Yn₂|).
Defaults to
'euclidean'. -
-
max_discards (
int): Este parâmetro indica o número máximo de descartes de detectores em sequência, que tem como objetivo evitar um possível loop infinito caso seja definido um raio que não seja possível gerar detectores do não-próprio. -
seed (
int): Semente para a geração randômica dos valores nos detectores. Defaults toNone. -
algorithm (
str), Definir a versão do algoritmo:-
'default-NSA': Algoritmo padrão com raio fixo. -
'V-detector': Este algoritmo é baseado no artigo "Real-Valued Negative Selection Algorithm with Variable-Sized Detectors", de autoria de Ji, Z., Dasgupta, D. (2004), e utiliza um raio variável para a detecção de anomalias em espaços de características.
Defaults to
'default-NSA'. -
-
r_s (
float): O valor derₛé o raio das amostras próprias da matrizX. -
**kwargs:-
non_self_label (
str): Esta variável armazena o rótulo que será atribuído quando os dados possuírem apenas uma classe de saída, e a amostra for classificada como não pertencente a essa classe. Defaults to'non-self'. -
cell_bounds (
bool): Se definido comoTrue, esta opção limita a geração dos detectores ao espaço do plano compreendido entre 0 e 1. Isso significa que qualquer detector cujo raio ultrapasse esse limite é descartado, e esta variável é usada exclusivamente no algoritmoV-detector.
-
non_self_label (
Outras variáveis iniciadas:
-
detectors (
dict): Esta variável armazena uma lista de detectores por classe. -
classes (
npt.NDArray): lista de classes de saída.
A função fit(...) gera os detectores para os não próprios com relação às amostras:
def fit(self, X: npt.NDArray, y: npt.NDArray):Nela é realizado o treinamento de acordo com X e y, usando o método de seleção negativa(NegativeSelect).
Os parâmetros de entrada são:
-
X: array com as características das amostras com N amostras (linhas) e N características (colunas), normalizados para valores entre [0, 1]. -
y: array com as classes de saídas disposto em N amostras que são relacionadas aoX. -
verbose: booleano com valor defaultTrue, determina se o feedback da geração dos detectores será printado.
Retorna a instância da classe.
A função predict(...) realiza a previsão das classes utilizando os detectores gerados:
def predict(self, X: npt.NDArray) -> npt.NDArray:O parâmetro de entrada:
-
X: array com as características para a previsão, com N amostras (Linhas) e N colunas.
Retorna:
-
C: Um array de previsão com as classes de saída para as características informadas. -
None: se não houver detectores.
A função "score(...)" calcula a precisão do modelo treinado por meio da realização de previsões e do cálculo da acurácia.
def score(self, X: npt.NDArray, y: list) -> float:retorna a acurácia, do tipo float.
A função def __checks_valid_detector(...) verifica se o detector possui raio r válido para o não-próprio da classe:
def __checks_valid_detector(self, X: npt.NDArray, vector_x: npt.NDArray, samplesIndexClass: npt.NDArray) -> bool:Os parâmetros de entrada são:
-
X: array com as características das amostras com N amostras (linhas) e N características (colunas), normalizados para valores entre [0, 1]. -
vector_x: Detector candidato gerado aleatoriamente. -
samplesIndexClass: Array com os indexs de uma classe.
Retorna: Verdadeiro (True) para os detectores que não possuam amostras em seu interior ou falso (False) se possuir.
A função def __compare_KnearestNeighbors_List(...) compara a distância dos k-vizinhos mais próximo, para isso se a distância da nova amostra for menor, substitui k-1 e ordena em ordem crescente:
def __compare_KnearestNeighbors_List(self, knn: npt.NDArray, distance: float) -> npt.NDArray:Retorna: uma lista com as distâncias dos k-vizinhos mais próximo.
Função para comparar uma amostra com os detectores, verificando se a amostra é própria.
Nesta função, quando possui ambiguidade de classes, retorna a classe que possuir a média de distância maior entre os detectores.
Os parâmetros de entrada são:
- line: vetor com N-características
Retorna: A classe prevista com os detectores ou None se a amostra não se qualificar a nenhuma classe.
Verifique se a distância entre o detector e as amostras, descontando o raio das amostras, é maior do que o raio mínimo.
def __detector_is_valid_to_Vdetector(self, distance, vector_x):Os parâmetros de entrada são:
- distance (
float): distância mínima calculada entre todas as amostras. - vector_x (
numpy.ndarray): vetor x candidato do detector gerado aleatoriamente, com valores entre 0 e 1.
Retorna:
-
False: caso o raio calculado seja menor do que a distância mínima ou ultrapasse a borda do espaço, caso essa opção esteja habilitada. -
Truee a distância menos o raio das amostras, caso o raio seja válido.
A função def __distance(...) calcula a distância entre dois pontos utilizando a técnica definida em mettric, no qual são: 'euclidiana', 'minkowski', ou 'manhattan'
def __distance(self, u: npt.NDArray, v: npt.NDArray):Os parâmetros de entrada são NDArrays: u e v, com as coordenadas para os pontos.
Retorna a distancia (double) entre os dois pontos.
A função __slice_index_list_by_class(...), separa os índices das linhas conforme a classe de saída, para percorrer o array de amostra, apenas nas posições que a saída for a classe que está sendo treinada:
def __slice_index_list_by_class(self, y: npt.NDArray) -> dict:Retorna um dicionario com as classes como chave e os índices em X das amostras.