aisp 0.1.35__py3-none-any.whl → 0.1.40__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 CHANGED
@@ -1,4 +1,4 @@
1
1
  """Artificial Immune Systems Package"""
2
2
 
3
3
  __author__ = "João Paulo da Silva Barros"
4
- __version__ = "0.1.35"
4
+ __version__ = "0.1.40"
aisp/base/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ """Base class modules."""
2
+ from ._classifier import BaseClassifier
3
+
4
+ __all__ = ['BaseClassifier']
@@ -0,0 +1,90 @@
1
+ """Base class for classification algorithm."""
2
+
3
+ from abc import ABC, abstractmethod
4
+ from typing import Optional
5
+
6
+ import numpy.typing as npt
7
+
8
+ from ..utils.metrics import accuracy_score
9
+
10
+
11
+ class BaseClassifier(ABC):
12
+ """
13
+ Base class for classification algorithms, defining the abstract methods ``fit`` and ``predict``,
14
+ and implementing the ``get_params`` method.
15
+ """
16
+
17
+ @abstractmethod
18
+ def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
19
+ """
20
+ Function to train the model using the input data ``X`` and corresponding labels ``y``.
21
+
22
+ This abstract method is implemented by the class that inherits it.
23
+
24
+ Parameters
25
+ ----------
26
+ * X (``npt.NDArray``): Input data used for training the model, previously normalized to the
27
+ range [0, 1].
28
+ * y (``npt.NDArray``): Corresponding labels or target values for the input data.
29
+ * verbose (``bool``, optional): Flag to enable or disable detailed output during training.
30
+ Default is ``True``.
31
+
32
+ Returns
33
+ ----------
34
+ * self: Returns the instance of the class that implements this method.
35
+ """
36
+
37
+ @abstractmethod
38
+ def predict(self, X) -> Optional[npt.NDArray]:
39
+ """
40
+ Function to generate predictions based on the input data ``X``.
41
+
42
+ This abstract method is implemented by the class that inherits it.
43
+
44
+ Parameters
45
+ ----------
46
+ * X (``npt.NDArray``): Input data for which predictions will be generated.
47
+
48
+ Returns
49
+ ----------
50
+ * Predictions (``Optional[npt.NDArray]``): Predicted values for each input sample, or
51
+ ``None`` if the prediction fails.
52
+ """
53
+
54
+ def score(self, X: npt.NDArray, y: list) -> float:
55
+ """
56
+ Score function calculates forecast accuracy.
57
+
58
+ Details
59
+ ----------
60
+ This function performs the prediction of X and checks how many elements are equal
61
+ between vector y and y_predicted. This function was added for compatibility with some
62
+ scikit-learn functions.
63
+
64
+ Parameters
65
+ ----------
66
+ * X (``np.ndarray``):
67
+ Feature set with shape (n_samples, n_features).
68
+ * y (``np.ndarray``):
69
+ True values with shape (n_samples,).
70
+
71
+ Returns
72
+ ----------
73
+ * accuracy (``float``): The accuracy of the model.
74
+ """
75
+ if len(y) == 0:
76
+ return 0
77
+ y_pred = self.predict(X)
78
+ return accuracy_score(y, y_pred)
79
+
80
+ def get_params(self, deep: bool = True) -> dict: # pylint: disable=W0613
81
+ """
82
+ The get_params function Returns a dictionary with the object's main parameters.
83
+
84
+ This function is required to ensure compatibility with scikit-learn functions.
85
+ """
86
+ return {
87
+ key: value
88
+ for key, value in self.__dict__.items()
89
+ if not key.startswith("_")
90
+ }
aisp/nsa/__init__.py CHANGED
@@ -8,4 +8,4 @@ from ._negative_selection import BNSA, RNSA
8
8
 
9
9
  __author__ = "João Paulo da Silva Barros"
10
10
  __all__ = ["RNSA", "BNSA"]
11
- __version__ = "0.1.35"
11
+ __version__ = "0.1.40"
aisp/nsa/_base.py CHANGED
@@ -1,68 +1,27 @@
1
1
  """Base Class for Negative Selection Algorithm."""
2
- from abc import abstractmethod
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 Base:
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
- X: npt.NDArray = None,
64
- y: npt.NDArray = None,
65
- _class_: Literal["RNSA", "BNSA"] = "RNSA",
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
- If X is not an ndarray or list.
128
- * FeatureDimensionMismatch
129
- If the number of features in X does not match the expected number.
130
- * ValueError
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
- This abstract method is implemented by the class that inherits it.
106
+ @dataclass(slots=True)
107
+ class Detector:
108
+ """
109
+ Represents a non-self detector of the RNSA class.
203
110
 
204
- Parameters
205
- ----------
206
- * X (``npt.NDArray``): Input data for which predictions will be generated.
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
- Returns
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