waveletml.models package¶
waveletml.models.base_model module¶
- class waveletml.models.base_model.BaseModel[source]¶
Bases:
BaseEstimatorBase class for all models
- 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
- static load_model(load_path='history', filename='model.pkl')[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:
BaseMlp
- 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”).
waveletml.models.custom_wnn module¶
- class waveletml.models.custom_wnn.BaseCustomWNN(input_dim, hidden_dim, output_dim, wavelet_fn='morlet', act_output=None, seed=None)[source]¶
Bases:
ModuleBase class for custom wavelet neural networks. This class is not meant to be used directly.
- 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_WAVELETS = ['morlet', 'mexican_hat', 'haar', 'db1', 'db2', 'sym2', 'coif1', 'bior1.3', 'bior1.5', 'rbio1.3', 'rbio1.5', 'dmey', 'cmor']¶
- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- get_weights()[source]¶
Retrieve network weights as a flattened array.
- Returns:
Flattened array of the model’s weights.
- Return type:
np.ndarray
- get_weights_size()[source]¶
Calculate the total number of trainable parameters in the model.
- Returns:
Total number of parameters.
- Return type:
int
- set_weights(solution)[source]¶
Set network weights based on a given solution vector.
- Parameters:
solution (-) – A flat array of weights to set in the model.
- training: bool¶
- class waveletml.models.custom_wnn.CustomWaveletExpansionNetwork(input_dim, hidden_dim, output_dim, wavelet_fn='morlet', act_output=None, seed=None)[source]¶
Bases:
BaseCustomWNNA custom wavelet neural network using a feature expansion-based wavelet layer.
This network computes the wavelet function for each input dimension, expands the feature space, and passes the result through a standard linear output layer.
In this version, we calculate the z value for each input dimension, then we apply the wavelet function to each z value. The output of each hidden neuron is forming a new feature space. Wavelet-based feature expansion. The output layer is a standard linear layer. (weights and bias).
The number of parameters for WNN(3, 5, 1) is: 5*3 (centers) + 5*3 (scales) + 3*5 (weights at output layer) + 1 (bias ouput) = 5*3 + 5*3 + 3*5 + 1 = 46
- wavelet_layer¶
The wavelet-based feature expansion layer.
- Type:
- output_layer¶
The standard linear output layer.
- Type:
torch.nn.Linear
- forward(x)[source]¶
Performs the forward pass of the network.
- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, input_dim).
- Returns:
Output tensor of shape (batch_size, output_dim).
- Return type:
torch.Tensor
- training: bool¶
- class waveletml.models.custom_wnn.CustomWaveletProductNetwork(input_dim, hidden_dim, output_dim, wavelet_fn='morlet', act_output=None, seed=None)[source]¶
Bases:
BaseCustomWNNA custom wavelet neural network using a product-based wavelet layer. This network computes the wavelet function for each input dimension, takes the product of the results for each hidden neuron, and passes the result through a standard linear output layer.
In this version, we calculate the z value for each input dimension, then we apply the wavelet function to each z value. The output of each hidden neuron is the product of all wavelet functions from input to that hidden neuron. The output layer is a standard linear layer. (weights and bias).
Examples: The number of parameters for WNN(3, 5, 1) is: 5*3 (centers) + 5*3 (scales) + 5 (weights at output layer) + 1 (bias ouput) = 5*3 + 5*3 + 5 + 1 = 36
- wavelet_layer¶
The wavelet-based product layer.
- Type:
- output_layer¶
The standard linear output layer.
- Type:
torch.nn.Linear
- forward(x)[source]¶
Performs the forward pass of the network.
- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, input_dim).
- Returns:
Output tensor of shape (batch_size, output_dim).
- Return type:
torch.Tensor
- training: bool¶
- class waveletml.models.custom_wnn.CustomWaveletSummationNetwork(input_dim, hidden_dim, output_dim, wavelet_fn='morlet', act_output=None, seed=None)[source]¶
Bases:
BaseCustomWNNA custom wavelet neural network using a summation-based wavelet layer.
This network computes the wavelet function for each input dimension, takes the sum of the results for each hidden neuron, and passes the result through a standard linear output layer.
In this version, we calculate the z value for each input dimension, then we apply the wavelet function to each z value. The output of each hidden neuron is the sum of all wavelet functions from input to that hidden neuron. The output layer is a standard linear layer. (weights and bias).
The number of parameters for WNN(3, 5, 1) is: 5*3 (centers) + 5*3 (scales) + 5 (weights at output layer) + 1 (bias ouput) = 5*3 + 5*3 + 5 + 1 = 36
- wavelet_layer¶
The wavelet-based summation layer.
- Type:
- output_layer¶
The standard linear output layer.
- Type:
torch.nn.Linear
- forward(x)[source]¶
Performs the forward pass of the network.
- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, input_dim).
- Returns:
Output tensor of shape (batch_size, output_dim).
- Return type:
torch.Tensor
- training: bool¶
- class waveletml.models.custom_wnn.CustomWaveletWeightedLinearNetwork(input_dim, hidden_dim, output_dim, wavelet_fn='morlet', act_output=None, seed=None)[source]¶
Bases:
BaseCustomWNNA custom wavelet neural network using a weighted linear layer.
In this version, we calculate the sum of all inputs to each neuron (wx) Then we calculate z = (wx - b) / a, where wx is the weighted sum of inputs. Then we apply the wavelet function to the z value. The output layer is a standard linear layer. (weights and bias)
Examples: The number of parameters for WNN(3, 5, 1) is: 3 * 5 (weights) + 5 (centers) + 5 (scales) + 5 (weights at output layer) + 1 (bias ouput) = 3 * 5 + 5 + 5 + 1 = 26
- wavelet_layer¶
The wavelet-based weighted linear layer.
- output_layer¶
The standard linear output layer.
- Type:
torch.nn.Linear
- forward(x)[source]¶
Performs the forward pass of the network.
- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, input_dim).
- Returns:
Output tensor of shape (batch_size, output_dim).
- Return type:
torch.Tensor
- training: bool¶
waveletml.models.gd_wnn module¶
- class waveletml.models.gd_wnn.BaseGdWnnModel(size_hidden=10, wavelet_fn='morlet', act_output=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, valid_rate=0.1, seed=42, verbose=True, device=None, **kwargs)[source]¶
Bases:
BaseModelBase class for Fully Gradient-based Wavelet Neural Network (GdWNN) models.
This class provides common functionality for both classifiers and regressors, including data processing, training, and callback handling.
Number of hidden neurons in the wavelet neural network.
- Type:
int
- wavelet_fn¶
Name of the wavelet function to use.
- Type:
str
- act_output¶
Activation function for the output layer.
- Type:
callable or None
- epochs¶
Number of training epochs.
- Type:
int
- batch_size¶
Batch size for training.
- Type:
int
- optim¶
Name of the optimizer to use.
- Type:
str
- optim_params¶
Parameters for the optimizer.
- Type:
dict
- valid_rate¶
Proportion of data to use for validation.
- Type:
float
- seed¶
Random seed for reproducibility.
- Type:
int
- verbose¶
Whether to print training progress.
- Type:
bool
- device¶
Device to use for training (‘cpu’ or ‘gpu’).
- Type:
str
- callbacks¶
List of callback instances for training.
- Type:
list
- wnn_model¶
Wavelet neural network model class.
- Type:
class
- kwargs¶
Additional keyword arguments.
- Type:
dict
- size_input¶
Number of input features.
- Type:
int or None
- size_output¶
Number of output features.
- Type:
int or None
- network¶
Neural network instance.
- Type:
torch.nn.Module or None
- optimizer¶
Optimizer instance.
- Type:
torch.optim.Optimizer or None
- criterion¶
Loss function.
- Type:
torch.nn.Module or None
- valid_mode¶
Whether validation mode is enabled.
- Type:
bool
- loss_train¶
List of training losses for each epoch.
- Type:
list
- class waveletml.models.gd_wnn.GdWnnClassifier(size_hidden=10, wavelet_fn='morlet', act_output=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, valid_rate=0.1, seed=42, verbose=True, device=None, **kwargs)[source]¶
Bases:
BaseGdWnnModel,ClassifierMixinGradient-based Wavelet Neural Network (GdWNN) Classifier.
This class implements a wavelet-based neural network for classification tasks, leveraging gradient-based optimization for training.
Parameters:¶
- size_hiddenint, optional
Number of hidden neurons in the wavelet neural network (default is 10).
- wavelet_fnstr, optional
Name of the wavelet function to use (default is “morlet”).
- act_outputcallable or None, optional
Activation function for the output layer (default is None).
- epochsint, optional
Number of training epochs (default is 1000).
- batch_sizeint, optional
Batch size for training (default is 16).
- optimstr, optional
Name of the optimizer to use (default is “Adam”).
- optim_paramsdict, optional
Parameters for the optimizer (default is None).
- valid_ratefloat, optional
Proportion of data to use for validation (default is 0.1).
- seedint, optional
Random seed for reproducibility (default is 42).
- verbosebool, optional
Whether to print training progress (default is True).
- devicestr, optional
Device to use for training (‘cpu’ or ‘gpu’, default is None).
- kwargsdict, optional
Additional keyword arguments.
Number of hidden neurons in the wavelet neural network.
- Type:
int
- wavelet_fn¶
Name of the wavelet function to use.
- Type:
str
- act_output¶
Activation function for the output layer.
- Type:
callable or None
- epochs¶
Number of training epochs.
- Type:
int
- batch_size¶
Batch size for training.
- Type:
int
- optim¶
Name of the optimizer to use.
- Type:
str
- optim_params¶
Parameters for the optimizer.
- Type:
dict
- valid_rate¶
Proportion of data to use for validation.
- Type:
float
- seed¶
Random seed for reproducibility.
- Type:
int
- verbose¶
Whether to print training progress.
- Type:
bool
- device¶
Device to use for training (‘cpu’ or ‘gpu’).
- Type:
str
- classes_¶
Unique class labels in the dataset.
- Type:
np.ndarray or None
- task¶
Classification task type (‘classification’ or ‘binary_classification’).
- Type:
str
- class waveletml.models.gd_wnn.GdWnnRegressor(size_hidden=10, wavelet_fn='morlet', act_output=None, epochs=1000, batch_size=16, optim='Adam', optim_params=None, valid_rate=0.1, seed=42, verbose=True, device=None, **kwargs)[source]¶
Bases:
BaseGdWnnModel,RegressorMixinGradient-based Wavelet Neural Network (GdWNN) Regressor.
This class implements a wavelet-based neural network for regression tasks, leveraging gradient-based optimization for training.
- Parameters:
size_hidden (int, optional) – Number of hidden neurons in the wavelet neural network (default is 10).
wavelet_fn (str, optional) – Name of the wavelet function to use (default is “morlet”).
act_output (callable or None, optional) – Activation function for the output layer (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) – Name of the optimizer to use (default is “Adam”).
optim_params (dict, optional) – Parameters for the optimizer (default is None).
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) – Whether to print training progress (default is True).
device (str, optional) – Device to use for training (‘cpu’ or ‘gpu’, default is None).
kwargs (dict, optional) – Additional keyword arguments.
Number of hidden neurons in the wavelet neural network.
- Type:
int
- wavelet_fn¶
Name of the wavelet function to use.
- Type:
str
- act_output¶
Activation function for the output layer.
- Type:
callable or None
- epochs¶
Number of training epochs.
- Type:
int
- batch_size¶
Batch size for training.
- Type:
int
- optim¶
Name of the optimizer to use.
- Type:
str
- optim_params¶
Parameters for the optimizer.
- Type:
dict
- valid_rate¶
Proportion of data to use for validation.
- Type:
float
- seed¶
Random seed for reproducibility.
- Type:
int
- verbose¶
Whether to print training progress.
- Type:
bool
- device¶
Device to use for training (‘cpu’ or ‘gpu’).
- Type:
str
- task¶
Regression task type (‘regression’ or ‘multi_regression’).
- Type:
str
- size_input¶
Number of input features.
- Type:
int or None
- size_output¶
Number of output features.
- Type:
int or None
- network¶
Neural network instance.
- Type:
torch.nn.Module or None
- optimizer¶
Optimizer instance.
- Type:
torch.optim.Optimizer or None
- criterion¶
Loss function.
- Type:
torch.nn.Module or None
- valid_mode¶
Whether validation mode is enabled.
- Type:
bool
- loss_train¶
List of training losses for each epoch.
- Type:
list
- evaluate(y_true, y_pred, list_metrics=('MSE', 'MAE'))[source]¶
Evaluates the model using specified metrics.
waveletml.models.mha_wnn module¶
- class waveletml.models.mha_wnn.BaseMhaWnnModel(size_hidden=10, wavelet_fn='morlet', act_output=None, optim='Adam', optim_params=None, obj_name=None, seed=42, verbose=True, wnn_type=None, lb=None, ub=None, mode='single', n_workers=None, termination=None)[source]¶
Bases:
BaseModelBase class for Metaheuristic-Optimized Wavelet Neural Network (MhaWNN) models.
This class serves as a foundation for constructing Wavelet Neural Networks (WNNs) optimized using various metaheuristic algorithms from the Mealpy library. It supports both regression and classification tasks, offering flexible model configuration, optimization setup, and training management.
- Parameters:
size_hidden (int, optional (default=10)) – Number of hidden neurons in the WNN.
wavelet_fn (str, optional (default="morlet")) – Name of the wavelet basis function used in hidden layers.
act_output (callable or None, optional (default=None)) – Activation function for the output layer.
optim (str, optional (default="Adam")) – Name of the metaheuristic optimizer. Must be supported by Mealpy.
optim_params (dict, optional (default=None)) – Dictionary of parameters to configure the optimizer.
obj_name (str or None, optional (default=None)) – Name of the objective function or performance metric (e.g., “mse”, “accuracy”).
seed (int, optional (default=42)) – Random seed for reproducibility.
verbose (bool, optional (default=True)) – If True, prints optimization progress during training.
wnn_type (str or subclass of BaseCustomWNN, optional (default=None)) – Type or custom implementation of the Wavelet Neural Network architecture.
lb (float, int, list, or np.ndarray, optional (default=None)) – Lower bounds for the model’s trainable parameters.
ub (float, int, list, or np.ndarray, optional (default=None)) – Upper bounds for the model’s trainable parameters.
mode (str, optional (default='single')) – Mode for optimization (‘single’ or ‘swarm’, ‘thread’ or ‘process’).
n_workers (int or None, optional (default=None)) – Number of parallel workers used in optimizer.
termination (dict or callable, optional (default=None)) – Termination condition for the optimization process.
- size_input¶
Number of features in the input dataset.
- Type:
int or None
- size_output¶
Number of output targets.
- Type:
int or None
- network¶
Constructed wavelet neural network.
- Type:
torch.nn.Module or None
- optimizer¶
Metaheuristic optimizer instance.
- Type:
Optimizer or None
- metric_class¶
Metric computation class based on selected objective.
- Type:
callable or None
- loss_train¶
History of training loss over optimization iterations.
- Type:
list
- minmax¶
Indicates whether the optimization is minimizing or maximizing the objective.
- Type:
str or None
- data¶
Training data (X, y) used during optimization.
- Type:
tuple
- _set_optimizer(optim=None, optim_params=None)[source]¶
Initializes the optimizer using a string or an Optimizer instance.
- _set_lb_ub(lb=None, ub=None, n_dims=None)[source]¶
Normalizes and validates lower and upper bounds for optimization.
- objective_function(solution=None)[source]¶
Evaluates the loss/fitness of the model given a parameter solution.
- 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]¶
Builds the model architecture and sets the optimizer and loss function based on the task.
- Raises:
ValueError – If the task is not recognized.
- 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
- class waveletml.models.mha_wnn.MhaWnnClassifier(size_hidden=10, wavelet_fn='morlet', act_output=None, optim='Adam', optim_params=None, obj_name=None, seed=42, verbose=True, wnn_type: str | Type[BaseCustomWNN] | None = None, lb=None, ub=None, mode='single', n_workers=None, termination=None)[source]¶
Bases:
BaseMhaWnnModel,ClassifierMixinMetaheuristic-based Wavelet Neural Network (MhaWNN) Classifier.
A classifier that combines wavelet neural networks (WNNs) with metaheuristic optimization techniques to perform supervised classification tasks. The model architecture is based on customizable wavelet functions, and it leverages population-based optimizers to train the weights of the WNN.
- Parameters:
size_hidden (int, optional (default=10)) – Number of hidden neurons in the wavelet neural network.
wavelet_fn (str, optional (default="morlet")) – Name of the wavelet function to use in hidden layers.
act_output (callable or None, optional (default=None)) – Activation function for the output layer.
optim (str, optional (default="Adam")) – Name of the metaheuristic optimizer to use. Must be supported by the Mealpy library.
optim_params (dict or None, optional (default=None)) – Parameters to configure the optimizer. If None, default parameters are used.
obj_name (str or None, optional (default=None)) – Name of the classification objective function (metric) to optimize.
seed (int, optional (default=42)) – Random seed for reproducibility.
verbose (bool, optional (default=True)) – If True, prints progress during optimization.
wnn_type (str, type, or None, optional (default=None)) –
- Type of wavelet neural network to use. Accepts:
A string name of a WNN class from waveletml.models.custom_wnn
A class object that inherits from BaseCustomWNN
None to use the default CustomWaveletWeightedLinearNetwork
lb (float, int, list, tuple, or np.ndarray, optional) – Lower bounds for optimization. If not provided, defaults to -1.0 for each dimension.
ub (float, int, list, tuple, or np.ndarray, optional) – Upper bounds for optimization. If not provided, defaults to 1.0 for each dimension.
mode (str, optional (default="single")) – Optimization mode (‘single’, ‘parallel’, etc.).
n_workers (int or None, optional) – Number of workers for parallel optimization (only used in parallel mode).
termination (Any, optional) – Termination condition for the optimizer.
- size_input¶
Number of input features in the dataset.
- Type:
int
- size_output¶
Number of output classes (1 for binary classification, C for multi-class).
- Type:
int
- network¶
Instantiated wavelet neural network model.
- Type:
torch.nn.Module
- optimizer¶
Instantiated metaheuristic optimizer.
- Type:
Optimizer
- classes_¶
Sorted array of unique class labels.
- Type:
np.ndarray
- task¶
Task type, either ‘binary_classification’ or ‘classification’.
- Type:
str
- minmax¶
Direction of optimization (‘min’ or ‘max’), based on obj_name.
- Type:
str
- loss_train¶
List of training loss values (objective metric) for each epoch.
- Type:
list of float
- metric_class¶
Metric class used for evaluation (set to ClassificationMetric).
- Type:
callable
- fit(X, y)[source]¶
Trains the classifier using metaheuristic optimization on provided training data.
- evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]¶
Evaluates model performance using specified classification metrics.
- 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:
- 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.
- class waveletml.models.mha_wnn.MhaWnnRegressor(size_hidden=10, wavelet_fn='morlet', act_output=None, optim='Adam', optim_params=None, obj_name=None, seed=42, verbose=True, wnn_type: str | Type[BaseCustomWNN] | None = None, lb=None, ub=None, mode='single', n_workers=None, termination=None)[source]¶
Bases:
BaseMhaWnnModel,RegressorMixinMetaheuristic-based Wavelet Neural Network (MhaWNN) Regressor.
A regressor that combines wavelet neural networks (WNNs) with metaheuristic optimization algorithms to solve single- and multi-output regression problems. The model leverages customizable wavelet functions in its architecture, and uses population-based optimizers to train the network parameters.
- Parameters:
size_hidden (int, optional (default=10)) – Number of hidden neurons in the wavelet neural network.
wavelet_fn (str, optional (default="morlet")) – Name of the wavelet function to use in the hidden layer.
act_output (callable or None, optional (default=None)) – Activation function to apply at the output layer.
optim (str, optional (default="Adam")) – Name of the metaheuristic optimizer to use. Must be supported by Mealpy.
optim_params (dict or None, optional (default=None)) – Additional parameters for the optimizer. If None, defaults are used.
obj_name (str or None, optional (default=None)) – Name of the objective function (metric) to optimize. Must be supported by permetrics.
seed (int, optional (default=42)) – Random seed for reproducibility.
verbose (bool, optional (default=True)) – If True, prints progress and logs during training.
wnn_type (str, type, or None, optional (default=None)) –
- Specifies the type of WNN to use. Options include:
String name of a WNN class defined in waveletml.models.custom_wnn
A subclass of BaseCustomWNN
None to use the default CustomWaveletWeightedLinearNetwork
lb (float, int, list, tuple, or np.ndarray, optional) – Lower bounds for the optimizer. Defaults to -1.0 for all weights if not set.
ub (float, int, list, tuple, or np.ndarray, optional) – Upper bounds for the optimizer. Defaults to 1.0 for all weights if not set.
mode (str, optional (default="single")) – Optimization mode, e.g., ‘single’ or ‘swarm’, ‘thread’, or ‘process’.
n_workers (int or None, optional) – Number of parallel workers (if supported by the optimizer).
termination (any, optional) – Termination criteria for the optimizer.
- size_input¶
Number of input features.
- Type:
int
- size_output¶
Number of regression targets (1 for single-output, >1 for multi-output).
- Type:
int
- network¶
Instantiated wavelet neural network model.
- Type:
torch.nn.Module
- optimizer¶
Configured metaheuristic optimizer.
- Type:
Optimizer
- task¶
Type of regression task: ‘regression’ or ‘multi_regression’.
- Type:
str
- minmax¶
Optimization direction (‘min’ or ‘max’) based on objective function.
- Type:
str
- metric_class¶
Metric class used for evaluation (set to RegressionMetric).
- Type:
callable
- loss_train¶
Training losses (metric values) recorded over epochs.
- Type:
list of float
- evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]¶
Evaluates regression performance using selected metrics.
- 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: