aisp 0.1.40__tar.gz → 0.1.41__tar.gz
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-0.1.40 → aisp-0.1.41}/PKG-INFO +1 -1
- {aisp-0.1.40 → aisp-0.1.41}/aisp/base/_classifier.py +21 -1
- {aisp-0.1.40 → aisp-0.1.41}/aisp/nsa/_negative_selection.py +16 -41
- {aisp-0.1.40 → aisp-0.1.41}/aisp.egg-info/PKG-INFO +1 -1
- {aisp-0.1.40 → aisp-0.1.41}/pyproject.toml +1 -1
- {aisp-0.1.40 → aisp-0.1.41}/LICENSE +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/README.md +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/__init__.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/base/__init__.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/exceptions.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/nsa/__init__.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/nsa/_base.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/nsa/_ns_core.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/utils/__init__.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/utils/_multiclass.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/utils/distance.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/utils/metrics.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp/utils/sanitizers.py +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp.egg-info/SOURCES.txt +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp.egg-info/dependency_links.txt +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp.egg-info/requires.txt +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/aisp.egg-info/top_level.txt +0 -0
- {aisp-0.1.40 → aisp-0.1.41}/setup.cfg +0 -0
@@ -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.
|
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 =
|
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_] =
|
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.
|
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)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|