ANFIS Models¶
anfis_toolbox.model.TSKANFIS ¶
TSKANFIS(
input_mfs: dict[str, list[MembershipFunction]],
rules: Sequence[Sequence[int]] | None = None,
)
Bases: _TSKANFISSharedMixin
Adaptive Neuro-Fuzzy Inference System (legacy TSK ANFIS) model.
Implements the classic 4-layer ANFIS architecture:
1) MembershipLayer — fuzzification of inputs 2) RuleLayer — rule strength computation (T-norm) 3) NormalizationLayer — weight normalization 4) ConsequentLayer — final output via a TSK model
Supports forward/backward passes for training, parameter access/update, and a simple prediction API.
Attributes:
| Name | Type | Description |
|---|---|---|
input_mfs |
dict[str, list[MembershipFunction]]
|
Mapping from input name to its list of membership functions. |
membership_layer |
MembershipLayer
|
Layer 1 — fuzzification. |
rule_layer |
RuleLayer
|
Layer 2 — rule strength computation. |
normalization_layer |
NormalizationLayer
|
Layer 3 — weight normalization. |
consequent_layer |
ConsequentLayer
|
Layer 4 — final TSK output. |
input_names |
list[str]
|
Ordered list of input variable names. |
n_inputs |
int
|
Number of input variables (features). |
n_rules |
int
|
Number of fuzzy rules used by the system. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_mfs
|
dict[str, list[MembershipFunction]]
|
Mapping from input
name to a list of membership functions. Example:
|
required |
rules
|
Sequence[Sequence[int]] | None
|
Optional explicit set of rules, each specifying one membership index per
input. When |
None
|
Examples:
>>> from anfis_toolbox.membership import GaussianMF
>>> input_mfs = {
... 'x1': [GaussianMF(0, 1), GaussianMF(1, 1)],
... 'x2': [GaussianMF(0, 1), GaussianMF(1, 1)]
... }
>>> model = ANFIS(input_mfs)
Source code in anfis_toolbox/model.py
__repr__ ¶
backward ¶
Run a backward pass through all layers.
Propagates gradients from the output back through all layers and stores parameter gradients for a later update step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dL_dy
|
ndarray
|
Gradient of the loss w.r.t. the model output,
shape |
required |
Source code in anfis_toolbox/model.py
fit ¶
fit(
x: ndarray,
y: ndarray,
epochs: int = 100,
learning_rate: float = 0.01,
verbose: bool = False,
trainer: TrainerLike | None = None,
*,
validation_data: tuple[ndarray, ndarray] | None = None,
validation_frequency: int = 1,
) -> TrainingHistory
Train the ANFIS model.
If a trainer is provided (see anfis_toolbox.optim), delegate training
to it while preserving a scikit-learn-style fit(X, y) entry point. If
no trainer is provided, a default HybridTrainer is used with the given
hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Training inputs of shape |
required |
y
|
ndarray
|
Training targets of shape |
required |
epochs
|
int
|
Number of epochs. Defaults to |
100
|
learning_rate
|
float
|
Learning rate. Defaults to |
0.01
|
verbose
|
bool
|
Whether to log progress. Defaults to |
False
|
trainer
|
TrainerLike | None
|
External trainer implementing
|
None
|
validation_data
|
tuple[ndarray, ndarray] | None
|
Optional
validation inputs and targets evaluated according to |
None
|
validation_frequency
|
int
|
Evaluate validation loss every N epochs. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
TrainingHistory |
TrainingHistory
|
Dictionary with |
Source code in anfis_toolbox/model.py
forward ¶
Run a forward pass through the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Output array of shape |
Source code in anfis_toolbox/model.py
forward_antecedents ¶
Run a forward pass through the antecedent layers only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Normalized rule weights of shape |
Source code in anfis_toolbox/model.py
forward_consequents ¶
Run a forward pass through the consequent layer only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input array of shape |
required |
normalized_weights
|
ndarray
|
Normalized rule weights of shape
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Output array of shape |
Source code in anfis_toolbox/model.py
predict ¶
Predict using the current model parameters.
Accepts Python lists, 1D or 2D arrays and coerces to the expected shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray | list[float]
|
Input data. If 1D, must have
exactly |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Predictions of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input dimensionality or feature count does not match the model configuration. |
Source code in anfis_toolbox/model.py
anfis_toolbox.model.TSKANFISClassifier ¶
TSKANFISClassifier(
input_mfs: dict[str, list[MembershipFunction]],
n_classes: int,
random_state: int | None = None,
rules: Sequence[Sequence[int]] | None = None,
)
Bases: _TSKANFISSharedMixin
Adaptive Neuro-Fuzzy classifier with a softmax head (TSK variant).
Aggregates per-rule linear consequents into per-class logits and trains with cross-entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_mfs
|
dict[str, list[MembershipFunction]]
|
Mapping from input variable name to its list of membership functions. |
required |
n_classes
|
int
|
Number of output classes (>= 2). |
required |
random_state
|
int | None
|
Optional random seed for parameter init. |
None
|
rules
|
Sequence[Sequence[int]] | None
|
Optional explicit rule definitions
where each inner sequence lists the membership-function index per input.
When |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Attributes:
| Name | Type | Description |
|---|---|---|
input_mfs |
dict[str, list[MembershipFunction]]
|
Membership functions per input. |
input_names |
list[str]
|
Input variable names. |
n_inputs |
int
|
Number of input variables. |
n_classes |
int
|
Number of classes. |
n_rules |
int
|
Number of fuzzy rules (product of MFs per input). |
membership_layer |
MembershipLayer
|
Computes membership degrees. |
rule_layer |
RuleLayer
|
Evaluates rule activations. |
normalization_layer |
NormalizationLayer
|
Normalizes rule strengths. |
consequent_layer |
ClassificationConsequentLayer
|
Computes class logits. |
Source code in anfis_toolbox/model.py
__repr__ ¶
Return a string representation of the ANFISClassifier.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A formatted string describing the classifier configuration. |
Source code in anfis_toolbox/model.py
backward ¶
Backpropagate gradients through all layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dL_dlogits
|
ndarray
|
Gradient of the loss w.r.t. logits,
shape |
required |
Source code in anfis_toolbox/model.py
fit ¶
fit(
X: ndarray,
y: ndarray,
epochs: int = 100,
learning_rate: float = 0.01,
verbose: bool = False,
trainer: TrainerLike | None = None,
loss: LossFunction | str | None = None,
*,
validation_data: tuple[ndarray, ndarray] | None = None,
validation_frequency: int = 1,
) -> TrainingHistory
Fits the ANFIS model to the provided training data using the specified optimization strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input features for training. |
required |
y
|
ndarray
|
Target values for training. |
required |
epochs
|
int
|
Number of training epochs. Defaults to 100. |
100
|
learning_rate
|
float
|
Learning rate for the optimizer. Defaults to 0.01. |
0.01
|
verbose
|
bool
|
If True, prints training progress. Defaults to False. |
False
|
trainer
|
TrainerLike | None
|
Custom trainer instance. If None, uses AdamTrainer. Defaults to None. |
None
|
loss
|
LossFunction, str, or None
|
Loss function to use. If None, defaults to cross-entropy for classification. |
None
|
validation_data
|
tuple[ndarray, ndarray] | None
|
Optional validation dataset. |
None
|
validation_frequency
|
int
|
Evaluate validation metrics every N epochs. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
TrainingHistory |
TrainingHistory
|
Dictionary containing |
Source code in anfis_toolbox/model.py
forward ¶
Run a forward pass through the classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Logits of shape |
Source code in anfis_toolbox/model.py
predict ¶
Predict the most likely class label for each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray | list[float]
|
Inputs. If 1D, must have exactly
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Predicted labels of shape |
Source code in anfis_toolbox/model.py
predict_proba ¶
Predict per-class probabilities for the given inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray | list[float]
|
Inputs. If 1D, must have exactly
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Probabilities of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input dimensionality or feature count is invalid. |