xanfis.models package

xanfis.models.base_anfis module

class xanfis.models.base_anfis.BaseAnfis(num_rules, mf_class, task='classification', vanishing_strategy='prod', act_output=None, reg_lambda=None, seed=None)[source]

Bases: BaseEstimator

BaseAnfis is a scikit-learn style base class for managing and training an Adaptive Neuro-Fuzzy Inference System (ANFIS) model.

This class provides a high-level interface for building and evaluating ANFIS models using PyTorch while retaining compatibility with scikit-learn-style APIs (e.g., fit, predict, score). It includes functionality for saving/loading models, metrics evaluation, and logging training results.

Parameters:
  • num_rules (int) – Number of fuzzy rules in the ANFIS model.

  • mf_class (str or object) – The membership function class to use. Can be a string name of a predefined MF type or a custom MF instance.

  • task (str, optional (default="classification")) – Type of supervised learning task. One of {“classification”, “binary_classification”, “regression”}.

  • vanishing_strategy (str or None, optional) – Strategy to handle vanishing gradients when combining membership values. Can be one of {“prod”, “mean”, “blend”}. default=’prod’)

  • act_output (str or None, optional) – Output activation function. If None, will be inferred based on task type.

  • reg_lambda (float or None, optional (default=0.0)) – Regularization term used when solving for consequent parameters via least squares.

  • seed (int or None, optional) – Random seed for reproducibility.

network

The underlying ANFIS model instance. Must be assigned by the subclass or during training.

Type:

torch.nn.Module or None

loss_train

A list to store training loss per epoch (if tracking is implemented).

Type:

list or None

SUPPORTED_CLS_METRICS

List of supported classification evaluation metrics.

Type:

list of str

SUPPORTED_REG_METRICS

List of supported regression evaluation metrics.

Type:

list of str

Notes

  • This class is designed to be inherited and extended. The core methods fit, predict, and score must be implemented in a subclass or concrete version.

  • Helper methods for saving/loading models and logging training history are included.

  • The evaluate method provides metric evaluation for classification and regression tasks.

SUPPORTED_CLS_METRICS = {'AS': 'max', 'BSL': 'min', 'CEL': 'min', 'CKS': 'max', 'F1S': 'max', 'F2S': 'max', 'FBS': 'max', 'GINI': 'min', 'GMS': 'max', 'HL': 'min', 'HS': 'max', 'JSI': 'max', 'KLDL': 'min', 'LS': 'max', 'MCC': 'max', 'NPV': 'max', 'PS': 'max', 'ROC-AUC': 'max', 'RS': 'max', 'SS': 'max'}
SUPPORTED_REG_METRICS = {'A10': 'max', 'A20': 'max', 'A30': 'max', 'ACOD': 'max', 'APCC': 'max', 'AR': 'max', 'AR2': 'max', 'CI': 'max', 'COD': 'max', 'COR': 'max', 'COV': 'max', 'CRM': 'min', 'DRV': 'min', 'EC': 'max', 'EVS': 'max', 'GINI': 'min', 'GINI_WIKI': 'min', 'JSD': 'min', 'KGE': 'max', 'MAAPE': 'min', 'MAE': 'min', 'MAPE': 'min', 'MASE': 'min', 'ME': 'min', 'MRB': 'min', 'MRE': 'min', 'MSE': 'min', 'MSLE': 'min', 'MedAE': 'min', 'NNSE': 'max', 'NRMSE': 'min', 'NSE': 'max', 'OI': 'max', 'PCC': 'max', 'PCD': 'max', 'R': 'max', 'R2': 'max', 'R2S': 'max', 'RAE': 'min', 'RMSE': 'min', 'RSE': 'min', 'RSQ': 'max', 'SMAPE': 'min', 'VAF': 'max', 'WI': 'max'}
evaluate(y_true, y_pred, list_metrics=None)[source]

Evaluate the model using specified metrics.

Parameters:
  • y_true (array-like) – True target values.

  • y_pred (array-like) – Model’s predicted values.

  • list_metrics (list of str, optional) – Names of metrics for evaluation (e.g., “MSE”, “MAE”).

Returns:

Evaluation metrics and their values.

Return type:

dict

fit(X, y)[source]

Train the ANFIS model on the given dataset.

Parameters:
  • X (array-like or torch.Tensor) – Training features.

  • y (array-like or torch.Tensor) – Target values.

static load_model(load_path='history', filename='model.pkl') EstimatorType[source]

Load a model from a pickle file.

Parameters:
  • load_path (str, optional) – Path to load the model from (default: “history”).

  • filename (str, optional) – Filename of the saved model (default: “model.pkl”).

Returns:

The loaded model.

Return type:

BaseAnfis

predict(X)[source]

Generate predictions for input data using the trained model.

Parameters:

X (array-like or torch.Tensor) – Input features for prediction.

Returns:

Model predictions for each input sample.

Return type:

array-like or torch.Tensor

save_evaluation_metrics(y_true, y_pred, list_metrics=('RMSE', 'MAE'), save_path='history', filename='metrics.csv')[source]

Save evaluation metrics to a CSV file.

Parameters:
  • y_true (array-like) – Ground truth values.

  • y_pred (array-like) – Model predictions.

  • list_metrics (list of str, optional) – Metrics for evaluation (default: (“RMSE”, “MAE”)).

  • save_path (str, optional) – Path to save the file (default: “history”).

  • filename (str, optional) – Filename for saving metrics (default: “metrics.csv”).

save_model(save_path='history', filename='model.pkl')[source]

Save the trained model to a pickle file.

Parameters:
  • save_path (str, optional) – Path to save the model (default: “history”).

  • filename (str, optional) – Filename for saving model, with “.pkl” extension (default: “model.pkl”).

save_training_loss(save_path='history', filename='loss.csv')[source]

Save training loss history to a CSV file.

Parameters:
  • save_path (str, optional) – Path to save the file (default: “history”).

  • filename (str, optional) – Filename for saving loss history (default: “loss.csv”).

save_y_predicted(X, y_true, save_path='history', filename='y_predicted.csv')[source]

Save true and predicted values to a CSV file.

Parameters:
  • X (array-like or torch.Tensor) – Input features.

  • y_true (array-like) – True values.

  • save_path (str, optional) – Path to save the file (default: “history”).

  • filename (str, optional) – Filename for saving predicted values (default: “y_predicted.csv”).

score(X, y)[source]

Evaluate the model on the given dataset.

Parameters:
  • X (array-like or torch.Tensor) – Evaluation features.

  • y (array-like or torch.Tensor) – True values.

Returns:

The accuracy or evaluation score.

Return type:

float

set_seed(seed)[source]

Set the random seed for the model to ensure reproducibility.

Parameters:

seed (int, None) – The seed value to use for random number generators within the model.

Notes

  • This method stores the seed value in the self.seed attribute.

  • Setting a seed helps achieve reproducible results, especially in training neural networks where randomness affects initialization and other stochastic operations.

class xanfis.models.base_anfis.BaseBioAnfis(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, optim='BaseGA', optim_params=None, obj_name=None, seed=42, verbose=True, lb=None, ub=None, mode='single', n_workers=None, termination=None)[source]

Bases: BaseAnfis

Base class for biologically-inspired ANFIS models using metaheuristic optimization.

This class serves as a base for integrating Adaptive Neuro-Fuzzy Inference Systems (ANFIS) with metaheuristic algorithms (e.g., Genetic Algorithm, PSO, etc.) to optimize membership function parameters and rule weights. The consequent parameters are learned using Least Squares Estimation (LSE) in a hybrid-learning manner.

Parameters:
  • num_rules (int, optional) – Number of fuzzy rules in the ANFIS model (default is 10).

  • mf_class (str, optional) – Type of membership function to use (e.g., “Gaussian”, “Triangular”) (default is “Gaussian”).

  • vanishing_strategy (str or None, optional) – Strategy to handle vanishing gradients when combining membership values. Can be one of {“prod”, “mean”, “blend”} (default is “prod”).

  • act_output (any, optional) – Activation function to apply to the output layer (default is None).

  • reg_lambda (float or None, optional) – L2 regularization strength for least squares estimation (default is None).

  • optim (str, optional) – Name of the metaheuristic optimizer to use (default is “BaseGA”).

  • optim_params (dict, optional) – Dictionary of hyperparameters for the optimizer (default is None).

  • obj_name (str, optional) – Name of the objective function for optimization (default is None).

  • seed (int, optional) – Random seed for reproducibility (default is 42).

  • verbose (bool, optional) – Whether to print logs during training (default is True).

  • lb (int, float, list, tuple, np.ndarray, optional.) – Lower bounds for optimization (default is (-1.0,)).

  • ub (int, float, list, tuple, np.ndarray, optional.) – Upper bounds for optimization (default is (1.0,)).

  • mode (str, optional) – Mode for optimization (default is ‘single’).

  • n_workers (int, optional) – Number of workers for parallel processing (default is None).

  • termination (any, optional) – Termination criteria for optimization (default is None).

SUPPORTED_OPTIMIZERS

List of supported optimizer names from the Mealpy library.

Type:

list of str

SUPPORTED_CLS_OBJECTIVES

Dictionary of supported classification metrics from the permetrics library.

Type:

dict

SUPPORTED_REG_OBJECTIVES

Dictionary of supported regression metrics from the permetrics library.

Type:

dict

optim

Name of the metaheuristic optimizer used.

Type:

str

optim_params

Hyperparameters for the selected optimizer.

Type:

dict

verbose

Whether to print logs during training.

Type:

bool

size_input

Number of input features (set during model build).

Type:

int

size_output

Number of output neurons (set during model build).

Type:

int

network

The core ANFIS network module.

Type:

CustomANFIS

optimizer

Metaheuristic optimizer instance.

Type:

Optimizer

obj_name

Name of the optimization objective function.

Type:

str

metric_class

Metric computation class for evaluating performance.

Type:

permetrics object

set_optim_and_paras(optim, optim_params)[source]

Sets the optimizer name and parameters.

build_model()[source]

Constructs the ANFIS network and optimizer instance.

get_name()[source]

Returns the name of the model based on optimizer settings.

_set_optimizer(optim, optim_params)[source]

Internal method to initialize the optimizer.

_set_lb_ub(lb, ub, n_dims)[source]

Validates and formats lower and upper bounds for optimization.

objective_function(solution)[source]

Computes the loss/fitness value for a given solution vector.

_fit(data, lb, ub, mode, n_workers, termination, save_population, \*\*kwargs)[source]

Trains the ANFIS model using a metaheuristic-based optimization strategy.

Notes

  • This class supports hybrid learning by combining metaheuristics and LSE.

  • Intended to be extended by specific regressors/classifiers in the library.

  • Requires CustomANFIS, Optimizer from mealpy, and metrics from permetrics.

SUPPORTED_CLS_OBJECTIVES = {'AS': 'max', 'BSL': 'min', 'CEL': 'min', 'CKS': 'max', 'F1S': 'max', 'F2S': 'max', 'FBS': 'max', 'GINI': 'min', 'GMS': 'max', 'HL': 'min', 'HS': 'max', 'JSI': 'max', 'KLDL': 'min', 'LS': 'max', 'MCC': 'max', 'NPV': 'max', 'PS': 'max', 'ROC-AUC': 'max', 'RS': 'max', 'SS': 'max'}
SUPPORTED_OPTIMIZERS = ['OriginalABC', 'OriginalACOR', 'AugmentedAEO', 'EnhancedAEO', 'ImprovedAEO', 'ModifiedAEO', 'OriginalAEO', 'MGTO', 'OriginalAGTO', 'DevALO', 'OriginalALO', 'OriginalAO', 'OriginalAOA', 'IARO', 'LARO', 'OriginalARO', 'OriginalASO', 'OriginalAVOA', 'OriginalArchOA', 'AdaptiveBA', 'DevBA', 'OriginalBA', 'DevBBO', 'OriginalBBO', 'OriginalBBOA', 'OriginalBES', 'ABFO', 'OriginalBFO', 'OriginalBMO', 'DevBRO', 'OriginalBRO', 'OriginalBSA', 'ImprovedBSO', 'OriginalBSO', 'CleverBookBeesA', 'OriginalBeesA', 'ProbBeesA', 'OriginalCA', 'OriginalCDO', 'OriginalCEM', 'OriginalCGO', 'DevCHIO', 'OriginalCHIO', 'OriginalCOA', 'OCRO', 'OriginalCRO', 'OriginalCSA', 'OriginalCSO', 'OriginalCircleSA', 'OriginalCoatiOA', 'JADE', 'OriginalDE', 'SADE', 'SAP_DE', 'DevDMOA', 'OriginalDMOA', 'OriginalDO', 'DevEFO', 'OriginalEFO', 'OriginalEHO', 'AdaptiveEO', 'ModifiedEO', 'OriginalEO', 'OriginalEOA', 'LevyEP', 'OriginalEP', 'CMA_ES', 'LevyES', 'OriginalES', 'Simple_CMA_ES', 'OriginalESOA', 'OriginalEVO', 'OriginalFA', 'DevFBIO', 'OriginalFBIO', 'OriginalFFA', 'OriginalFFO', 'OriginalFLA', 'DevFOA', 'OriginalFOA', 'WhaleFOA', 'DevFOX', 'OriginalFOX', 'OriginalFPA', 'BaseGA', 'EliteMultiGA', 'EliteSingleGA', 'MultiGA', 'SingleGA', 'OriginalGBO', 'DevGCO', 'OriginalGCO', 'OriginalGJO', 'OriginalGOA', 'DevGSKA', 'OriginalGSKA', 'Matlab101GTO', 'Matlab102GTO', 'OriginalGTO', 'GWO_WOA', 'IGWO', 'OriginalGWO', 'RW_GWO', 'OriginalHBA', 'OriginalHBO', 'OriginalHC', 'SwarmHC', 'OriginalHCO', 'OriginalHGS', 'OriginalHGSO', 'OriginalHHO', 'DevHS', 'OriginalHS', 'OriginalICA', 'OriginalINFO', 'OriginalIWO', 'DevJA', 'LevyJA', 'OriginalJA', 'DevLCO', 'ImprovedLCO', 'OriginalLCO', 'OriginalMA', 'OriginalMFO', 'OriginalMGO', 'OriginalMPA', 'OriginalMRFO', 'WMQIMRFO', 'OriginalMSA', 'DevMVO', 'OriginalMVO', 'OriginalNGO', 'ImprovedNMRA', 'OriginalNMRA', 'OriginalNRO', 'OriginalOOA', 'OriginalPFA', 'OriginalPOA', 'AIW_PSO', 'CL_PSO', 'C_PSO', 'HPSO_TVAC', 'LDW_PSO', 'OriginalPSO', 'P_PSO', 'OriginalPSS', 'DevQSA', 'ImprovedQSA', 'LevyQSA', 'OppoQSA', 'OriginalQSA', 'OriginalRIME', 'OriginalRUN', 'GaussianSA', 'OriginalSA', 'SwarmSA', 'DevSARO', 'OriginalSARO', 'DevSBO', 'OriginalSBO', 'DevSCA', 'OriginalSCA', 'QleSCA', 'OriginalSCSO', 'ImprovedSFO', 'OriginalSFO', 'L_SHADE', 'OriginalSHADE', 'OriginalSHIO', 'OriginalSHO', 'ImprovedSLO', 'ModifiedSLO', 'OriginalSLO', 'DevSMA', 'OriginalSMA', 'DevSOA', 'OriginalSOA', 'OriginalSOS', 'DevSPBO', 'OriginalSPBO', 'OriginalSRSR', 'DevSSA', 'OriginalSSA', 'OriginalSSDO', 'OriginalSSO', 'OriginalSSpiderA', 'OriginalSSpiderO', 'OriginalSTO', 'OriginalSeaHO', 'OriginalServalOA', 'OriginalTDO', 'DevTLO', 'ImprovedTLO', 'OriginalTLO', 'OriginalTOA', 'DevTPO', 'OriginalTS', 'OriginalTSA', 'OriginalTSO', 'EnhancedTWO', 'LevyTWO', 'OppoTWO', 'OriginalTWO', 'DevVCS', 'OriginalVCS', 'OriginalWCA', 'OriginalWDO', 'OriginalWHO', 'HI_WOA', 'OriginalWOA', 'OriginalWaOA', 'OriginalWarSO', 'OriginalZOA']
SUPPORTED_REG_OBJECTIVES = {'A10': 'max', 'A20': 'max', 'A30': 'max', 'ACOD': 'max', 'APCC': 'max', 'AR': 'max', 'AR2': 'max', 'CI': 'max', 'COD': 'max', 'COR': 'max', 'COV': 'max', 'CRM': 'min', 'DRV': 'min', 'EC': 'max', 'EVS': 'max', 'GINI': 'min', 'GINI_WIKI': 'min', 'JSD': 'min', 'KGE': 'max', 'MAAPE': 'min', 'MAE': 'min', 'MAPE': 'min', 'MASE': 'min', 'ME': 'min', 'MRB': 'min', 'MRE': 'min', 'MSE': 'min', 'MSLE': 'min', 'MedAE': 'min', 'NNSE': 'max', 'NRMSE': 'min', 'NSE': 'max', 'OI': 'max', 'PCC': 'max', 'PCD': 'max', 'R': 'max', 'R2': 'max', 'R2S': 'max', 'RAE': 'min', 'RMSE': 'min', 'RSE': 'min', 'RSQ': 'max', 'SMAPE': 'min', 'VAF': 'max', 'WI': 'max'}
build_model()[source]

Build and initialize the ANFIS model, optimizer, and loss criterion.

Notes

  • Initializes CustomANFIS with user settings.

  • Instantiates a PyTorch optimizer for trainable parameters.

  • Sets the appropriate loss function based on task type.

  • Prepares early stopping monitor if enabled.

get_name()[source]

Generate a descriptive name for the ANFIS model based on the optimizer.

Returns:

A string representing the name of the model, including details about the optimizer used. If self.optim is a string, the name will be formatted as “<self.optim_params>-ANFIS”. Otherwise, it will return “<self.optimizer.name>-ANFIS”, assuming self.optimizer is an object with a name attribute.

Return type:

str

Notes

  • This method relies on the presence of self.optim, self.optim_params, and self.optimizer.name attributes within the model instance.

  • It is intended to provide a consistent naming scheme for model instances based on the optimizer configuration.

objective_function(solution=None)[source]

Evaluates the fitness function for classification metrics based on the provided solution.

Parameters:

solution (np.ndarray, default=None) – The proposed solution to evaluate.

Returns:

result – The fitness value, representing the loss for the current solution.

Return type:

float

set_optim_and_paras(optim=None, optim_params=None)[source]

Sets the optim and optim_params parameters for this class.

Parameters:
  • optim (str) – The optimizer name to be set.

  • optim_params (dict) – Parameters to configure the optimizer.

class xanfis.models.base_anfis.BaseClassicAnfis(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, early_stopping=True, n_patience=10, epsilon=0.001, valid_rate=0.1, seed=42, verbose=True, device=None)[source]

Bases: BaseAnfis

A classical ANFIS (Adaptive Neuro-Fuzzy Inference System) model for classification tasks with hybrid learning: gradient descent for premise parameters and least squares estimation for consequent parameters.

This implementation supports customizable optimizers, early stopping, L2 regularization, and validation split.

Parameters:
  • num_rules (int, default=10) – Number of fuzzy rules in the ANFIS model.

  • mf_class (str, default="Gaussian") – Type of membership function to use (e.g., “Gaussian”, “Triangular”).

  • vanishing_strategy (str or None, optional) – Strategy to handle vanishing gradients when combining membership values. Can be one of {“prod”, “mean”, “blend”}. default=’prod’)

  • act_output (callable or None, default=None) – Activation function to apply to the output layer (e.g., softmax for classification).

  • reg_lambda (float or None, optional) – L2 regularization strength. If None, regularization is disabled.

  • epochs (int, default=1000) – Number of training epochs.

  • batch_size (int, default=16) – Batch size used for training.

  • optim (str, default="Adam") – Name of the optimizer. Must be one of the supported optimizers.

  • optim_params (dict or None, default=None) – Dictionary of optimizer hyperparameters (e.g., learning rate).

  • early_stopping (bool, default=True) – Whether to apply early stopping during training.

  • n_patience (int, default=10) – Number of epochs to wait before early stopping if no improvement.

  • epsilon (float, default=0.001) – Minimum change in loss to qualify as improvement for early stopping.

  • valid_rate (float, default=0.1) – Percentage of data to use for validation split.

  • seed (int, default=42) – Random seed for reproducibility.

  • verbose (bool, default=True) – Whether to print progress during training.

  • device (str, optional (default = None)) – Device to run the model on (e.g., “cpu” or “gpu”).

network

The core ANFIS model with fuzzy rules and trainable layers.

Type:

CustomANFIS

optimizer

Optimizer instance based on the specified strategy.

Type:

torch.optim.Optimizer

criterion

Loss function used (e.g., CrossEntropyLoss for classification).

Type:

torch.nn.Module

early_stopper

Instance of early stopping monitor (if enabled).

Type:

EarlyStopper or None

SUPPORTED_OPTIMIZERS = ['Adafactor', 'Adadelta', 'Adagrad', 'Adam', 'Adamax', 'AdamW', 'ASGD', 'LBFGS', 'NAdam', 'RAdam', 'RMSprop', 'Rprop', 'SGD', 'SparseAdam']
build_model()[source]

Construct the ANFIS model, optimizer, and loss criterion.

This method: - Instantiates the ANFIS network based on current configuration. - Configures the optimizer for trainable (non-consequent) parameters. - Selects the loss function based on task type. - Initializes early stopping if enabled.

process_data(X, y, **kwargs)[source]

Process and prepare data for training.

Parameters:
  • X (array-like) – Feature data for training.

  • y (array-like) – Target labels or values for training.

  • **kwargs (additional keyword arguments) – Additional parameters for data processing, if needed.

class xanfis.models.base_anfis.BaseGdAnfis(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, early_stopping=True, n_patience=10, epsilon=0.001, valid_rate=0.1, seed=42, verbose=True, device=None)[source]

Bases: BaseAnfis

A gradient-based Adaptive Neuro-Fuzzy Inference System (ANFIS) base class using PyTorch.

This class supports training an ANFIS model using various gradient descent optimizers provided by PyTorch. It includes options for early stopping, regularization, and model validation.

SUPPORTED_OPTIMIZERS

List of supported PyTorch optimizer names.

Type:

list of str

epochs

Number of training epochs.

Type:

int

batch_size

Mini-batch size used for gradient-based optimization.

Type:

int

optim

Name of the optimizer to use (must be in SUPPORTED_OPTIMIZERS).

Type:

str

optim_params

Parameters to initialize the optimizer.

Type:

dict

early_stopping

Whether to apply early stopping during training.

Type:

bool

n_patience

Number of epochs with no improvement after which training is stopped.

Type:

int

epsilon

Minimum change in the monitored loss to qualify as an improvement.

Type:

float

valid_rate

Proportion of the training data to be used for validation.

Type:

float

verbose

Whether to print logs during training.

Type:

bool

size_input

Number of input features (set during data processing).

Type:

int

size_output

Number of output features (set during data processing).

Type:

int

network

The ANFIS model instance.

Type:

CustomANFIS

optimizer

The PyTorch optimizer instance.

Type:

torch.optim.Optimizer

criterion

Loss function used for training.

Type:

torch.nn.Module

patience_count

Counter for tracking early stopping patience.

Type:

int or None

valid_mode

Whether validation mode is enabled.

Type:

bool

early_stopper

Instance managing early stopping logic.

Type:

EarlyStopper or None

Parameters:
  • num_rules (int, optional) – Number of fuzzy rules (default is 10).

  • mf_class (str, optional) – Type of membership function to use (default is “Gaussian”).

  • vanishing_strategy (str, optional) – Strategy to compute rule strengths (to avoid gradient vanishing too), e.g., “prod” or “min” (default is “prod”).

  • act_output (callable or None, optional) – Activation function for the output layer (default is None).

  • reg_lambda (float or None, optional) – Regularization parameter for L2 loss (default is None).

  • epochs (int, optional) – Number of training epochs (default is 1000).

  • batch_size (int, optional) – Batch size for training (default is 16).

  • optim (str, optional) – Optimizer name from SUPPORTED_OPTIMIZERS (default is “Adam”).

  • optim_params (dict or None, optional) – Parameters for optimizer initialization (default is None).

  • early_stopping (bool, optional) – Enable or disable early stopping (default is True).

  • n_patience (int, optional) – Patience threshold for early stopping (default is 10).

  • epsilon (float, optional) – Minimum improvement threshold for early stopping (default is 0.001).

  • valid_rate (float, optional) – Validation split ratio from training data (default is 0.1).

  • seed (int, optional) – Random seed for reproducibility (default is 42).

  • verbose (bool, optional) – Enable verbose output (default is True).

  • device (str, optional (default = None)) – Device to run the model on (e.g., “cpu” or “gpu”).

build_model():

Build and initialize the ANFIS network, optimizer, and loss function.

process_data(X, y, \*\*kwargs):

Prepares input features and targets for training (to be implemented in subclass).

_fit(data, \*\*kwargs):

Trains the ANFIS model using mini-batch gradient descent and early stopping.

SUPPORTED_OPTIMIZERS = ['Adafactor', 'Adadelta', 'Adagrad', 'Adam', 'Adamax', 'AdamW', 'ASGD', 'LBFGS', 'NAdam', 'RAdam', 'RMSprop', 'Rprop', 'SGD', 'SparseAdam']
build_model()[source]

Build and initialize the ANFIS model, optimizer, and criterion based on user specifications.

This function sets up the model structure, optimizer type and parameters, and loss criterion depending on the task type (classification or regression).

process_data(X, y, **kwargs)[source]

Process and prepare data for training.

Parameters:
  • X (array-like) – Feature data for training.

  • y (array-like) – Target labels or values for training.

  • **kwargs (additional keyword arguments) – Additional parameters for data processing, if needed.

class xanfis.models.base_anfis.CustomANFIS(input_dim=None, num_rules=None, output_dim=None, mf_class=None, task='classification', vanishing_strategy='prod', act_output=None, reg_lambda=None, seed=None, **kwargs)[source]

Bases: Module

A customizable Adaptive Neuro-Fuzzy Inference System (ANFIS) model implemented in PyTorch.

This class implements a modular and flexible ANFIS architecture that supports different membership functions, activation functions, task types (classification, binary classification, regression), and rule strengths calculation strategies (to address vanishing gradient issues in fuzzy logic models).

Parameters:
  • input_dim (int) – Number of input features.

  • num_rules (int) – Number of fuzzy rules in the ANFIS model.

  • output_dim (int) – Number of output units (e.g., number of classes for classification tasks).

  • mf_class (str or mfam.BaseMembership, optional) – The membership function class to use. Can be a string referring to a predefined membership function name or an instance of a custom membership class.

  • task (str, optional) – Type of learning task: ‘classification’, ‘binary_classification’, or ‘regression’. Default is ‘classification’.

  • act_output (str, optional) – Activation function to apply at the output layer. If not provided, a default activation is chosen based on the task (Softmax for classification, Sigmoid for binary classification, Identity for regression).

  • vanishing_strategy (str, optional) – Strategy for computing rule strength to mitigate vanishing gradient issues. Supported values: ‘prod’, ‘mean’, ‘blend’. Default is ‘prod’.

  • reg_lambda (float, optional) – Regularization strength for L2-regularized least squares when updating consequent parameters. Default is 0 (no regularization).

  • seed (int, optional) – Random seed for reproducibility.

  • **kwargs – Additional arguments reserved for future compatibility.

memberships

List of membership function modules for each fuzzy rule.

Type:

nn.ModuleList

coeffs

Learnable parameters (consequents) representing the linear coefficients per rule.

Type:

nn.Parameter

act_output_

Activation function module used in the output layer.

Type:

nn.Module

mf_class_

Class object of the selected membership function.

Type:

type

_get_strength

Method used for computing rule strength (based on chosen strategy).

Type:

Callable

Supported Membership Functions:
  • Gaussian

  • Trapezoidal

  • Triangular

  • Sigmoid

  • Bell

  • GBell

  • PiShaped

  • SShaped

  • ZShaped

  • Linear

Supported Output Activations:

Any activation function in torch.nn.modules.activation, including: ReLU, Sigmoid, Tanh, GELU, Softmax, Identity, etc.

Supported Vanishing Strategies:
  • ‘prod’: Product of membership values (classical approach).

  • ‘mean’: Mean of membership values.

  • ‘blend’: A learned blend between product and mean based on log-strength scaling.

Example

>>> model = CustomANFIS(input_dim=4, num_rules=5, output_dim=3, mf_class="Gaussian",
>>>                     task="classification", act_output="Softmax", vanishing_strategy="blend")
>>> output = model(torch.randn(32, 4))
SUPPORTED_ACTIVATIONS = ['Threshold', 'ReLU', 'RReLU', 'Hardtanh', 'ReLU6', 'Sigmoid', 'Hardsigmoid', 'Tanh', 'SiLU', 'Mish', 'Hardswish', 'ELU', 'CELU', 'SELU', 'GLU', 'GELU', 'Hardshrink', 'LeakyReLU', 'LogSigmoid', 'Softplus', 'Softshrink', 'MultiheadAttention', 'PReLU', 'Softsign', 'Tanhshrink', 'Softmin', 'Softmax', 'Softmax2d', 'LogSoftmax']
SUPPORTED_VANISHING_STRATEGIES = ['prod', 'mean', 'blend']
SUPPORT_MEMBERSHIP_CLASSES = {'Bell': 'BellMembership', 'GBell': 'GBellMembership', 'Gaussian': 'GaussianMembership', 'Linear': 'LinearMembership', 'PiShaped': 'PiShapedMembership', 'SShaped': 'SShapedMembership', 'Sigmoid': 'SigmoidMembership', 'Trapezoidal': 'TrapezoidalMembership', 'Triangular': 'TriangularMembership', 'ZShaped': 'ZShapedMembership'}
forward(X)[source]

Forward pass through the Anfis model.

Parameters:

x (-) – The input tensor.

Returns:

The output of the ANFIS model.

Return type:

  • torch.Tensor

get_weights()[source]

Retrieve only the premise (non-consequent) weights as a flattened NumPy array.

Returns:

Flattened array of the model’s premise weights.

Return type:

  • np.ndarray

get_weights_size()[source]

Calculate the number of trainable premise (non-consequent) parameters in the model.

Returns:

Total number of premise parameters.

Return type:

  • int

set_weights(solution)[source]

Set only the premise (non-consequent) weights of the network based on a given solution vector.

Parameters:

solution (-) – A flat array of weights to set in the model (excluding consequent weights).

training: bool
update_output_weights_by_least_squares(X, y)[source]
class xanfis.models.base_anfis.EarlyStopper(patience=1, epsilon=0.01)[source]

Bases: object

A utility class for implementing early stopping in training processes to prevent overfitting.

- patience

Number of consecutive epochs to tolerate no improvement before stopping.

Type:

int

- epsilon

Minimum loss improvement threshold to reset the patience counter.

Type:

float

- counter

Tracks the number of epochs without sufficient improvement.

Type:

int

- min_loss

Keeps track of the minimum observed loss.

Type:

float

early_stop(loss)[source]

Checks if training should be stopped based on the current loss.

Parameters:

loss (-) – The current loss value for the epoch.

Returns:

True if training should stop, False otherwise.

Return type:

  • bool

xanfis.models.bio_anfis module

class xanfis.models.bio_anfis.BioAnfisClassifier(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, optim='BaseGA', optim_params=None, obj_name='F1S', seed=42, verbose=True, lb=None, ub=None, mode='single', n_workers=None, termination=None)[source]

Bases: BaseBioAnfis, ClassifierMixin

A Bio-based ANFIS Classifier that extends the BaseBioAnfis class and implements the ClassifierMixin interface from Scikit-Learn for classification tasks.

This class integrates bio-inspired optimization algorithms for training an Adaptive Neuro-Fuzzy Inference System (ANFIS) model, supporting both binary and multi-class classification tasks.

classes_

Unique classes found in the target variable.

Type:

np.ndarray

metric_class

The metric class used for evaluating classification performance.

Type:

type

Parameters:
  • num_rules (int, optional) – Number of fuzzy rules (default is 10).

  • mf_class (str, optional) – Membership function class (default is “Gaussian”).

  • vanishing_strategy (str or None, optional) – Strategy for calculating rule strengths (default is ‘prod’).

  • act_output (any, optional) – Activation function for the output layer (default is None).

  • reg_lambda (float or None, optional) – Regularization parameter (default is None). This is used for regularizing the model.

  • optim (str, optional) – The optimization algorithm to use (default is “BaseGA”).

  • optim_params (dict, optional) – Parameters for the optimizer (default is None).

  • obj_name (str, optional) – The objective name for the optimization (default is “F1S”).

  • seed (int, optional) – Random seed for reproducibility (default is 42).

  • verbose (bool, optional) – Whether to print detailed logs during fitting (default is True).

  • lb (int, float, list, tuple, np.ndarray, optional.) – Lower bounds for optimization (default is (-1.0,)).

  • ub (int, float, list, tuple, np.ndarray, optional.) – Upper bounds for optimization (default is (1.0,)).

  • mode (str, optional) – Mode for optimization (default is ‘single’).

  • n_workers (int, optional) – Number of workers for parallel processing (default is None).

  • termination (any, optional) – Termination criteria for optimization (default is None).

fit(X, y, lb=(-1.0,), ub=(1.0,), mode='single', n_workers=None, termination=None, save_population=False, \*\*kwargs):

Fits the model to the training data using the specified bio-inspired optimizer.

predict(X):

Predicts the class labels for the provided input data.

score(X, y):

Computes the accuracy score of the model based on predictions.

predict_proba(X):

Computes the probability estimates for each class (for classification tasks only).

evaluate(y_true, y_pred, list_metrics=("AS", "RS")):

Returns the list of performance metrics on the given test data and labels.

evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]

Return the list of performance metrics on the given test data and labels.

Parameters:
  • y_true (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True values for X.

  • y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted values for X.

  • list_metrics (list, default=("AS", "RS")) – List of metrics to compute using Permetrics library: https://github.com/thieu1995/permetrics

Returns:

results – A dictionary containing the results of the requested metrics.

Return type:

dict

fit(X, y)[source]

Fits the model to the training data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Training data.

  • y (array-like, shape (n_samples,)) – Target values.

Returns:

self – Returns the instance of the fitted model.

Return type:

BioAnfisClassifier

predict(X)[source]

Predicts the class labels for the provided input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – Input data for prediction.

Returns:

Predicted class labels for each sample.

Return type:

np.ndarray

predict_proba(X)[source]

Computes the probability estimates for each class (for classification tasks only).

Parameters:

X (array-like, shape (n_samples, n_features)) – Input data for which to predict probabilities.

Returns:

Probability predictions for each class.

Return type:

np.ndarray

Raises:

ValueError – If the task is not a classification task.

score(X, y)[source]

Computes the accuracy score of the model based on predictions.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Input data for scoring.

  • y (array-like, shape (n_samples,)) – True labels for comparison.

Returns:

Accuracy score of the model.

Return type:

float

class xanfis.models.bio_anfis.BioAnfisRegressor(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, optim='BaseGA', optim_params=None, obj_name='MSE', seed=42, verbose=True, lb=None, ub=None, mode='single', n_workers=None, termination=None)[source]

Bases: BaseBioAnfis, RegressorMixin

A Bio-based ANFIS Regressor that extends the BaseBioAnfis class and implements the RegressorMixin interface from Scikit-Learn for regression tasks.

This class integrates bio-inspired optimization algorithms for training an Adaptive Neuro-Fuzzy Inference System (ANFIS) model, supporting both single-output and multi-output regression tasks.

metric_class

The metric class used for evaluating regression performance.

Type:

type

size_input

Number of input features (set during training).

Type:

int

size_output

Number of output features (set during training).

Type:

int

task

The type of regression task (“regression” or “multi_regression”).

Type:

str

network

The ANFIS model instance.

Type:

nn.Module

Parameters:
  • num_rules (int, optional) – Number of fuzzy rules (default is 10).

  • mf_class (str, optional) – Membership function class (default is “Gaussian”).

  • vanishing_strategy (str or None, optional) – Strategy for calculating rule strengths (default is ‘prod’).

  • act_output (any, optional) – Activation function for the output layer (default is None).

  • reg_lambda (float or None, optional) – Regularization parameter (default is None). This is used for regularizing the model.

  • optim (str, optional) – The optimization algorithm to use (default is “BaseGA”).

  • optim_params (dict, optional) – Parameters for the optimizer (default is None).

  • obj_name (str, optional) – The objective name for the optimization (default is “MSE”).

  • seed (int, optional) – Random seed for reproducibility (default is 42).

  • verbose (bool, optional) – Whether to print detailed logs during fitting (default is True).

  • lb (int, float, list, tuple, np.ndarray, optional.) – Lower bounds for optimization (default is (-1.0,)).

  • ub (int, float, list, tuple, np.ndarray, optional.) – Upper bounds for optimization (default is (1.0,)).

  • mode (str, optional) – Mode for optimization (default is ‘single’).

  • n_workers (int, optional) – Number of workers for parallel processing (default is None).

  • termination (any, optional) – Termination criteria for optimization (default is None).

fit(X, y, lb=(-1.0,), ub=(1.0,), mode='single', n_workers=None, termination=None, save_population=False, \*\*kwargs):

Fits the model to the training data using the specified bio-inspired optimizer.

predict(X):

Predicts the output values for the provided input data.

score(X, y):

Computes the R2 score of the model based on predictions.

evaluate(y_true, y_pred, list_metrics=("AS", "RS")):

Returns the list of performance metrics on the given test data and labels.

evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]

Return the list of performance metrics on the given test data and labels.

Parameters:
  • y_true (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True values for X.

  • y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted values for X.

  • list_metrics (list, default=("AS", "RS")) – List of metrics to compute using Permetrics library: https://github.com/thieu1995/permetrics

Returns:

results – A dictionary containing the results of the requested metrics.

Return type:

dict

fit(X, y)[source]

Fits the model to the training data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Training data.

  • y (array-like, shape (n_samples,) or (n_samples, n_outputs)) – Target values.

Returns:

self – Returns the instance of the fitted model.

Return type:

BioAnfisRegressor

predict(X)[source]

Predicts the output values for the provided input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – Input data for prediction.

Returns:

Predicted output values for each sample.

Return type:

np.ndarray

score(X, y)[source]

Computes the R2 score of the model based on predictions.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Input data for scoring.

  • y (array-like, shape (n_samples,)) – True labels for comparison.

Returns:

R2 score of the model.

Return type:

float

xanfis.models.classic_anfis module

class xanfis.models.classic_anfis.AnfisClassifier(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, early_stopping=True, n_patience=10, epsilon=0.001, valid_rate=0.1, seed=42, verbose=True, device=None)[source]

Bases: BaseClassicAnfis, ClassifierMixin

Classic Adaptive Neuro-Fuzzy Inference System (ANFIS) Classifier

This classifier implements a traditional ANFIS model for classification tasks (binary and multi-class), where:
  • The parameters of the fuzzy membership functions are updated using a gradient descent-based algorithm.

  • The parameters of the output layer are estimated analytically using either the pseudo-inverse method or Ridge regression.

The architecture supports flexible configuration of fuzzy rules, membership function types, output activation, and various optimization strategies for training.

classes_

Unique class labels inferred from the training target data.

Type:

np.ndarray

size_input

Number of input features.

Type:

int

size_output

Number of output neurons, determined by the number of classes.

Type:

int

task

Type of classification task: “binary_classification” or “classification” (multi-class).

Type:

str

network

The internal ANFIS network model built dynamically during training.

Type:

nn.Module

Parameters:
  • num_rules (int, optional (default=10)) – Number of fuzzy rules to be used in the rule base.

  • mf_class (str, optional (default="Gaussian")) – Type of membership function used in the fuzzy layer.

  • vanishing_strategy (str or None, optional (default=None)) – Strategy to address vanishing rule strengths, if any.

  • act_output (str or None, optional (default=None)) – Activation function applied at the output layer.

  • reg_lambda (float or None, optional (default=None)) – Regularization strength for Ridge regression (if used in output parameter estimation).

  • epochs (int, optional (default=1000)) – Number of training iterations.

  • batch_size (int, optional (default=16)) – Number of samples per batch during training.

  • optim (str, optional (default="Adam")) – Name of the optimizer to use for training the membership function parameters.

  • optim_params (dict or None, optional (default=None)) – Dictionary of optimizer hyperparameters, such as learning rate or momentum.

  • early_stopping (bool, optional (default=True)) – Whether to apply early stopping during training based on validation loss.

  • n_patience (int, optional (default=10)) – Number of epochs with no improvement before early stopping is triggered.

  • epsilon (float, optional (default=0.001)) – Minimum improvement in validation loss to continue training.

  • valid_rate (float, optional (default=0.1)) – Fraction of training data reserved for validation.

  • seed (int, optional (default=42)) – Random seed used for reproducibility.

  • verbose (bool, optional (default=True)) – Whether to print training progress and validation results.

  • device (str, optional (default = None)) – Device to run the model on (e.g., “cpu” or “gpu”).

process_data(X, y, \*\*kwargs):

Splits and preprocesses the training data, and prepares PyTorch DataLoader objects.

fit(X, y, \*\*kwargs):

Builds and trains the ANFIS classifier using the hybrid learning approach.

predict(X):

Predicts class labels for the given input samples.

score(X, y):

Computes the classification accuracy on the given dataset.

predict_proba(X):

Returns predicted probabilities for each class (available for classification tasks only).

evaluate(y_true, y_pred, list_metrics=("AS", "RS")):

Computes evaluation metrics using the Permetrics library.

evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]

Returns performance metrics for the model on the provided test data.

Parameters:
  • y_true (array-like of shape (n_samples,)) – True class labels.

  • y_pred (array-like of shape (n_samples,)) – Predicted class labels.

  • list_metrics (list, default=("AS", "RS")) – List of performance metrics to calculate. Refer to Permetrics (https://github.com/thieu1995/permetrics) library for available metrics.

Returns:

Dictionary with results for the specified metrics.

Return type:

dict

fit(X, y, **kwargs)[source]

Trains the ANFIS model on the provided data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Training data.

  • y (array-like, shape (n_samples,)) – Target values.

Returns:

self – Fitted classifier.

Return type:

object

predict(X)[source]

Predicts the class labels for the given input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – Input data.

Returns:

Predicted class labels for each sample.

Return type:

numpy.ndarray

predict_proba(X)[source]

Computes the probability estimates for each class (for classification tasks only).

Parameters:

X (array-like, shape (n_samples, n_features)) – Input data.

Returns:

Probability predictions for each class.

Return type:

numpy.ndarray

process_data(X, y, **kwargs)[source]

Prepares and processes data for training, including optional splitting into validation data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Training data.

  • y (array-like, shape (n_samples,)) – Target values.

Returns:

tuple – Data loader for training data, and tensors for validation data (if specified).

Return type:

(train_loader, X_valid_tensor, y_valid_tensor)

score(X, y)[source]

Computes the accuracy score for the classifier.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Input data.

  • y (array-like, shape (n_samples,)) – True class labels.

Returns:

Accuracy score of the classifier.

Return type:

float

class xanfis.models.classic_anfis.AnfisRegressor(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, early_stopping=True, n_patience=10, epsilon=0.001, valid_rate=0.1, seed=42, verbose=True, device=None)[source]

Bases: BaseClassicAnfis, RegressorMixin

Adaptive Neuro-Fuzzy Inference System (ANFIS) Regressor for predicting continuous values.

This classifier implements a traditional ANFIS model for regression tasks (single and multi-output), where:
  • The parameters of the fuzzy membership functions are updated using a gradient descent-based algorithm.

  • The parameters of the output layer are estimated analytically using either the pseudo-inverse method or Ridge regression.

size_input

Number of input features (set during training).

Type:

int

size_output

Number of output features (set during training).

Type:

int

task

The type of regression task (“regression” or “multi_regression”).

Type:

str

network

The ANFIS model instance.

Type:

nn.Module

Parameters:
  • num_rules (int, optional) – Number of fuzzy rules (default is 10).

  • mf_class (str, optional) – Membership function class (default is “Gaussian”).

  • vanishing_strategy (str or None, optional) – Strategy for calculating rule strengths (default is None).

  • act_output (str, optional) – Activation function for the output layer (default is None).

  • reg_lambda (float or None, optional) – Regularization strength for the output layer (default is None).

  • epochs (int, optional) – Number of epochs for training (default is 1000).

  • batch_size (int, optional) – Size of the mini-batch during training (default is 16).

  • optim (str, optional) – Optimization algorithm (default is “Adam”).

  • optim_params (dict or None, optional) – Additional parameters for the optimizer (default is None).

  • early_stopping (bool, optional) – Flag to enable early stopping during training (default is True).

  • n_patience (int, optional) – Number of epochs to wait for improvement before stopping (default is 10).

  • epsilon (float, optional) – Tolerance for improvement (default is 0.001).

  • valid_rate (float, optional) – Proportion of data to use for validation (default is 0.1).

  • seed (int, optional) – Random seed for reproducibility (default is 42).

  • verbose (bool, optional) – Flag to enable verbose output during training (default is True).

  • device (str, optional (default = None)) – Device to run the model on (e.g., “cpu” or “gpu”).

process_data(X, y, \*\*kwargs):

Prepares the input data for training and validation by converting it to tensors and creating DataLoaders for batch processing.

fit(X, y, \*\*kwargs):

Fits the ANFIS model to the provided training data.

predict(X):

Predicts the target values for the given input features.

score(X, y):

Computes the R2 score of the predictions.

evaluate(y_true, y_pred, list_metrics=("MSE", "MAE")):

Returns a list of performance metrics for the predictions.

evaluate(y_true, y_pred, list_metrics=('MSE', 'MAE'))[source]

Returns a list of performance metrics for the predictions.

Parameters:
  • y_true (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True values for the input features.

  • y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted values for the input features.

  • list_metrics (list, default=("MSE", "MAE")) – List of metrics to evaluate (can be from Permetrics library: https://github.com/thieu1995/permetrics).

Returns:

results – A dictionary containing the results of the specified metrics.

Return type:

dict

fit(X, y, **kwargs)[source]

Fits the ANFIS model to the provided training data.

Parameters:
  • X (array-like) – Input features for training.

  • y (array-like) – Target values for training.

Returns:

self – Returns the instance of the fitted model.

Return type:

AnfisRegressor

predict(X)[source]

Predicts the target values for the given input features.

Parameters:

X (array-like) – Input features for prediction.

Returns:

Predicted values for the input features.

Return type:

numpy.ndarray

process_data(X, y, **kwargs)[source]

Prepares the input data for training and validation by converting it to tensors and creating DataLoaders for batch processing.

Parameters:
  • X (array-like) – Input features for the regression task.

  • y (array-like) – Target values for the regression task.

Returns:

A tuple containing the training DataLoader and optional validation tensors.

Return type:

tuple

score(X, y)[source]

Computes the R2 score of the predictions.

Parameters:
  • X (array-like) – Input features for scoring.

  • y (array-like) – True target values for the input features.

Returns:

R2 score indicating the model’s performance.

Return type:

float

xanfis.models.gd_anfis module

class xanfis.models.gd_anfis.GdAnfisClassifier(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, early_stopping=True, n_patience=10, epsilon=0.001, valid_rate=0.1, seed=42, verbose=True, device=None)[source]

Bases: BaseGdAnfis, ClassifierMixin

Adaptive Neuro-Fuzzy Inference System (ANFIS) Classifier for predicting class labels.

This class integrates Adaptive Neuro-Fuzzy Inference System (ANFIS) with gradient-based optimization techniques for classification tasks. It supports both binary and multi-class classification, enabling users to perform predictions on categorical target variables.

classes_

List of unique class labels determined during the fitting process.

Type:

array-like, shape (n_classes,)

size_input

Number of input features (set during training).

Type:

int

size_output

Number of output classes (set during training).

Type:

int

task

The type of classification task (“binary_classification” or “classification”).

Type:

str

network

The ANFIS model instance.

Type:

nn.Module

Parameters:
  • num_rules (int, optional) – Number of fuzzy rules for the ANFIS model (default is 10).

  • mf_class (str, optional) – Membership function class (default is “Gaussian”).

  • vanishing_strategy (str or None, optional) – Strategy for calculating rule strengths (default is ‘prod’).

  • act_output (str, optional) – Activation function for the output layer (default is None).

  • reg_lambda (float or None, optional) – Regularization parameter (default is None).

  • epochs (int, optional) – Number of epochs for training (default is 1000).

  • batch_size (int, optional) – Size of the mini-batch during training (default is 16).

  • optim (str, optional) – Optimization algorithm (default is “Adam”).

  • optim_params (dict, optional) – Additional parameters for the optimizer (default is None).

  • early_stopping (bool, optional) – Flag to enable early stopping during training (default is True).

  • n_patience (int, optional) – Number of epochs to wait for improvement before stopping (default is 10).

  • epsilon (float, optional) – Tolerance for improvement (default is 0.001).

  • valid_rate (float, optional) – Proportion of data to use for validation (default is 0.1).

  • seed (int, optional) – Random seed for reproducibility (default is 42).

  • verbose (bool, optional) – Flag to enable verbose output during training (default is True).

  • device (str, optional (default = None)) – Device to run the model on (e.g., “cpu” or “gpu”).

process_data(X, y, \*\*kwargs):

Prepares and processes data for training, including optional splitting into validation data.

fit(X, y, \*\*kwargs):

Trains the ANFIS model on the provided data.

predict(X):

Predicts the class labels for the given input data.

score(X, y):

Computes the accuracy score for the classifier.

predict_proba(X):

Computes the probability estimates for each class (for classification tasks only).

evaluate(y_true, y_pred, list_metrics=("AS", "RS")):

Returns performance metrics for the model on the provided test data.

evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]

Returns performance metrics for the model on the provided test data.

Parameters:
  • y_true (array-like of shape (n_samples,)) – True class labels.

  • y_pred (array-like of shape (n_samples,)) – Predicted class labels.

  • list_metrics (list, default=("AS", "RS")) – List of performance metrics to calculate. Refer to Permetrics (https://github.com/thieu1995/permetrics) library for available metrics.

Returns:

Dictionary with results for the specified metrics.

Return type:

dict

fit(X, y, **kwargs)[source]

Trains the ANFIS model on the provided data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Training data.

  • y (array-like, shape (n_samples,)) – Target values.

Returns:

self – Fitted classifier.

Return type:

object

predict(X)[source]

Predicts the class labels for the given input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – Input data.

Returns:

Predicted class labels for each sample.

Return type:

numpy.ndarray

predict_proba(X)[source]

Computes the probability estimates for each class (for classification tasks only).

Parameters:

X (array-like, shape (n_samples, n_features)) – Input data.

Returns:

Probability predictions for each class.

Return type:

numpy.ndarray

process_data(X, y, **kwargs)[source]

Prepares and processes data for training, including optional splitting into validation data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Training data.

  • y (array-like, shape (n_samples,)) – Target values.

Returns:

tuple – Data loader for training data, and tensors for validation data (if specified).

Return type:

(train_loader, X_valid_tensor, y_valid_tensor)

score(X, y)[source]

Computes the accuracy score for the classifier.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Input data.

  • y (array-like, shape (n_samples,)) – True class labels.

Returns:

Accuracy score of the classifier.

Return type:

float

class xanfis.models.gd_anfis.GdAnfisRegressor(num_rules=10, mf_class='Gaussian', vanishing_strategy='prod', act_output=None, reg_lambda=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, early_stopping=True, n_patience=10, epsilon=0.001, valid_rate=0.1, seed=42, verbose=True, device=None)[source]

Bases: BaseGdAnfis, RegressorMixin

Adaptive Neuro-Fuzzy Inference System (ANFIS) Regressor for predicting continuous values.

This class integrates Adaptive Neuro-Fuzzy Inference System (ANFIS) with gradient-based optimization techniques for regression tasks. It supports both single-output and multi-output regression, allowing users to perform predictions on continuous target variables.

size_input

Number of input features (set during training).

Type:

int

size_output

Number of output features (set during training).

Type:

int

task

The type of regression task (“regression” or “multi_regression”).

Type:

str

network

The ANFIS model instance.

Type:

nn.Module

Parameters:
  • num_rules (int, optional) – Number of fuzzy rules for the ANFIS model (default is 10).

  • mf_class (str, optional) – Membership function class (default is “Gaussian”).

  • vanishing_strategy (str or None, optional) – Strategy for calculating rule strengths (default is ‘prod’).

  • act_output (str, optional) – Activation function for the output layer (default is None).

  • reg_lambda (float or None, optional) – Regularization parameter (default is None).

  • epochs (int, optional) – Number of epochs for training (default is 1000).

  • batch_size (int, optional) – Size of the mini-batch during training (default is 16).

  • optim (str, optional) – Optimization algorithm (default is “Adam”).

  • optim_params (dict, optional) – Additional parameters for the optimizer (default is None).

  • early_stopping (bool, optional) – Flag to enable early stopping during training (default is True).

  • n_patience (int, optional) – Number of epochs to wait for improvement before stopping (default is 10).

  • epsilon (float, optional) – Tolerance for improvement (default is 0.001).

  • valid_rate (float, optional) – Proportion of data to use for validation (default is 0.1).

  • seed (int, optional) – Random seed for reproducibility (default is 42).

  • verbose (bool, optional) – Flag to enable verbose output during training (default is True).

  • device (str, optional (default = None)) – Device to run the model on (e.g., “cpu” or “gpu”).

process_data(X, y, \*\*kwargs):

Prepares the input data for training and validation by converting it to tensors and creating DataLoaders for batch processing.

fit(X, y, \*\*kwargs):

Fits the ANFIS model to the provided training data.

predict(X):

Predicts the target values for the given input features.

score(X, y):

Computes the R2 score of the predictions.

evaluate(y_true, y_pred, list_metrics=("MSE", "MAE")):

Returns a list of performance metrics for the predictions.

evaluate(y_true, y_pred, list_metrics=('MSE', 'MAE'))[source]

Returns a list of performance metrics for the predictions.

Parameters:
  • y_true (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True values for the input features.

  • y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted values for the input features.

  • list_metrics (list, default=("MSE", "MAE")) – List of metrics to evaluate (can be from Permetrics library: https://github.com/thieu1995/permetrics).

Returns:

results – A dictionary containing the results of the specified metrics.

Return type:

dict

fit(X, y, **kwargs)[source]

Fits the ANFIS model to the provided training data.

Parameters:
  • X (array-like) – Input features for training.

  • y (array-like) – Target values for training.

Returns:

self – Returns the instance of the fitted model.

Return type:

GdAnfisRegressor

predict(X)[source]

Predicts the target values for the given input features.

Parameters:

X (array-like) – Input features for prediction.

Returns:

Predicted values for the input features.

Return type:

numpy.ndarray

process_data(X, y, **kwargs)[source]

Prepares the input data for training and validation by converting it to tensors and creating DataLoaders for batch processing.

Parameters:
  • X (array-like) – Input features for the regression task.

  • y (array-like) – Target values for the regression task.

Returns:

A tuple containing the training DataLoader and optional validation tensors.

Return type:

tuple

score(X, y)[source]

Computes the R2 score of the predictions.

Parameters:
  • X (array-like) – Input features for scoring.

  • y (array-like) – True target values for the input features.

Returns:

R2 score indicating the model’s performance.

Return type:

float