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/nsa/__init__.py CHANGED
@@ -1,11 +1,11 @@
1
- """nsa: Module (NSA) Negative Selection Algorithm
1
+ """Module (NSA) Negative Selection Algorithm.
2
2
 
3
3
  NSAs simulate the maturation process of T-cells in the immune system, where these cells learn to
4
4
  distinguish between self and non-self. Only T-cells capable of recognizing non-self elements are
5
5
  preserved.
6
6
  """
7
+
7
8
  from ._negative_selection import BNSA, RNSA
8
9
 
9
10
  __author__ = "João Paulo da Silva Barros"
10
11
  __all__ = ["RNSA", "BNSA"]
11
- __version__ = "0.1.40"
aisp/nsa/_base.py CHANGED
@@ -13,8 +13,9 @@ from ..exceptions import FeatureDimensionMismatch
13
13
 
14
14
  class BaseNSA(BaseClassifier, ABC):
15
15
  """
16
- The base class contains functions that are used by more than one class in the package, and
17
- therefore are considered essential for the overall functioning of the system.
16
+ Base class containing functions used by multiple classes in the package.
17
+
18
+ These functions are essential for the overall functioning of the system.
18
19
  """
19
20
 
20
21
  @staticmethod
@@ -23,22 +24,26 @@ class BaseNSA(BaseClassifier, ABC):
23
24
  y: npt.NDArray = None,
24
25
  _class_: Literal["RNSA", "BNSA"] = "RNSA",
25
26
  ) -> None:
26
- """
27
- Function responsible for verifying fit function parameters and throwing exceptions if the
28
- verification is not successful.
27
+ """Verify fit function parameters.
28
+
29
+ Throw exceptions if the verification fails.
29
30
 
30
31
  Parameters
31
32
  ----------
32
- * X (``npt.NDArray``) Training array, containing the samples and their
33
- characteristics, [``N samples`` (rows)][``N features`` (columns)].
34
- * y (``npt.NDArray``) Array of target classes of ``X`` with [``N samples`` (lines)].
35
- * _class_ (``Literal[RNSA, BNSA], optional``) Current class. Defaults to 'RNSA'.
33
+ * X : npt.NDArray
34
+ Training array, containing the samples and their characteristics, [``N samples`` (
35
+ rows)][``N features`` (columns)].
36
+ * y : npt.NDArray
37
+ Array of target classes of ``X`` with [``N samples`` (lines)].
38
+ * _class_ : Literal[RNSA, BNSA], default="RNSA"
39
+ Current class.
36
40
 
37
41
  Raises
38
- ----------
39
- * TypeError: If X or y are not ndarrays or have incompatible shapes.
40
- * ValueError: If _class_ is BNSA and X contains values that are not composed only of
41
- 0 and 1.
42
+ ------
43
+ TypeError
44
+ If X or y are not ndarrays or have incompatible shapes.
45
+ ValueError
46
+ If _class_ is BNSA and X contains values that are not composed only of 0 and 1.
42
47
  """
43
48
  if isinstance(X, list):
44
49
  X = np.array(X)
@@ -66,27 +71,28 @@ class BaseNSA(BaseClassifier, ABC):
66
71
  expected: int = 0,
67
72
  _class_: Literal["RNSA", "BNSA"] = "RNSA",
68
73
  ) -> None:
69
- """
70
- Function responsible for verifying predict function parameters and throwing exceptions if
71
- the verification is not successful.
74
+ """Verify predict function parameters.
75
+
76
+ Throw exceptions if the verification fails.
72
77
 
73
78
  Parameters
74
79
  ----------
75
- * X (``npt.NDArray``)
80
+ X : npt.NDArray
76
81
  Input array for prediction, containing the samples and their characteristics,
77
82
  [``N samples`` (rows)][``N features`` (columns)].
78
- * expected (``int``)
83
+ expected : int
79
84
  Expected number of features per sample (columns in X).
80
- * _class_ (``Literal[RNSA, BNSA], optional``)
85
+ _class_ : Literal[RNSA, BNSA], default="RNSA"
81
86
  Current class. Defaults to 'RNSA'.
82
87
 
83
88
  Raises
84
- ----------
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.
89
+ ------
90
+ TypeError
91
+ If X is not an numpy.ndarray or list.
92
+ FeatureDimensionMismatch
93
+ If the number of features in X does not match the expected number.
94
+ ValueError
95
+ If _class_ is BNSA and X contains values that are not composed only of 0 and 1.
90
96
  """
91
97
  if not isinstance(X, (np.ndarray, list)):
92
98
  raise TypeError("X is not an ndarray or list")
@@ -110,8 +116,10 @@ class Detector:
110
116
 
111
117
  Attributes
112
118
  ----------
113
- * position (``npt.NDArray[np.float64]``): Detector feature vector.
114
- * radius (``float, optional``): Detector radius, used in the V-detector algorithm.
119
+ position : npt.NDArray[np.float64]
120
+ Detector feature vector.
121
+ radius : float, optional
122
+ Detector radius, used in the V-detector algorithm.
115
123
  """
116
124
 
117
125
  position: npt.NDArray[np.float64]
@@ -22,58 +22,64 @@ from ._base import BaseNSA, Detector
22
22
 
23
23
 
24
24
  class RNSA(BaseNSA):
25
- """
26
- The ``RNSA`` (Real-Valued Negative Selection Algorithm) class is for classification and
27
- identification purposes. of anomalies through the self and not self method.
25
+ """Real-Valued Negative Selection Algorithm (RNSA) for classification and anomaly detection.
26
+
27
+ Uses the self and non-self method to identify anomalies.
28
28
 
29
29
  Parameters
30
30
  ----------
31
- * N (``int``): Number of detectors. Defaults to ``100``.
32
- * r (``float``): Radius of the detector. Defaults to ``0.05``.
33
- * r_s (``float``): rₛ Radius of the ``X`` own samples. Defaults to ``0.0001``.
34
- * k (``int``): Number of neighbors near the randomly generated detectors to perform the
35
- distance average calculation. Defaults to ``1``.
36
- * metric (``str``): Way to calculate the distance between the detector and the sample:
37
- + ``'Euclidean'`` The calculation of the distance is given by the expression:
38
- √( (x₁ x₂)² + (y₁ y₂)² + ... + (yn – yn)²).
39
- + ``'minkowski'`` ➜ The calculation of the distance is given by the expression:
40
- ( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ.
41
- + ``'manhattan'`` The calculation of the distance is given by the expression:
42
- ( |x₁ – x₂| + |y₁ – y₂| + ... + |yn – yn|) .
43
-
44
- Defaults to ``'euclidean'``.
45
- * max_discards (``int``): This parameter indicates the maximum number of consecutive
46
- detector discards, aimed at preventing a possible infinite loop in case a radius
47
- is defined that cannot generate non-self detectors. Defaults to ``1000``.
48
- * seed (``int``): Seed for the random generation of values in the detectors. Defaults to
49
- ``None``.
50
- * algorithm(``str``), Set the algorithm version:
51
- * ``'default-NSA'``: Default algorithm with fixed radius.
52
- * ``'V-detector'``: This algorithm is based on the article \
53
- [Real-Valued Negative Selection Algorithm with Variable-Sized Detectors][2], \
54
- by Ji, Z., Dasgupta, D. (2004), and uses a variable radius for anomaly \
55
- detection in feature spaces.
56
-
57
- Defaults to ``'default-NSA'``.
58
-
59
- * ``**kwargs``:
60
- - non_self_label (``str``): This variable stores the label that will be assigned \
61
- when the data has only one output class, and the sample is classified as not \
62
- belonging to that class. Defaults to ``'non-self'``.
63
- - cell_bounds (``bool``): If set to ``True``, this option limits the generation \
64
- of detectors to the space within the plane between 0 and 1. This means that \
65
- any detector whose radius exceeds this limit is discarded, this variable is \
66
- only used in the ``V-detector`` algorithm. Defaults to ``False``.
67
- - p (``float``): This parameter stores the value of ``p`` used in the Minkowski \
68
- distance. The default is ``2``, which represents normalized Euclidean distance.\
69
- Different values of p lead to different variants of the [Minkowski Distance][1].
70
-
71
- Notes
31
+ N : int, default=100
32
+ Number of detectors.
33
+ r : float, default=0.05
34
+ Radius of the detector.
35
+ r_s : float, default=0.0001
36
+ rₛ Radius of the ``X`` own samples.
37
+ k : int, default=1
38
+ Number of neighbors near the randomly generated detectors to perform the distance average
39
+ calculation.
40
+ metric: str, default='euclidean'
41
+ Way to calculate the distance between the detector and the sample:
42
+
43
+ + ``'Euclidean'`` ➜ The calculation of the distance is given by the expression:
44
+ √( (x₁ – x₂)² + (y₁ – y₂)² + ... + (yn – yn)²).
45
+ + ``'minkowski'`` The calculation of the distance is given by the expression:
46
+ ( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn – Yn|p) ¹/ₚ.
47
+ + ``'manhattan'`` The calculation of the distance is given by the expression:
48
+ ( |x₁ x₂| + |y₁ y₂| + ... + |yn yn|) .
49
+ max_discards : int, default=1000
50
+ This parameter indicates the maximum number of consecutive detector discards, aimed at
51
+ preventing a possible infinite loop in case a radius is defined that cannot generate
52
+ non-self detectors.
53
+ seed : int, default=None
54
+ Seed for the random generation of values in the detectors.
55
+ algorithm : str, default='default-NSA'
56
+ Set the algorithm version:
57
+
58
+ + ``'default-NSA'``: Default algorithm with fixed radius.
59
+ + ``'V-detector'``: This algorithm is based on the article Ji & Dasgupta (2004) [1]_
60
+ and uses a variable radius for anomaly detection in feature spaces.
61
+
62
+ **kwargs : dict
63
+ Parâmetros adicionais. Os seguintes argumentos são reconhecidos:
64
+
65
+ + non_self_label : str, default='non-self'
66
+ This variable stores the label that will be assigned when the data has only one
67
+ output class, and the sample is classified as not belonging to that class.
68
+ + cell_bounds : bool, default=False
69
+ If set to ``True``, this option limits the generation of detectors to the space
70
+ within the plane between 0 and 1. This means that any detector whose radius exceeds
71
+ this limit is discarded, this variable is only used in the ``V-detector`` algorithm.
72
+ + p : float, default=2
73
+ This parameter stores the value of ``p`` used in the Minkowski distance. The default
74
+ is ``2``, which represents Euclidean distance. Different values of p lead
75
+ to different variants of the Minkowski Distance.
76
+
77
+ References
72
78
  ----------
73
- [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.minkowski_distance.html
74
-
75
- [2] https://doi.org/10.1007/978-3-540-24854-5_30
76
-
79
+ .. [1] Ji, Z.; Dasgupta, D. (2004).
80
+ Real-Valued Negative Selection Algorithm with Variable-Sized Detectors.
81
+ In *Lecture Notes in Computer Science*, vol. 3025.
82
+ https://doi.org/10.1007/978-3-540-24854-5_30
77
83
  """
78
84
 
79
85
  def __init__(
@@ -117,25 +123,30 @@ class RNSA(BaseNSA):
117
123
 
118
124
  def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
119
125
  """
120
- The function ``fit(...)``, performs the training according to ``X`` and ``y``, using the
121
- method negative selection method(``NegativeSelect``).
126
+ Perform training according to X and y, using the negative selection method (NegativeSelect).
122
127
 
123
128
  Parameters
124
129
  ----------
125
- * X (``npt.NDArray``): Training array, containing the samples and their \
126
- characteristics, [``N samples`` (rows)][``N features`` (columns)].
127
- * y (``npt.NDArray``): Array of target classes of ``X`` with [``N samples`` (lines)].
128
- verbose (``bool``): Feedback from detector generation to the user.
130
+ X : npt.NDArray
131
+ Training array, containing the samples and their characteristics, [``N samples`` (
132
+ rows)][``N features`` (columns)].
133
+ y : npt.NDArray
134
+ Array of target classes of ``X`` with [``N samples`` (lines)].
135
+ verbose: bool, default=True
136
+ Feedback from detector generation to the user.
129
137
 
130
138
  Raises
131
- ----------
132
- * TypeError: If X or y are not ndarrays or have incompatible shapes.
133
- * MaxDiscardsReachedError: The maximum number of detector discards was reached during
134
- maturation. Check the defined radius value and consider reducing it.
139
+ ------
140
+ TypeError
141
+ If X or y are not ndarrays or have incompatible shapes.
142
+ MaxDiscardsReachedError
143
+ The maximum number of detector discards was reached during maturation. Check the
144
+ defined radius value and consider reducing it.
135
145
 
136
146
  Returns
137
- ----------
138
- * (``self``): Returns the instance itself.
147
+ -------
148
+ self : RNSA
149
+ Returns the instance itself.
139
150
  """
140
151
  progress = None
141
152
  super()._check_and_raise_exceptions_fit(X, y)
@@ -197,29 +208,26 @@ class RNSA(BaseNSA):
197
208
 
198
209
  def predict(self, X: npt.NDArray) -> Optional[npt.NDArray]:
199
210
  """
200
- Function to perform the prediction of classes based on detectors
201
- created after training.
211
+ Prediction of classes based on detectors created after training.
202
212
 
203
213
  Parameters
204
214
  ----------
205
- * X (``npt.NDArray``)
206
- Array with input samples with [``N samples`` (Lines)] and
207
- [``N characteristics``(Columns)]
215
+ X : npt.NDArray
216
+ Array with input samples with [``N_samples`` (Lines)] and [``N_characteristics``
217
+ (Columns)]
208
218
 
209
219
  Raises
210
- ----------
211
- * TypeError
220
+ ------
221
+ TypeError
212
222
  If X is not an ndarray or list.
213
- * FeatureDimensionMismatch
223
+ FeatureDimensionMismatch
214
224
  If the number of features in X does not match the expected number.
215
225
 
216
226
  Returns
217
- ----------
218
- * C (``npt.NDArray``)
227
+ -------
228
+ C : npt.NDArray or None
219
229
  an ndarray of the form ``C`` [``N samples``], containing the predicted classes
220
- for ``X``.
221
- * ``None``
222
- If there are no detectors for the prediction.
230
+ for ``X``. Returns `None` if no detectors are available for prediction.
223
231
  """
224
232
  # If there are no detectors, Returns None.
225
233
  if self._detectors is None:
@@ -260,21 +268,22 @@ class RNSA(BaseNSA):
260
268
  return np.array(c)
261
269
 
262
270
  def __checks_valid_detector(
263
- self, x_class: npt.NDArray = None, vector_x: npt.NDArray = None
271
+ self, x_class: npt.NDArray, vector_x: npt.NDArray
264
272
  ) -> Union[bool, tuple[bool, float]]:
265
273
  """
266
- Function to check if the detector has a valid non-proper ``r`` radius for the class.
274
+ Check if the detector has a valid non-proper r radius for the class.
267
275
 
268
276
  Parameters
269
277
  ----------
270
- * x_class (``npt.NDArray``)
278
+ x_class : npt.NDArray
271
279
  Array ``x_class`` with the samples per class.
272
- * vector_x (``npt.NDArray``)
280
+ vector_x : npt.NDArray
273
281
  Randomly generated vector x candidate detector with values between[0, 1].
274
282
 
275
283
  Returns
276
- ----------
277
- * Validity (``bool``): Returns whether the detector is valid or not.
284
+ -------
285
+ Validity : bool
286
+ Returns whether the detector is valid or not.
278
287
  """
279
288
  # If any of the input arrays have zero size, Returns false.
280
289
  if np.size(x_class) == 0 or np.size(vector_x) == 0:
@@ -282,7 +291,7 @@ class RNSA(BaseNSA):
282
291
  # If self.k > 1, uses the k nearest neighbors (kNN); otherwise, checks the detector
283
292
  # without considering kNN.
284
293
  if self.k > 1:
285
- knn_list = np.empty(shape=0)
294
+ knn_list = []
286
295
  for x in x_class:
287
296
  # Calculates the distance between the two vectors and adds it to the kNN list if
288
297
  # the distance is smaller than the largest distance in the list.
@@ -317,20 +326,21 @@ class RNSA(BaseNSA):
317
326
  self, knn: npt.NDArray, distance: float
318
327
  ) -> npt.NDArray:
319
328
  """
320
- Compares the k-nearest neighbor distance at position ``k-1`` in the list ``knn``,
321
- if the distance of the new sample is less, replace it and sort in ascending order.
329
+ Compare the k-nearest neighbor distance at position k=1 in the list knn.
322
330
 
331
+ If the distance of the new sample is less, replace it and sort in ascending order.
323
332
 
324
333
  Parameters
325
334
  ----------
326
- * knn (``npt.NDArray``)
335
+ knn : npt.NDArray
327
336
  List of k-nearest neighbor distances.
328
- * distance (``float``)
337
+ distance : float
329
338
  Distance to check.
330
339
 
331
340
  Returns
332
- ----------
333
- * ``npt.NDArray``: Updated and sorted nearest neighbor list.
341
+ -------
342
+ knn : npt.NDArray
343
+ Updated and sorted nearest neighbor list.
334
344
  """
335
345
  # If the number of distances in kNN is less than k, adds the distance.
336
346
  if len(knn) < self.k:
@@ -346,18 +356,20 @@ class RNSA(BaseNSA):
346
356
 
347
357
  return knn
348
358
 
349
- def __compare_sample_to_detectors(self, line: npt.NDArray):
359
+ def __compare_sample_to_detectors(self, line: npt.NDArray) -> Optional[str]:
350
360
  """
351
- Function to compare a sample with the detectors, verifying if the sample is proper.
361
+ Compare a sample with the detectors, verifying if the sample is proper.
352
362
 
353
363
  Parameters
354
364
  ----------
355
- * line (``npt.NDArray``): vector with N-features
365
+ line : npt.NDArray
366
+ vector with N-features
356
367
 
357
368
  Returns
358
- ----------
359
- * Returns the predicted class with the detectors or None if the sample does not qualify
360
- for any class.
369
+ -------
370
+ possible_classes : Optional[str]
371
+ Returns the predicted class with the detectors or None if the sample does not qualify
372
+ for any class.
361
373
  """
362
374
  # List to store the classes and the average distance between the detectors and the sample.
363
375
  possible_classes = []
@@ -391,37 +403,45 @@ class RNSA(BaseNSA):
391
403
 
392
404
  def __distance(self, u: npt.NDArray, v: npt.NDArray) -> float:
393
405
  """
394
- Function to calculate the distance between two points by the chosen ``metric``.
406
+ Calculate the distance between two points by the chosen ``metric``.
395
407
 
396
408
  Parameters
397
409
  ----------
398
- * u (``npt.NDArray``): Coordinates of the first point.
399
- * v (``npt.NDArray``): Coordinates of the second point.
410
+ u : npt.NDArray
411
+ Coordinates of the first point.
412
+ v : npt.NDArray
413
+ Coordinates of the second point.
400
414
 
401
415
  Returns
402
- ----------
403
- * Distance (``float``): between the two points.
416
+ -------
417
+ Distance : float
418
+ between the two points.
404
419
  """
405
420
  return compute_metric_distance(u, v, get_metric_code(self.metric), self.p)
406
421
 
407
422
  def __detector_is_valid_to_vdetector(
408
423
  self, distance: float, vector_x: npt.NDArray
409
424
  ) -> Union[bool, tuple[bool, float]]:
410
- """
411
- Check if the distance between the detector and the samples, minus the radius of the samples,
412
- is greater than the minimum radius.
425
+ """Validate the detector against the vdetector.
426
+
427
+ Check if the distance between the detector and the samples, minus the radius of the
428
+ samples, is greater than the minimum radius.
413
429
 
414
430
  Parameters
415
431
  ----------
416
- * distance (``float``): minimum distance calculated between all samples.
417
- * vector_x (``numpy.ndarray``): randomly generated candidate detector vector x with
418
- values between 0 and 1.
432
+ distance : float
433
+ minimum distance calculated between all samples.
434
+ vector_x : np.ndarray
435
+ randomly generated candidate detector vector x with values between 0 and 1.
419
436
 
420
437
  Returns
421
- ----------
422
- * ``False`` if the calculated radius is smaller than the minimum distance or exceeds the
423
- edge of the space, if this option is enabled.
424
- * ``True`` and the distance minus the radius of the samples, if the radius is valid.`
438
+ -------
439
+ valid : bool
440
+
441
+ - ``False`` if the calculated radius is smaller than the minimum distance or exceeds
442
+ the edge of the space, if this option is enabled.
443
+
444
+ - ``True`` and the distance minus the radius of the samples, if the radius is valid.`
425
445
  """
426
446
  new_detector_r = float(distance - self.r_s)
427
447
  if self.r >= new_detector_r:
@@ -437,27 +457,33 @@ class RNSA(BaseNSA):
437
457
 
438
458
 
439
459
  class BNSA(BaseNSA):
440
- """
441
- The ``BNSA`` (Binary Negative Selection Algorithm) class is for classification and
442
- identification purposes of anomalies through the self and not self method.
460
+ """BNSA (Binary Negative Selection Algorithm).
461
+
462
+ Class is for classification and identification purposes of anomalies through the self and not
463
+ self method.
443
464
 
444
465
  Parameters
445
466
  ----------
446
- * N (``int``): Number of detectors. Defaults to ``100``.
447
- * aff_thresh (``float``): The variable represents the percentage of similarity
448
- between the T cell and the own samples. The default value is 10% (0.1), while a value of
449
- 1.0 represents 100% similarity.
450
- * max_discards (``int``): This parameter indicates the maximum number of detector discards in
451
- sequence, which aims to avoid a possible infinite loop if a radius is defined that it is
452
- not possible to generate non-self detectors. Defaults to ``1000``.
453
- * seed (``int``): Seed for the random generation of values in the detectors. Defaults to
454
- ``None``.
455
- * no_label_sample_selection (``str``): Method for selecting labels for samples designated as
456
- non-self by all detectors. Available method types:
457
- - (``max_average_difference``): Selects the class with the highest average difference
458
- among the detectors.
459
- - (``max_nearest_difference``): Selects the class with the highest difference between
460
- the nearest and farthest detector from the sample.
467
+ N : int, default=100
468
+ Number of detectors.
469
+ aff_thresh : float, default=0.1
470
+ The variable represents the percentage of similarity between the T cell and the own
471
+ samples. The default value is 10% (0.1), while a value of 1.0 represents 100% similarity.
472
+ max_discards : int, default=1000
473
+ This parameter indicates the maximum number of detector discards in sequence, which aims
474
+ to avoid a possible infinite loop if a radius is defined that it is not possible to
475
+ generate non-self detectors.
476
+ seed : Optional[int], default=None
477
+ Seed for the random generation of values in the detectors.
478
+ no_label_sample_selection : str, default="max_average_difference"
479
+ Method for selecting labels for samples designated as non-self by all detectors.
480
+ Available method types:
481
+
482
+ - max_average_difference - Selects the class with the highest average difference among the
483
+ detectors.
484
+
485
+ - max_nearest_difference - Selects the class with the highest difference between the
486
+ nearest and farthest detector from the sample.
461
487
  """
462
488
 
463
489
  def __init__(
@@ -470,8 +496,6 @@ class BNSA(BaseNSA):
470
496
  "max_average_difference", "max_nearest_difference"
471
497
  ] = "max_average_difference",
472
498
  ):
473
- super().__init__()
474
-
475
499
  self.N: int = sanitize_param(N, 100, lambda x: x > 0)
476
500
  self.aff_thresh: float = sanitize_param(aff_thresh, 0.1, lambda x: 0 < x < 1)
477
501
  self.max_discards: float = sanitize_param(max_discards, 1000, lambda x: x > 0)
@@ -497,22 +521,22 @@ class BNSA(BaseNSA):
497
521
  return self._detectors
498
522
 
499
523
  def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
500
- """
501
- The function ``fit(...)``, performs the training according to ``X`` and ``y``, using the
502
- method negative selection method(``NegativeSelect``).
524
+ """Training according to X and y, using the method negative selection method.
503
525
 
504
526
  Parameters
505
527
  ----------
506
- * X (``npt.NDArray``):
507
- Training array, containing the samples and their characteristics,
508
- [``N samples`` (rows)][``N features`` (columns)].
509
- * y (``npt.NDArray``):
528
+ X : npt.NDArray
529
+ Training array, containing the samples and their characteristics, [``N samples`` (
530
+ rows)][``N features`` (columns)].
531
+ y : npt.NDArray
510
532
  Array of target classes of ``X`` with [``N samples`` (lines)].
511
- verbose (``bool``): Feedback from detector generation to the user.
533
+ verbose : bool, default=True
534
+ Feedback from detector generation to the user.
512
535
 
513
536
  Returns
514
- ----------
515
- * (``self``): Returns the instance itself.
537
+ -------
538
+ self : BNSA
539
+ Returns the instance it self.
516
540
  """
517
541
  super()._check_and_raise_exceptions_fit(X, y, "BNSA")
518
542
 
@@ -574,20 +598,19 @@ class BNSA(BaseNSA):
574
598
  return self
575
599
 
576
600
  def predict(self, X: npt.NDArray) -> Optional[npt.NDArray]:
577
- """
578
- Function to perform the prediction of classes based on detectors
579
- created after training.
601
+ """Prediction of classes based on detectors created after training.
580
602
 
581
603
  Parameters
582
604
  ----------
583
- * X (``npt.NDArray``): Array with input samples with [``N samples`` (Lines)] and
584
- [``N characteristics``(Columns)]
605
+ X : npt.NDArray
606
+ Array with input samples with [``N_samples`` (Lines)] and [``N_characteristics``(
607
+ Columns)]
585
608
 
586
609
  Returns
587
- ----------
588
- * c (``npt.NDArray``): an ndarray of the form ``C`` [``N samples``],
589
- containing the predicted classes for ``X``
590
- * ``None``: If there are no detectors for the prediction.
610
+ -------
611
+ c : Optional[npt.NDArray]
612
+ an ndarray of the form ``C`` [``N samples``], containing the predicted classes for
613
+ ``X``. Returns``None``: If there are no detectors for the prediction.
591
614
  """
592
615
  # If there are no detectors, Returns None.
593
616
  if self._detectors is None:
@@ -629,19 +652,17 @@ class BNSA(BaseNSA):
629
652
  return np.array(c)
630
653
 
631
654
  def __assign_class_to_non_self_sample(self, line: npt.NDArray, c: list):
632
- """
633
- This function determines the class of a sample when all detectors classify it
634
- as "non-self". Classification is performed using the ``max_average_difference``
635
- and ``max_nearest_difference`` methods.
655
+ """Determine the class of a sample when all detectors classify it as "non-self".
656
+
657
+ Classification is performed using the ``max_average_difference`` and
658
+ ``max_nearest_difference`` methods.
636
659
 
637
660
  Parameters
638
661
  ----------
639
- * line (list): Sample to be classified.
640
- * c (list): List of predictions to be updated with the new classification.
641
-
642
- Returns
643
- ----------
644
- * list: The list of predictions `c` updated with the class assigned to the sample.
662
+ line : npt.NDArray
663
+ Sample to be classified.
664
+ c : list
665
+ List of predictions to be updated with the new classification.
645
666
  """
646
667
  class_differences: dict = {}
647
668
  for _class_ in self.classes: