Skip to content

Commit

Permalink
Merge pull request #9 from AIS-Package/main
Browse files Browse the repository at this point in the history
v0.1.33
  • Loading branch information
Joao-Paulo-Silva authored Oct 14, 2024
2 parents 08fee2e + 7696f9f commit bae8f59
Show file tree
Hide file tree
Showing 24 changed files with 1,066 additions and 446 deletions.
27 changes: 2 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
> 2. [Installation.](#installation)
> 1. [Dependencies](#dependencies)
> 2. [User installation](#user-installation)
> 3. [How to import the Techniques](#how-to-import-the-techniques)
> 3. [Examples.](#examples)
---
Expand All @@ -55,8 +54,8 @@ Artificial Immune Systems (AIS) are inspired by the vertebrate immune system, cr
##### Algorithms implemented:

> - [x] [**Negative Selection.**](https://ais-package.github.io/docs/aisp-techniques/Negative%20Selection/)
> - [ ] *Clonal Selection Algorithms.*
> - [ ] *Dendritic Cells.*
> - [ ] *Clonalg.*
> - [ ] *Immune Network Theory.*
</section>
Expand Down Expand Up @@ -93,17 +92,7 @@ pip install aisp
```

</section>
<section id='how-to-import-the-techniques'>

##### **How to import the Techniques**

``` Python
from aisp.NSA import RNSA

nsa = RNSA(N=300, r=0.05)
```

</section>
</section>
<section id='examples'>

Expand Down Expand Up @@ -147,7 +136,6 @@ Below are some examples that use a database for classification with the [Jupyter
> 2. [Instalação.](#instalação)
> 1. [Dependências](#dependências)
> 2. [Instalação do usuário](#instalação-do-usuário)
> 3. [Como importar as Tecnicas](#como-importar-as-tecnicas)
> 3. [Exemplos.](#exemplos)
---
Expand All @@ -164,8 +152,8 @@ Os sistemas imunológicos artificiais (SIA) inspiram-se no sistema imunológico
##### Algoritmos implementados:

> - [x] [**Seleção Negativa.**](https://ais-package.github.io/docs/aisp-techniques/Negative%20Selection/)
> - [ ] *Algoritmos de Seleção Clonal.*
> - [ ] *Células Dendríticas.*
> - [ ] *Clonalg.*
> - [ ] *Teoria da Rede Imune.*
</section>
Expand Down Expand Up @@ -203,17 +191,6 @@ pip install aisp

</section>

<section id='como-importar-as-tecnicas'>

##### **Como importar as Tecnicas**

``` Python
from aisp.NSA import RNSA

nsa = RNSA(N=300, r=0.05)
```

</section>
</section>
<section id='exemplos'>

Expand Down
2 changes: 1 addition & 1 deletion aisp/NSA/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__author__ = "João Paulo da Silva Barros"
__all__ = ["RNSA", "BNSA"]
__version__ = "0.1.31"
__version__ = "0.1.33"
123 changes: 82 additions & 41 deletions aisp/NSA/_base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from abc import abstractmethod

import numpy as np
import numpy.typing as npt
from typing import Literal
from typing import Literal, Optional
from scipy.spatial.distance import euclidean, cityblock, minkowski

from ..utils.metrics import accuracy_score


class Base:
"""
Expand Down Expand Up @@ -93,8 +97,11 @@ def _distance(self, u: npt.NDArray, v: npt.NDArray):
else:
return euclidean(u, v)

def _check_and_raise_exceptions_fit(self, X: npt.NDArray = None, y: npt.NDArray = None,
_class_: Literal["RNSA", "BNSA"] = "RNSA",
@staticmethod
def _check_and_raise_exceptions_fit(
X: npt.NDArray = None,
y: npt.NDArray = None,
_class_: Literal["RNSA", "BNSA"] = "RNSA",
):
"""
Function responsible for verifying fit function parameters and throwing exceptions if the \
Expand Down Expand Up @@ -149,43 +156,6 @@ def _check_and_raise_exceptions_fit(self, X: npt.NDArray = None, y: npt.NDArray
"The array X contains values that are not composed only of 0 and 1."
)

def _slice_index_list_by_class(self, y: npt.NDArray) -> dict:
"""
The function ``__slice_index_list_by_class(...)``, separates the indices of the lines \
according to the output class, to loop through the sample array, only in positions where \
the output is the class being trained.
Parameters:
---
* y (npt.NDArray): Receives a ``y``[``N sample``] array with the output classes of the \
``X`` sample array.
returns:
---
* dict: A dictionary with the list of array positions(``y``), with the classes as key.
---
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.
Parameters:
---
* y (npt.NDArray): Recebe um array ``y``[``N amostra``] com as classes de saída do \
array de amostra ``X``.
Returns:
---
* dict: Um dicionário com a lista de posições do array(``y``), com as classes como chave.
"""
position_samples = dict()
for _class_ in self.classes:
# Pega as posições das amostras por classes a partir do y.
position_samples[_class_] = list(np.where(y == _class_)[0])

return position_samples

def _score(self, X: npt.NDArray, y: list) -> float:
"""
Score function calculates forecast accuracy.
Expand Down Expand Up @@ -237,4 +207,75 @@ def _score(self, X: npt.NDArray, y: list) -> float:
if len(y) == 0:
return 0
y_pred = self.predict(X)
return np.sum(y == y_pred) / len(y)
return accuracy_score(y, y_pred)

@abstractmethod
def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
"""
Function to train the model using the input data ``X`` and corresponding labels ``y``.
This abstract method is implemented by the class that inherits it.
Parameters:
---
* X (``npt.NDArray``): Input data used for training the model, previously normalized to the range [0, 1].
* y (``npt.NDArray``): Corresponding labels or target values for the input data.
* verbose (``bool``, optional): Flag to enable or disable detailed output during \
training. Default is ``True``.
Returns:
---
* self: Returns the instance of the class that implements this method.
---
Função para treinar o modelo usando os dados de entrada ``X`` e os classes correspondentes ``y``.
Este método abstrato é implementado pela classe que o herdar.
Parâmetros:
---
* X (``npt.NDArray``): Dados de entrada utilizados para o treinamento do modelo, previamente \
normalizados no intervalo [0, 1].
* y (``npt.NDArray``): Rótulos ou valores-alvo correspondentes aos dados de entrada.
* verbose (``bool``, opcional): Flag para ativar ou desativar a saída detalhada durante o \
treinamento. O padrão é ``True``.
Retornos:
---
* self: Retorna a instância da classe que implementa este método.
"""
pass

@abstractmethod
def predict(self, X) -> Optional[npt.NDArray]:
"""
Function to generate predictions based on the input data ``X``.
This abstract method is implemented by the class that inherits it.
Parameters:
---
* X (``npt.NDArray``): Input data for which predictions will be generated.
Returns:
---
* Predictions (``Optional[npt.NDArray]``): Predicted values for each input sample, or ``None``
if the prediction fails.
---
Função para gerar previsões com base nos dados de entrada ``X``.
Este método abstrato é implementado pela classe que o herdar.
Parâmetros:
---
* X (``npt.NDArray``): Dados de entrada para os quais as previsões serão geradas.
Retornos:
---
* Previsões (``Optional[npt.NDArray]``): Valores previstos para cada amostra de entrada,
ou ``None`` se a previsão falhar.
"""
pass
Loading

0 comments on commit bae8f59

Please sign in to comment.