aisp 0.1.41__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aisp/__init__.py +2 -2
- aisp/base/__init__.py +1 -0
- aisp/base/_classifier.py +38 -32
- aisp/base/mutation.py +86 -0
- aisp/csa/__init__.py +9 -0
- aisp/csa/_ai_immune_recognition_sys.py +506 -0
- aisp/csa/_base.py +119 -0
- aisp/csa/_cell.py +47 -0
- aisp/exceptions.py +9 -8
- aisp/nsa/__init__.py +2 -2
- aisp/nsa/_base.py +35 -27
- aisp/nsa/_negative_selection.py +177 -156
- aisp/nsa/_ns_core.py +49 -39
- aisp/utils/__init__.py +1 -1
- aisp/utils/_multiclass.py +18 -12
- aisp/utils/distance.py +96 -78
- aisp/utils/metrics.py +10 -8
- aisp/utils/sanitizers.py +27 -18
- {aisp-0.1.41.dist-info → aisp-0.2.0.dist-info}/METADATA +9 -16
- aisp-0.2.0.dist-info/RECORD +23 -0
- {aisp-0.1.41.dist-info → aisp-0.2.0.dist-info}/WHEEL +1 -1
- aisp-0.1.41.dist-info/RECORD +0 -18
- {aisp-0.1.41.dist-info → aisp-0.2.0.dist-info}/licenses/LICENSE +0 -0
- {aisp-0.1.41.dist-info → aisp-0.2.0.dist-info}/top_level.txt +0 -0
aisp/__init__.py
CHANGED
aisp/base/__init__.py
CHANGED
aisp/base/_classifier.py
CHANGED
@@ -10,9 +10,10 @@ from ..utils.metrics import accuracy_score
|
|
10
10
|
|
11
11
|
|
12
12
|
class BaseClassifier(ABC):
|
13
|
-
"""
|
14
|
-
|
15
|
-
and
|
13
|
+
"""Base class for classification algorithms.
|
14
|
+
|
15
|
+
Defines the abstract methods ``fit`` and ``predict``, and implements the ``score``,
|
16
|
+
``get_params`` method.
|
16
17
|
"""
|
17
18
|
|
18
19
|
classes: Optional[Union[npt.NDArray, list]] = None
|
@@ -20,38 +21,41 @@ class BaseClassifier(ABC):
|
|
20
21
|
@abstractmethod
|
21
22
|
def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
|
22
23
|
"""
|
23
|
-
|
24
|
+
Train the model using the input data X and corresponding labels y.
|
24
25
|
|
25
26
|
This abstract method is implemented by the class that inherits it.
|
26
27
|
|
27
28
|
Parameters
|
28
29
|
----------
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
X : npt.NDArray
|
31
|
+
Input data used for training the model.
|
32
|
+
y : npt.NDArray
|
33
|
+
Corresponding labels or target values for the input data.
|
34
|
+
verbose : bool, default=True
|
35
|
+
Flag to enable or disable detailed output during training.
|
34
36
|
|
35
37
|
Returns
|
36
|
-
|
37
|
-
|
38
|
+
-------
|
39
|
+
self : BaseClassifier
|
40
|
+
Returns the instance of the class that implements this method.
|
38
41
|
"""
|
39
42
|
|
40
43
|
@abstractmethod
|
41
44
|
def predict(self, X) -> Optional[npt.NDArray]:
|
42
45
|
"""
|
43
|
-
|
46
|
+
Generate predictions based on the input data X.
|
44
47
|
|
45
48
|
This abstract method is implemented by the class that inherits it.
|
46
49
|
|
47
50
|
Parameters
|
48
51
|
----------
|
49
|
-
|
52
|
+
X : npt.NDArray
|
53
|
+
Input data for which predictions will be generated.
|
50
54
|
|
51
55
|
Returns
|
52
|
-
|
53
|
-
|
54
|
-
``None`` if the prediction fails.
|
56
|
+
-------
|
57
|
+
Predictions : Optional[npt.NDArray]
|
58
|
+
Predicted values for each input sample, or ``None`` if the prediction fails.
|
55
59
|
"""
|
56
60
|
|
57
61
|
def score(self, X: npt.NDArray, y: list) -> float:
|
@@ -59,21 +63,22 @@ class BaseClassifier(ABC):
|
|
59
63
|
Score function calculates forecast accuracy.
|
60
64
|
|
61
65
|
Details
|
62
|
-
|
66
|
+
-------
|
63
67
|
This function performs the prediction of X and checks how many elements are equal
|
64
68
|
between vector y and y_predicted. This function was added for compatibility with some
|
65
69
|
scikit-learn functions.
|
66
70
|
|
67
71
|
Parameters
|
68
72
|
----------
|
69
|
-
|
73
|
+
X : np.ndarray
|
70
74
|
Feature set with shape (n_samples, n_features).
|
71
|
-
|
75
|
+
y : np.ndarray
|
72
76
|
True values with shape (n_samples,).
|
73
77
|
|
74
78
|
Returns
|
75
|
-
|
76
|
-
|
79
|
+
-------
|
80
|
+
accuracy : float
|
81
|
+
The accuracy of the model.
|
77
82
|
"""
|
78
83
|
if len(y) == 0:
|
79
84
|
return 0
|
@@ -81,27 +86,28 @@ class BaseClassifier(ABC):
|
|
81
86
|
return accuracy_score(y, y_pred)
|
82
87
|
|
83
88
|
def _slice_index_list_by_class(self, y: npt.NDArray) -> dict:
|
84
|
-
"""
|
85
|
-
|
86
|
-
|
87
|
-
|
89
|
+
"""Separate the indices of the lines according to the output class.
|
90
|
+
|
91
|
+
Loop through the sample array only in positions where the output matches the class
|
92
|
+
being trained.
|
88
93
|
|
89
94
|
Parameters
|
90
95
|
----------
|
91
|
-
|
92
|
-
``X`` sample array.
|
96
|
+
y : npt.NDArray
|
97
|
+
Receives a y [``N sample``] array with the output classes of the ``X`` sample array.
|
93
98
|
|
94
|
-
|
95
|
-
|
96
|
-
|
99
|
+
Returns
|
100
|
+
-------
|
101
|
+
dict: dict
|
102
|
+
A dictionary with the list of array positions(``y``), with the classes as key.
|
97
103
|
"""
|
98
104
|
return slice_index_list_by_class(self.classes, y)
|
99
105
|
|
100
106
|
def get_params(self, deep: bool = True) -> dict: # pylint: disable=W0613
|
101
107
|
"""
|
102
|
-
|
108
|
+
Return a dictionary with the object's main parameters.
|
103
109
|
|
104
|
-
This
|
110
|
+
This method is required to ensure compatibility with scikit-learn functions.
|
105
111
|
"""
|
106
112
|
return {
|
107
113
|
key: value
|
aisp/base/mutation.py
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
"""
|
2
|
+
The functions perform utilize Numba decorators for Just-In-Time compilation.
|
3
|
+
|
4
|
+
Contains functions that generate sets of mutated clones from continuous or binary vectors,
|
5
|
+
simulating the clonal expansion process in artificial immune systems.
|
6
|
+
"""
|
7
|
+
|
8
|
+
import numpy as np
|
9
|
+
import numpy.typing as npt
|
10
|
+
from numba import njit, types
|
11
|
+
|
12
|
+
|
13
|
+
@njit([(types.float64[:], types.int64)], cache=True)
|
14
|
+
def clone_and_mutate_continuous(
|
15
|
+
vector: npt.NDArray[np.float64],
|
16
|
+
n: int
|
17
|
+
) -> npt.NDArray[np.float64]:
|
18
|
+
"""
|
19
|
+
Generate a set of mutated clones from a cell represented by a continuous vector.
|
20
|
+
|
21
|
+
This function creates `n` clones of the input vector and applies random mutations to each of
|
22
|
+
them, simulating the process of clonal expansion in artificial immune systems. Each clone
|
23
|
+
will have a random number of mutations applied in distinct positions of the original vector.
|
24
|
+
|
25
|
+
Parameters
|
26
|
+
----------
|
27
|
+
vector : npt.NDArray[np.float64]
|
28
|
+
The original immune cell with continuous values to be cloned and mutated.
|
29
|
+
n : int
|
30
|
+
The number of mutated clones to be generated.
|
31
|
+
|
32
|
+
Returns
|
33
|
+
-------
|
34
|
+
clone_set : npt.NDArray
|
35
|
+
An Array(n, len(vector)) containing the `n` mutated clones of the original vector.
|
36
|
+
"""
|
37
|
+
n_features = vector.shape[0]
|
38
|
+
clone_set = np.empty((n, n_features), dtype=np.float64)
|
39
|
+
for i in range(n):
|
40
|
+
n_mutations = np.random.randint(1, n_features)
|
41
|
+
clone = vector.copy()
|
42
|
+
position_mutations = np.random.permutation(n_features)[:n_mutations]
|
43
|
+
for j in range(n_mutations):
|
44
|
+
idx = position_mutations[j]
|
45
|
+
clone[idx] = np.float64(np.random.random())
|
46
|
+
clone_set[i] = clone
|
47
|
+
|
48
|
+
return clone_set
|
49
|
+
|
50
|
+
|
51
|
+
@njit([(types.boolean[:], types.int64)], cache=True)
|
52
|
+
def clone_and_mutate_binary(
|
53
|
+
vector: npt.NDArray[np.bool_],
|
54
|
+
n: int
|
55
|
+
) -> npt.NDArray[np.bool_]:
|
56
|
+
"""
|
57
|
+
Generate a set of mutated clones from a cell represented by a binary vector.
|
58
|
+
|
59
|
+
This function creates `n` clones of the input vector and applies random mutations to each of
|
60
|
+
them, changing some bits randomly. The process simulates clonal expansion in artificial
|
61
|
+
immune systems with discrete representations.
|
62
|
+
|
63
|
+
Parameters
|
64
|
+
----------
|
65
|
+
vector : npt.NDArray[np.bool_]
|
66
|
+
The original immune cell with binary values to be cloned and mutated.
|
67
|
+
n : int
|
68
|
+
The number of mutated clones to be generated.
|
69
|
+
|
70
|
+
Returns
|
71
|
+
-------
|
72
|
+
clone_set : npt.NDArray[np.bool_]
|
73
|
+
An Array(n, len(vector)) containing the `n` mutated clones of the original vector.
|
74
|
+
"""
|
75
|
+
n_features = vector.shape[0]
|
76
|
+
clone_set = np.empty((n, n_features), dtype=np.bool_)
|
77
|
+
for i in range(n):
|
78
|
+
n_mutations = np.random.randint(1, n_features)
|
79
|
+
clone = vector.copy()
|
80
|
+
position_mutations = np.random.permutation(n_features)[:n_mutations]
|
81
|
+
for j in range(n_mutations):
|
82
|
+
idx = position_mutations[j]
|
83
|
+
clone[idx] = np.bool_(np.random.randint(0, 2))
|
84
|
+
clone_set[i] = clone
|
85
|
+
|
86
|
+
return clone_set
|
aisp/csa/__init__.py
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
"""Module (CSA) Clonal Selection Algorithm.
|
2
|
+
|
3
|
+
CSAs are inspired by the process of antibody proliferation upon detecting an antigen, during which
|
4
|
+
the generated antibodies undergo mutations in an attempt to enhance pathogen recognition.
|
5
|
+
"""
|
6
|
+
from ._ai_immune_recognition_sys import AIRS
|
7
|
+
|
8
|
+
__author__ = 'João Paulo da Silva Barros'
|
9
|
+
__all__ = ['AIRS']
|