Skip to main content
Version: 0.5.x

AiNet

Artificial Immune Network (AiNet) for Compression and Clustering.

Inheritance

This class extends BaseClusterer.

Module: aisp.ina
Import: from aisp.ina import AiNet


Overview

This class implements the aiNet algorithm, an artificial immune network model designed for clustering and data compression tasks. The aiNet algorithm uses principles from immune network theory, clonal selection, and affinity maturation to compress high-dimensional datasets 1.
For clustering, the class uses SciPy implementation of the Minimum Spanning Tree (MST) to remove the most distant nodes and separate the groups 2.


Example

import numpy as np
from aisp.ina import AiNet

np.random.seed(1)
# Generating training data
a = np.random.uniform(high=0.4, size=(50, 2))
b = np.random.uniform(low=0.6, size=(50, 2))
x_train = np.vstack((a, b))
# AiNet Instance
ai_net = AiNet(
N=150,
mst_inconsistency_factor=1,
seed=1,
affinity_threshold=0.85,
suppression_threshold=0.7
)
ai_net = ai_net.fit(x_train, verbose=True)
x_test = [
[0.15, 0.45], # Expected: label 0
[0.85, 0.65], # Expected: label 1
]
y_pred = ai_net.predict(x_test)
print(y_pred)

Output

[0 1]

Constructor Parameters

NameTypeDefaultDescription
Nint50Number of memory cells (antibodies) in the population.
n_cloneint10Number of clones generated for each selected memory cell.
top_clonal_memory_sizeint5Number of highest-affinity antibodies selected per antigen for cloning and mutation.
n_diversity_injectionint5Number of new random memory cells injected to maintain diversity.
affinity_thresholdfloat0.5Threshold for affinity (similarity) to determine cell suppression or selection.
suppression_thresholdfloat0.5Threshold for suppressing similar memory cells.
mst_inconsistency_factorfloat2.0Factor used to determine which edges in the Minimum Spanning Tree (MST) are considered inconsistent.
max_iterationsint10Maximum number of training iterations.
kint3The number of K nearest neighbors that will be used to choose a label in the prediction.
metricMetricType"euclidean"Distance metric used to compute similarity between memory cells.
seedOptional[int]NoneSeed for random generation.
use_mst_clusteringboolTrueIf True, performs clustering using the MST. If False, clustering is not performed and the predict method raises a ModelNotFittedError.
pfloat2.0This parameter stores the value of p used in the Minkowski distance.

Attributes

NameTypeDefaultDescription
memory_networkDict[int, List[Cell]]-The immune network representing clusters.
population_antibodiesOptional[npt.NDArray]-The set of memory antibodies.
mstdict-The Minimum Spanning Tree and its statistics (graph, mean_distance, std_distance).

Public Methods

fit

def fit(self, X: Union[npt.NDArray, list], verbose: bool = True) -> AiNet:
...

Train the AiNet model on input data.

Parameters

NameTypeDefaultDescription
XUnion[npt.NDArray, list]-Training input samples. Each row corresponds to a samples and column to feature.
verboseboolTrueIf True, prints training progress information.

Returns

TypeDescription
SelfReturns the instance itself.

Raises

ExceptionDescription
TypeErrorIf X is not a ndarray or list.
UnsupportedTypeErrorIf the data type of the feature on X is not supported.

predict

def predict(self, X: Union[npt.NDArray, list]) -> npt.NDArray:
...

Predict cluster labels for input data.

Parameters

NameTypeDefaultDescription
XUnion[npt.NDArray, list]-Input samples. Must have the same number of features used during training.

Returns

TypeDescription
npt.NDArrayPredicted cluster labels, or None if clustering is disabled.

Raises

ExceptionDescription
TypeErrorIf X is not a ndarray or list.
ValueErrorIf X contains values other than (0 and 1) or (True and False) when the trained features are of type "binary-features".
FeatureDimensionMismatchIf the number of features (columns) in X does not match the expected number.
ModelNotFittedErrorIf the mode has not yet been adjusted and does not have defined memory cells, it is not able to predictions.

update_clusters

def update_clusters(self, mst_inconsistency_factor: Optional[float] = None):
...

Partition the clusters based on the MST inconsistency factor.

Uses the precomputed Minimum Spanning Tree (MST) of the antibody population to redefine clusters. Edges whose weights exceed the mean plus the mst_inconsistency_factor multiplied by the standard deviation of MST edge weights are removed. Each connected component after pruning is treated as a distinct cluster.

Parameters

NameTypeDefaultDescription
mst_inconsistency_factorfloatNoneIf provided, overrides the current inconsistency factor.

Raises

ExceptionDescription
ValueErrorIf the Minimum Spanning Tree (MST) has not yet been created.
ValueErrorIf Population of antibodies is empty.
ValueErrorIf MST statistics (mean or std) are not available.

Updates

NameTypeDescription
memory_networkdict[int, npt.NDArray]Dictionary mapping cluster labels to antibody arrays.
labelslistList of cluster labels.

Extended Example

Complete usage examples are available in the Jupyter Notebooks:


References

Footnotes

  1. De Castro, Leandro & José, Fernando & von Zuben, Antonio Augusto. (2001). aiNet: An Artificial Immune Network for Data Analysis. Available at: https://www.researchgate.net/publication/228378350_aiNet_An_Artificial_Immune_Network_for_Data_Analysis

  2. SciPy Documentation. Minimum Spanning Tree. https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.minimum_spanning_tree