aisp 0.1.40__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/base/_classifier.py CHANGED
@@ -1,10 +1,11 @@
1
1
  """Base class for classification algorithm."""
2
2
 
3
3
  from abc import ABC, abstractmethod
4
- from typing import Optional
4
+ from typing import Optional, Union
5
5
 
6
6
  import numpy.typing as npt
7
7
 
8
+ from ..utils import slice_index_list_by_class
8
9
  from ..utils.metrics import accuracy_score
9
10
 
10
11
 
@@ -14,6 +15,8 @@ class BaseClassifier(ABC):
14
15
  and implementing the ``get_params`` method.
15
16
  """
16
17
 
18
+ classes: Optional[Union[npt.NDArray, list]] = None
19
+
17
20
  @abstractmethod
18
21
  def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
19
22
  """
@@ -77,6 +80,23 @@ class BaseClassifier(ABC):
77
80
  y_pred = self.predict(X)
78
81
  return accuracy_score(y, y_pred)
79
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
+
80
100
  def get_params(self, deep: bool = True) -> dict: # pylint: disable=W0613
81
101
  """
82
102
  The get_params function Returns a dictionary with the object's main parameters.
@@ -12,7 +12,6 @@ from ._ns_core import (
12
12
  check_detector_rnsa_validity,
13
13
  )
14
14
  from ..exceptions import MaxDiscardsReachedError
15
- from ..utils import slice_index_list_by_class
16
15
  from ..utils.distance import (
17
16
  min_distance_to_class_vectors,
18
17
  get_metric_code,
@@ -111,6 +110,11 @@ class RNSA(BaseNSA):
111
110
  self._detectors: Union[dict, None] = None
112
111
  self.classes: npt.NDArray = None
113
112
 
113
+ @property
114
+ def detectors(self) -> Dict[str, list[Detector]]:
115
+ """Returns the trained detectors, organized by class."""
116
+ return self._detectors
117
+
114
118
  def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
115
119
  """
116
120
  The function ``fit(...)``, performs the training according to ``X`` and ``y``, using the
@@ -141,7 +145,7 @@ class RNSA(BaseNSA):
141
145
  # Dictionary that will store detectors with classes as keys.
142
146
  list_detectors_by_class = {}
143
147
  # Separates the classes for training.
144
- sample_index = self.__slice_index_list_by_class(y)
148
+ sample_index = self._slice_index_list_by_class(y)
145
149
  # Progress bar for generating all detectors.
146
150
  if verbose:
147
151
  progress = tqdm(
@@ -168,7 +172,9 @@ class RNSA(BaseNSA):
168
172
  # If the detector is valid, add it to the list of valid detectors.
169
173
  if valid_detector is not False:
170
174
  discard_count = 0
171
- radius = valid_detector[1] if self.algorithm == "V-detector" else None
175
+ radius = (
176
+ valid_detector[1] if self.algorithm == "V-detector" else None
177
+ )
172
178
  valid_detectors_set.append(Detector(vector_x, radius))
173
179
  if verbose:
174
180
  progress.update(1)
@@ -178,7 +184,7 @@ class RNSA(BaseNSA):
178
184
  raise MaxDiscardsReachedError(_class_)
179
185
 
180
186
  # Add detectors, with classes as keys in the dictionary.
181
- list_detectors_by_class[_class_] = np.array(valid_detectors_set)
187
+ list_detectors_by_class[_class_] = valid_detectors_set
182
188
  # Notify completion of detector generation for the classes.
183
189
  if verbose:
184
190
  progress.set_description(
@@ -253,24 +259,6 @@ class RNSA(BaseNSA):
253
259
  c.append(max(average_distance, key=average_distance.get))
254
260
  return np.array(c)
255
261
 
256
- def __slice_index_list_by_class(self, y: npt.NDArray) -> dict:
257
- """
258
- The function ``__slice_index_list_by_class(...)``, separates the indices of the lines
259
- according to the output class, to loop through the sample array, only in positions where
260
- the output is the class being trained.
261
-
262
- Parameters
263
- ----------
264
- * y (npt.NDArray)
265
- Receives a ``y``[``N sample``] array with the output classes of the \
266
- ``X`` sample array.
267
-
268
- Returns
269
- ----------
270
- * dict: A dictionary with the list of array positions(``y``), with the classes as key.
271
- """
272
- return slice_index_list_by_class(self.classes, y)
273
-
274
262
  def __checks_valid_detector(
275
263
  self, x_class: npt.NDArray = None, vector_x: npt.NDArray = None
276
264
  ) -> Union[bool, tuple[bool, float]]:
@@ -503,6 +491,11 @@ class BNSA(BaseNSA):
503
491
  self._detectors: Optional[dict] = None
504
492
  self._detectors_stack: npt.NDArray = None
505
493
 
494
+ @property
495
+ def detectors(self) -> Dict[str, npt.NDArray[np.bool_]]:
496
+ """Returns the trained detectors, organized by class."""
497
+ return self._detectors
498
+
506
499
  def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
507
500
  """
508
501
  The function ``fit(...)``, performs the training according to ``X`` and ``y``, using the
@@ -531,7 +524,7 @@ class BNSA(BaseNSA):
531
524
  # Dictionary that will store detectors with classes as keys.
532
525
  list_detectors_by_class = {}
533
526
  # Separates the classes for training.
534
- sample_index: dict = self.__slice_index_list_by_class(y)
527
+ sample_index: dict = self._slice_index_list_by_class(y)
535
528
  # Progress bar for generating all detectors.
536
529
  if verbose:
537
530
  progress = tqdm(
@@ -662,21 +655,3 @@ class BNSA(BaseNSA):
662
655
  class_differences[_class_] = distances.sum() / self.N
663
656
 
664
657
  c.append(max(class_differences, key=class_differences.get))
665
-
666
- def __slice_index_list_by_class(self, y: npt.NDArray) -> dict:
667
- """
668
- The function ``__slice_index_list_by_class(...)``, separates the indices of the lines
669
- according to the output class, to loop through the sample array, only in positions where
670
- the output is the class being trained.
671
-
672
- Parameters
673
- ----------
674
- * y (``npt.NDArray``):
675
- Receives a ``y``[``N sample``] array with the output classes of the ``X``
676
- sample array.
677
-
678
- Returns
679
- ----------
680
- * dict: A dictionary with the list of array positions(``y``), with the classes as key.
681
- """
682
- return slice_index_list_by_class(self.classes, y)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aisp
3
- Version: 0.1.40
3
+ Version: 0.1.41
4
4
  Summary: Package with techniques of artificial immune systems.
5
5
  Author-email: João Paulo da Silva Barros <jpsilvabarr@gmail.com>
6
6
  Maintainer-email: Alison Zille Lopes <alisonzille@gmail.com>
@@ -1,18 +1,18 @@
1
1
  aisp/__init__.py,sha256=pKOdkhFxWup_lhQPofFTk4rpqd0_IlYygHnkBhE2g2w,111
2
2
  aisp/exceptions.py,sha256=ONiKCZrBhfUaMO1vYHnOr1_eL9PtVJ7b_HRroMbORpc,1405
3
3
  aisp/base/__init__.py,sha256=rpb9ADmBtNpRAEJw61c7CsbZhHA2vGQOvjFmpk7w39c,100
4
- aisp/base/_classifier.py,sha256=qv6TxzfnRwO7HvN7CA8PS1dueAnm-RifelRnCjrK9D0,3027
4
+ aisp/base/_classifier.py,sha256=Dvimv7JVfqeg4ws9NqZPj0Ue_fNh9kEEXrspOrLdBoQ,3845
5
5
  aisp/nsa/__init__.py,sha256=3-fSIJCMB_jcLGTr6DcVkloRfWyQJ8-JzD0CLhvpvgc,411
6
6
  aisp/nsa/_base.py,sha256=cT404V5onduK19WW6ajxy_yUZha3Fk8KZJvJHt_UZBM,4250
7
- aisp/nsa/_negative_selection.py,sha256=0_VI3rAeVzPcSRwjMjqCq6RT-1_a_-HCFtIIJL6jpyc,29582
7
+ aisp/nsa/_negative_selection.py,sha256=mWyjRmTMOJxtL8xHDooN3RreyLgq5JqmEb_CYAHBaCk,28485
8
8
  aisp/nsa/_ns_core.py,sha256=VWt_L2ODXtngJ7BVmSVT7i7kK98axibOmCzbYCkjeqI,4934
9
9
  aisp/utils/__init__.py,sha256=-r--qdfTq4dVJU-VoJ7dvXTNS5_I1q1aGAQ02zNBOGw,217
10
10
  aisp/utils/_multiclass.py,sha256=t0RLIdUoUbvFL30a8XfO3BMXc8MimtLCHNbGCDL2BQk,1051
11
11
  aisp/utils/distance.py,sha256=SZTE5KEB7QX9vKoY9GfFOfFGbswVhQM6-9baLq4ZXns,6304
12
12
  aisp/utils/metrics.py,sha256=o6aD6qTLQtXhWNOMGjgweHMQ6SMQ2hh42NcSEmgY3RI,1292
13
13
  aisp/utils/sanitizers.py,sha256=7wJSo5ZkRX1g16C9sEN8TWQ5l9Gzfl0-8CgLULnPNlk,1873
14
- aisp-0.1.40.dist-info/licenses/LICENSE,sha256=fTqV5eBpeAZO0_jit8j4Ref9ikBSlHJ8xwj5TLg7gFk,7817
15
- aisp-0.1.40.dist-info/METADATA,sha256=em8eJAx3lr87ZZ1ajsoSkIj-itauLz25E1FKcdjf1QU,4818
16
- aisp-0.1.40.dist-info/WHEEL,sha256=GHB6lJx2juba1wDgXDNlMTyM13ckjBMKf-OnwgKOCtA,91
17
- aisp-0.1.40.dist-info/top_level.txt,sha256=Q5aJi_rAVT5UNS1As0ZafoyS5dwNibnoyOYV7RWUB9s,5
18
- aisp-0.1.40.dist-info/RECORD,,
14
+ aisp-0.1.41.dist-info/licenses/LICENSE,sha256=fTqV5eBpeAZO0_jit8j4Ref9ikBSlHJ8xwj5TLg7gFk,7817
15
+ aisp-0.1.41.dist-info/METADATA,sha256=W0BxdV6IefnxXgDIe-eSa1XvGgFsqqsAnvIDxzL8EGE,4818
16
+ aisp-0.1.41.dist-info/WHEEL,sha256=0CuiUZ_p9E4cD6NyLD6UG80LBXYyiSYZOKDm5lp32xk,91
17
+ aisp-0.1.41.dist-info/top_level.txt,sha256=Q5aJi_rAVT5UNS1As0ZafoyS5dwNibnoyOYV7RWUB9s,5
18
+ aisp-0.1.41.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.3.0)
2
+ Generator: setuptools (80.3.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5