aisp 0.1.35__py3-none-any.whl → 0.1.41__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 +1 -1
- aisp/base/__init__.py +4 -0
- aisp/base/_classifier.py +110 -0
- aisp/nsa/__init__.py +1 -1
- aisp/nsa/_base.py +26 -120
- aisp/nsa/_negative_selection.py +118 -213
- aisp/nsa/_ns_core.py +153 -0
- aisp/utils/_multiclass.py +1 -0
- aisp/utils/distance.py +215 -0
- aisp/utils/metrics.py +2 -2
- aisp/utils/sanitizers.py +3 -2
- {aisp-0.1.35.dist-info → aisp-0.1.41.dist-info}/METADATA +10 -6
- aisp-0.1.41.dist-info/RECORD +18 -0
- {aisp-0.1.35.dist-info → aisp-0.1.41.dist-info}/WHEEL +1 -1
- aisp-0.1.35.dist-info/RECORD +0 -14
- {aisp-0.1.35.dist-info → aisp-0.1.41.dist-info}/licenses/LICENSE +0 -0
- {aisp-0.1.35.dist-info → aisp-0.1.41.dist-info}/top_level.txt +0 -0
aisp/__init__.py
CHANGED
aisp/base/__init__.py
ADDED
aisp/base/_classifier.py
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
"""Base class for classification algorithm."""
|
2
|
+
|
3
|
+
from abc import ABC, abstractmethod
|
4
|
+
from typing import Optional, Union
|
5
|
+
|
6
|
+
import numpy.typing as npt
|
7
|
+
|
8
|
+
from ..utils import slice_index_list_by_class
|
9
|
+
from ..utils.metrics import accuracy_score
|
10
|
+
|
11
|
+
|
12
|
+
class BaseClassifier(ABC):
|
13
|
+
"""
|
14
|
+
Base class for classification algorithms, defining the abstract methods ``fit`` and ``predict``,
|
15
|
+
and implementing the ``get_params`` method.
|
16
|
+
"""
|
17
|
+
|
18
|
+
classes: Optional[Union[npt.NDArray, list]] = None
|
19
|
+
|
20
|
+
@abstractmethod
|
21
|
+
def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
|
22
|
+
"""
|
23
|
+
Function to train the model using the input data ``X`` and corresponding labels ``y``.
|
24
|
+
|
25
|
+
This abstract method is implemented by the class that inherits it.
|
26
|
+
|
27
|
+
Parameters
|
28
|
+
----------
|
29
|
+
* X (``npt.NDArray``): Input data used for training the model, previously normalized to the
|
30
|
+
range [0, 1].
|
31
|
+
* y (``npt.NDArray``): Corresponding labels or target values for the input data.
|
32
|
+
* verbose (``bool``, optional): Flag to enable or disable detailed output during training.
|
33
|
+
Default is ``True``.
|
34
|
+
|
35
|
+
Returns
|
36
|
+
----------
|
37
|
+
* self: Returns the instance of the class that implements this method.
|
38
|
+
"""
|
39
|
+
|
40
|
+
@abstractmethod
|
41
|
+
def predict(self, X) -> Optional[npt.NDArray]:
|
42
|
+
"""
|
43
|
+
Function to generate predictions based on the input data ``X``.
|
44
|
+
|
45
|
+
This abstract method is implemented by the class that inherits it.
|
46
|
+
|
47
|
+
Parameters
|
48
|
+
----------
|
49
|
+
* X (``npt.NDArray``): Input data for which predictions will be generated.
|
50
|
+
|
51
|
+
Returns
|
52
|
+
----------
|
53
|
+
* Predictions (``Optional[npt.NDArray]``): Predicted values for each input sample, or
|
54
|
+
``None`` if the prediction fails.
|
55
|
+
"""
|
56
|
+
|
57
|
+
def score(self, X: npt.NDArray, y: list) -> float:
|
58
|
+
"""
|
59
|
+
Score function calculates forecast accuracy.
|
60
|
+
|
61
|
+
Details
|
62
|
+
----------
|
63
|
+
This function performs the prediction of X and checks how many elements are equal
|
64
|
+
between vector y and y_predicted. This function was added for compatibility with some
|
65
|
+
scikit-learn functions.
|
66
|
+
|
67
|
+
Parameters
|
68
|
+
----------
|
69
|
+
* X (``np.ndarray``):
|
70
|
+
Feature set with shape (n_samples, n_features).
|
71
|
+
* y (``np.ndarray``):
|
72
|
+
True values with shape (n_samples,).
|
73
|
+
|
74
|
+
Returns
|
75
|
+
----------
|
76
|
+
* accuracy (``float``): The accuracy of the model.
|
77
|
+
"""
|
78
|
+
if len(y) == 0:
|
79
|
+
return 0
|
80
|
+
y_pred = self.predict(X)
|
81
|
+
return accuracy_score(y, y_pred)
|
82
|
+
|
83
|
+
def _slice_index_list_by_class(self, y: npt.NDArray) -> dict:
|
84
|
+
"""
|
85
|
+
The function ``_slice_index_list_by_class(...)``, separates the indices of the lines \
|
86
|
+
according to the output class, to loop through the sample array, only in positions where \
|
87
|
+
the output is the class being trained.
|
88
|
+
|
89
|
+
Parameters
|
90
|
+
----------
|
91
|
+
* y (npt.NDArray): Receives a ``y``[``N sample``] array with the output classes of the \
|
92
|
+
``X`` sample array.
|
93
|
+
|
94
|
+
returns
|
95
|
+
----------
|
96
|
+
* dict: A dictionary with the list of array positions(``y``), with the classes as key.
|
97
|
+
"""
|
98
|
+
return slice_index_list_by_class(self.classes, y)
|
99
|
+
|
100
|
+
def get_params(self, deep: bool = True) -> dict: # pylint: disable=W0613
|
101
|
+
"""
|
102
|
+
The get_params function Returns a dictionary with the object's main parameters.
|
103
|
+
|
104
|
+
This function is required to ensure compatibility with scikit-learn functions.
|
105
|
+
"""
|
106
|
+
return {
|
107
|
+
key: value
|
108
|
+
for key, value in self.__dict__.items()
|
109
|
+
if not key.startswith("_")
|
110
|
+
}
|
aisp/nsa/__init__.py
CHANGED
aisp/nsa/_base.py
CHANGED
@@ -1,68 +1,27 @@
|
|
1
1
|
"""Base Class for Negative Selection Algorithm."""
|
2
|
-
|
2
|
+
|
3
|
+
from abc import ABC
|
4
|
+
from dataclasses import dataclass
|
3
5
|
from typing import Literal, Optional
|
4
6
|
|
5
7
|
import numpy as np
|
6
8
|
import numpy.typing as npt
|
7
|
-
from scipy.spatial.distance import cityblock, euclidean, minkowski
|
8
9
|
|
10
|
+
from ..base import BaseClassifier
|
9
11
|
from ..exceptions import FeatureDimensionMismatch
|
10
|
-
from ..utils.metrics import accuracy_score
|
11
|
-
from ..utils.sanitizers import sanitize_choice
|
12
12
|
|
13
13
|
|
14
|
-
class
|
14
|
+
class BaseNSA(BaseClassifier, ABC):
|
15
15
|
"""
|
16
16
|
The base class contains functions that are used by more than one class in the package, and
|
17
17
|
therefore are considered essential for the overall functioning of the system.
|
18
|
-
|
19
|
-
Parameters
|
20
|
-
----------
|
21
|
-
* metric (``str``): Way to calculate the distance between the detector and the sample:
|
22
|
-
* ``'Euclidean'`` ➜ The calculation of the distance is given by the expression: \
|
23
|
-
√( (x₁ – x₂)² + (y₁ – y₂)² + ... + (yn – yn)²).
|
24
|
-
* ``'minkowski'`` ➜ The calculation of the distance is given by the expression: \
|
25
|
-
( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ.
|
26
|
-
* ``'manhattan'`` ➜ The calculation of the distance is given by the expression: \
|
27
|
-
( |x₁ – x₂| + |y₁ – y₂| + ... + |yn – yn|) .
|
28
|
-
* p (``float``): This parameter stores the value of ``p`` used in the Minkowski distance.\
|
29
|
-
The default is ``2``, which represents normalized Euclidean distance. Different \
|
30
|
-
values of p lead to different variants of the [Minkowski Distance][1].
|
31
|
-
|
32
|
-
Notes
|
33
|
-
----------
|
34
|
-
[1]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.minkowski_distance.html
|
35
18
|
"""
|
36
19
|
|
37
|
-
def __init__(self, metric: str = "euclidean", p: float = 2):
|
38
|
-
self.metric = sanitize_choice(metric, ["manhattan", "minkowski"], "euclidean")
|
39
|
-
self.p: float = p
|
40
|
-
|
41
|
-
def _distance(self, u: npt.NDArray, v: npt.NDArray):
|
42
|
-
"""
|
43
|
-
Function to calculate the distance between two points by the chosen ``metric``.
|
44
|
-
|
45
|
-
Parameters
|
46
|
-
----------
|
47
|
-
* u (``npt.NDArray``): Coordinates of the first point.
|
48
|
-
* v (``npt.NDArray``): Coordinates of the second point.
|
49
|
-
|
50
|
-
returns
|
51
|
-
----------
|
52
|
-
* Distance (``double``) between the two points.
|
53
|
-
"""
|
54
|
-
if self.metric == "manhattan":
|
55
|
-
return cityblock(u, v)
|
56
|
-
if self.metric == "minkowski":
|
57
|
-
return minkowski(u, v, self.p)
|
58
|
-
|
59
|
-
return euclidean(u, v)
|
60
|
-
|
61
20
|
@staticmethod
|
62
21
|
def _check_and_raise_exceptions_fit(
|
63
|
-
|
64
|
-
|
65
|
-
|
22
|
+
X: npt.NDArray = None,
|
23
|
+
y: npt.NDArray = None,
|
24
|
+
_class_: Literal["RNSA", "BNSA"] = "RNSA",
|
66
25
|
) -> None:
|
67
26
|
"""
|
68
27
|
Function responsible for verifying fit function parameters and throwing exceptions if the
|
@@ -78,7 +37,7 @@ class Base:
|
|
78
37
|
Raises
|
79
38
|
----------
|
80
39
|
* TypeError: If X or y are not ndarrays or have incompatible shapes.
|
81
|
-
* ValueError: If _class_ is BNSA and X contains values that are not composed only of
|
40
|
+
* ValueError: If _class_ is BNSA and X contains values that are not composed only of
|
82
41
|
0 and 1.
|
83
42
|
"""
|
84
43
|
if isinstance(X, list):
|
@@ -123,21 +82,16 @@ class Base:
|
|
123
82
|
|
124
83
|
Raises
|
125
84
|
----------
|
126
|
-
* TypeError
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
If _class_ is BNSA and X contains values that are not composed only of 0 and 1.
|
85
|
+
* TypeError: If X is not an ndarray or list.
|
86
|
+
* FeatureDimensionMismatch: If the number of features in X does not match the expected
|
87
|
+
number.
|
88
|
+
* ValueError: If _class_ is BNSA and X contains values that are not composed only of 0
|
89
|
+
and 1.
|
132
90
|
"""
|
133
91
|
if not isinstance(X, (np.ndarray, list)):
|
134
92
|
raise TypeError("X is not an ndarray or list")
|
135
93
|
if expected != len(X[0]):
|
136
|
-
raise FeatureDimensionMismatch(
|
137
|
-
expected,
|
138
|
-
len(X[0]),
|
139
|
-
"X"
|
140
|
-
)
|
94
|
+
raise FeatureDimensionMismatch(expected, len(X[0]), "X")
|
141
95
|
|
142
96
|
if _class_ != "BNSA":
|
143
97
|
return
|
@@ -148,65 +102,17 @@ class Base:
|
|
148
102
|
"The array X contains values that are not composed only of 0 and 1."
|
149
103
|
)
|
150
104
|
|
151
|
-
def score(self, X: npt.NDArray, y: list) -> float:
|
152
|
-
"""
|
153
|
-
Score function calculates forecast accuracy.
|
154
|
-
|
155
|
-
Details
|
156
|
-
----------
|
157
|
-
This function performs the prediction of X and checks how many elements are equal
|
158
|
-
between vector y and y_predicted. This function was added for compatibility with some
|
159
|
-
scikit-learn functions.
|
160
|
-
|
161
|
-
Parameters
|
162
|
-
----------
|
163
|
-
* X (``np.ndarray``):
|
164
|
-
Feature set with shape (n_samples, n_features).
|
165
|
-
* y (``np.ndarray``):
|
166
|
-
True values with shape (n_samples,).
|
167
|
-
|
168
|
-
Returns
|
169
|
-
----------
|
170
|
-
* accuracy (``float``): The accuracy of the model.
|
171
|
-
"""
|
172
|
-
if len(y) == 0:
|
173
|
-
return 0
|
174
|
-
y_pred = self.predict(X)
|
175
|
-
return accuracy_score(y, y_pred)
|
176
|
-
|
177
|
-
@abstractmethod
|
178
|
-
def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
|
179
|
-
"""
|
180
|
-
Function to train the model using the input data ``X`` and corresponding labels ``y``.
|
181
|
-
|
182
|
-
This abstract method is implemented by the class that inherits it.
|
183
|
-
|
184
|
-
Parameters
|
185
|
-
----------
|
186
|
-
* X (``npt.NDArray``): Input data used for training the model, previously normalized to the
|
187
|
-
range [0, 1].
|
188
|
-
* y (``npt.NDArray``): Corresponding labels or target values for the input data.
|
189
|
-
* verbose (``bool``, optional): Flag to enable or disable detailed output during training.
|
190
|
-
Default is ``True``.
|
191
|
-
|
192
|
-
Returns
|
193
|
-
----------
|
194
|
-
* self: Returns the instance of the class that implements this method.
|
195
|
-
"""
|
196
|
-
|
197
|
-
@abstractmethod
|
198
|
-
def predict(self, X) -> Optional[npt.NDArray]:
|
199
|
-
"""
|
200
|
-
Function to generate predictions based on the input data ``X``.
|
201
105
|
|
202
|
-
|
106
|
+
@dataclass(slots=True)
|
107
|
+
class Detector:
|
108
|
+
"""
|
109
|
+
Represents a non-self detector of the RNSA class.
|
203
110
|
|
204
|
-
|
205
|
-
|
206
|
-
|
111
|
+
Attributes
|
112
|
+
----------
|
113
|
+
* position (``npt.NDArray[np.float64]``): Detector feature vector.
|
114
|
+
* radius (``float, optional``): Detector radius, used in the V-detector algorithm.
|
115
|
+
"""
|
207
116
|
|
208
|
-
|
209
|
-
|
210
|
-
* Predictions (``Optional[npt.NDArray]``): Predicted values for each input sample, or
|
211
|
-
``None`` if the prediction fails.
|
212
|
-
"""
|
117
|
+
position: npt.NDArray[np.float64]
|
118
|
+
radius: Optional[float] = None
|