Skip to content

Commit b11d4e7

Browse files
Rename folder
1 parent b705536 commit b11d4e7

45 files changed

Lines changed: 2223 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
last_update:
3+
date: 2025/08/28
4+
author: João Paulo
5+
---
6+
7+
# Distance
8+
9+
Utility functions for normalized distance between arrays with numba decorators.
10+
11+
## def hamming(...)
12+
13+
```python
14+
def hamming(u: npt.NDArray, v: npt.NDArray) -> np.float64:
15+
```
16+
17+
The function to calculate the normalized Hamming distance between two points.
18+
19+
$((x₁ ≠ x₂) + (y₁ ≠ y₂) + ... + (yn ≠ yn)) / n$
20+
21+
22+
**Parameters:**
23+
* u (``npt.NDArray``): Coordinates of the first point.
24+
* v (``npt.NDArray``): Coordinates of the second point.
25+
26+
**Returns:**
27+
* Distance (``float``) between the two points.
28+
29+
---
30+
31+
## def euclidean(...)
32+
33+
```python
34+
def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.float64:
35+
```
36+
37+
Function to calculate the normalized Euclidean distance between two points.
38+
39+
$√( (x₁ – x₂)² + (y₁ – y₂)² + ... + (yn – yn)²)$
40+
41+
42+
43+
**Parameters:**
44+
* u (``npt.NDArray``): Coordinates of the first point.
45+
* v (``npt.NDArray``): Coordinates of the second point.
46+
47+
**Returns:**
48+
* Distance (``float``) between the two points.
49+
50+
---
51+
52+
## def cityblock(...)
53+
54+
```python
55+
def cityblock(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.float64:
56+
```
57+
58+
Function to calculate the normalized Manhattan distance between two points.
59+
60+
$(|x₁ – x₂| + |y₁ – y₂| + ... + |yn – yn|) / n$
61+
62+
63+
**Parameters:**
64+
* u (``npt.NDArray``): Coordinates of the first point.
65+
* v (``npt.NDArray``): Coordinates of the second point.
66+
67+
**Returns:**
68+
* Distance (``float``) between the two points.
69+
70+
---
71+
72+
## def minkowski(...)
73+
74+
```python
75+
def minkowski(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64], p: float = 2.0):
76+
```
77+
78+
Function to calculate the normalized Minkowski distance between two points.
79+
80+
$(( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ) / n$
81+
82+
83+
**Parameters:**
84+
* u (``npt.NDArray``): Coordinates of the first point.
85+
* v (``npt.NDArray``): Coordinates of the second point.
86+
* p float: The p parameter defines the type of distance to be calculated:
87+
- p = 1: **Manhattan** distance — sum of absolute differences.
88+
- p = 2: **Euclidean** distance — sum of squared differences (square root).
89+
- p > 2: **Minkowski** distance with an increasing penalty as p increases.
90+
91+
**Returns:**
92+
* Distance (``float``) between the two points.
93+
94+
---
95+
96+
## def compute_metric_distance(...)
97+
98+
```python
99+
def compute_metric_distance(
100+
u: npt.NDArray[np.float64],
101+
v: npt.NDArray[np.float64],
102+
metric: int,
103+
p: np.float64 = 2.0
104+
) -> np.float64:
105+
```
106+
107+
Function to calculate the distance between two points by the chosen ``metric``.
108+
109+
**Parameters:**
110+
* u (``npt.NDArray``): Coordinates of the first point.
111+
* v (``npt.NDArray``): Coordinates of the second point.
112+
* metric (``int``): Distance metric to be used. Available options: [0 (Euclidean), 1 (Manhattan), 2 (Minkowski)]
113+
* p (``float``): Parameter for the Minkowski distance (used only if `metric` is "minkowski").
114+
115+
**Returns:**
116+
* Distance (``double``) between the two points with the selected metric.
117+
118+
---
119+
120+
## def min_distance_to_class_vectors(...)
121+
122+
```python
123+
def min_distance_to_class_vectors(
124+
x_class: npt.NDArray,
125+
vector_x: npt.NDArray,
126+
metric: int,
127+
p: float = 2.0
128+
) -> float:
129+
```
130+
131+
Calculates the minimum distance between an input vector and the vectors of a class.
132+
133+
134+
**Parameters:**
135+
* x_class (``npt.NDArray``): Array containing the class vectors to be compared with the input vector. Expected shape: (n_samples, n_features).
136+
* vector_x (``npt.NDArray``): Vector to be compared with the class vectors. Expected shape: (n_features,).
137+
* metric (``int``): Distance metric to be used. Available options: [0 (Euclidean), 1 (Manhattan), 2 (Minkowski)]
138+
* p (``float``): Parameter for the Minkowski distance (used only if `metric` is "minkowski").
139+
140+
**Returns:**
141+
* float: The minimum distance calculated between the input vector and the class vectors.
142+
* Returns -1.0 if the input dimensions are incompatible.
143+
144+
---
145+
146+
## def get_metric_code(...)
147+
148+
```python
149+
def get_metric_code(metric: str) -> int:
150+
```
151+
Returns the numeric code associated with a distance metric.
152+
153+
**Parameters:**
154+
* metric (str): Name of the metric. Can be "euclidean", "manhattan", "minkowski" or "hamming".
155+
156+
**Raises**
157+
----------
158+
* ``ValueError``: If the metric provided is not supported
159+
160+
**Returns:**
161+
* ``int``: Numeric code corresponding to the metric.
162+
163+
---
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
sidebar_position: 1
3+
title: Metrics
4+
sidebar_label: Metrics
5+
lastUpdatedAt: 2025/04/04
6+
author: João Paulo
7+
---
8+
9+
The metrics file provides utilities to measure, analyze, and compare the performance of the package's algorithms in a standardized way.
10+
11+
#### def accuracy_score(...)
12+
13+
```python
14+
def accuracy_score(
15+
y_true: Union[npt.NDArray, list],
16+
y_pred: Union[npt.NDArray, list]
17+
) -> float
18+
```
19+
20+
Function to calculate precision accuracy based on lists of true labels and
21+
predicted labels.
22+
23+
**Parameters**:
24+
* **_y_true_** (``Union[npt.NDArray, list]``): Ground truth (correct) labels.
25+
Expected to be of the same length as `y_pred`.
26+
* **_y_pred_** (``Union[npt.NDArray, list]``): Predicted labels. Expected to
27+
be of the same length as `y_true`.
28+
29+
Returns:
30+
* **_Accuracy_** (``float``): The ratio of correct predictions to the total
31+
number of predictions.
32+
33+
**Raises**:
34+
* `ValueError`: If `y_true` or `y_pred` are empty or if they do not have the same length.
35+
36+
---
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
sidebar_position: 1
3+
title: Multiclass
4+
sidebar_label: Multiclass
5+
lastUpdatedAt: 2025/04/04
6+
author: João Paulo
7+
---
8+
9+
This file contains internal utility functions designed to simplify data manipulation and processing in multiclass classification scenarios within the AISP package.
10+
11+
### def slice_index_list_by_class(...)
12+
13+
```python
14+
def slice_index_list_by_class(classes: Union[npt.NDArray, list], y: npt.NDArray) -> dict
15+
```
16+
17+
The function ``slice_index_list_by_class(...)``, separates the indices of the lines \
18+
according to the output class, to loop through the sample array, only in positions where \
19+
the output is the class being trained.
20+
21+
**Parameters**:
22+
* ***classes*** (``list or npt.NDArray``): list with unique classes.
23+
* ***y*** (npt.NDArray): Receives a ``y``[``N sample``] array with the output classes of the ``X`` sample array.
24+
25+
**returns**:
26+
* dict: A dictionary with the list of array positions(``y``), with the classes as key.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
last_update:
3+
date: 2025/05/17
4+
author: João Paulo
5+
---
6+
7+
# Sanitizers
8+
9+
## def sanitize_choice(...)
10+
11+
```python
12+
def sanitize_choice(value: T, valid_choices: Iterable[T], default: T) -> T
13+
```
14+
15+
The function ``sanitize_choice(...)``, returns the value if it is present in the set of valid choices; otherwise, returns the default value.
16+
17+
18+
**Parameters:**
19+
* ***value*** (``T``): The value to be checked.
20+
* ***valid_choices*** (``Iterable[T]``): A collection of valid choices.
21+
* ***default***: The default value to be returned if ``value`` is not in ``valid_choices``.
22+
23+
24+
**Returns:**
25+
* `T`: The original value if valid, or the default value if not.
26+
27+
---
28+
29+
## def sanitize_param(...)
30+
31+
```python
32+
def sanitize_param(value: T, default: T, condition: Callable[[T], bool]) -> T:
33+
```
34+
35+
The function ``sanitize_param(...)``, returns the value if it satisfies the specified condition; otherwise, returns the default value.
36+
37+
**Parameters:**
38+
* value (``T``): The value to be checked.
39+
* default (``T``): The default value to be returned if the condition is not satisfied.
40+
* condition (``Callable[[T], bool]``): A function that takes a value and returns a boolean, determining if the value is valid.
41+
42+
43+
**Returns:**
44+
* `T`: The original value if the condition is satisfied, or the default value if not.
45+
46+
---
47+
48+
## def sanitize_seed(...)
49+
50+
```python
51+
def sanitize_seed(seed: Any) -> Optional[int]:
52+
```
53+
54+
The function ``sanitize_param(...)``, returns the seed if it is a non-negative integer; otherwise, returns None.
55+
56+
**Parameters:**
57+
* seed (``Any``): The seed value to be validated.
58+
59+
**Returns:**
60+
* ``Optional[int]``: The original seed if it is a non-negative integer, or ``None`` if it is invalid.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Validation
2+
3+
## def detect_vector_data_type(...)
4+
5+
```python
6+
def detect_vector_data_type(
7+
vector: npt.NDArray
8+
) -> FeatureType:
9+
```
10+
11+
Detects the type of data in a given vector.
12+
13+
This function analyzes the input vector and classifies its data as one of the supported types:
14+
15+
* **binary**: Boolean values (`True`/`False`) or integer `0`/`1`.
16+
* **continuous**: Float values within the normalized range `[0.0, 1.0]`.
17+
* **ranged**: Float values outside the normalized range.
18+
19+
**Parameters**
20+
21+
* `vector` (`npt.NDArray`): An array containing the data to be classified.
22+
23+
**Returns**
24+
* `FeatureType` (`Literal["binary-features", "continuous-features", "ranged-features"]`): The detected type of data in the vector.
25+
26+
**Raises**
27+
* `UnsupportedDataTypeError`: Raised if the vector contains an unsupported data type.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Utils",
3+
"description": "Utility functions and helpers for development."
4+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
sidebar_position: 1
3+
lastUpdatedAt: 2025/05/25
4+
author: João Paulo
5+
showLastUpdateAuthor: true
6+
showLastUpdateTime: true
7+
keywords:
8+
- Binary
9+
- classifying
10+
- anomalies
11+
- not self
12+
- affinity threshold
13+
- Negative Selection Algorithm
14+
- Artificial Immune System (AIS)
15+
- Self and non-self
16+
- Immune
17+
- Computação Natural
18+
- mushrooms dataset
19+
- iris dataset
20+
- geyser dataset
21+
---
22+
23+
import DocCardList from '@theme/DocCardList';
24+
25+
# Classification
26+
27+
Access the notebooks with the option to run them online using Binder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/AIS-Package/aisp/HEAD?labpath=%2Fexamples%2Fen%2Fclassification)
28+
29+
---
30+
31+
## The examples are organized below:
32+
33+
### Data Normalization:
34+
> Shows how to normalize data using negative selection classes. In the real-valued version, the data is normalized between 0 and 1. In the binary version, it is normalized into a bit vector.
35+
36+
### K-fold Cross Validation with 50 Interactions:
37+
> In this example, the data is divided into training and test sets and model performance is evaluated by cross-validation. So with dividing the training data into k parts. In each iteration, 10% of the training data is reserved for testing.
38+
39+
### Training:
40+
> The trained model is tested in this example with all available training data.
41+
42+
The examples below show various functionality of negative selection classes so that you know how to use them in your project. Feel free to explore these examples and adapt them as needed to meet your specific needs.
43+
44+
## Examples:
45+
46+
<DocCardList />

0 commit comments

Comments
 (0)