reflame.model package

reflame.model.mha_flnn module

class reflame.model.mha_flnn.MhaFlnnClassifier(expand_name='chebyshev', n_funcs=4, act_name='elu', obj_name=None, optimizer='BaseGA', optimizer_paras=None, verbose=False)[source]

Bases: reflame.base_flnn.BaseMhaFlnn, sklearn.base.ClassifierMixin

Defines the general class of Metaheuristic-based FLNN model for Classification problems that inherit the BaseMhaFlnn and ClassifierMixin classes.

Parameters
  • expand_name (str, default="chebyshev") – The expand function that will be used. The supported expand functions are: {“chebyshev”, “legendre”, “gegenbauer”, “laguerre”, “hermite”, “power”, “trigonometric”}

  • n_funcs (int, default=4) – The first n_funcs in expand functions list will be used. Valid value from 1 to 10.

  • act_name (str, default='sigmoid') – Activation function for the hidden layer. The supported activation functions are: {“relu”, “prelu”, “gelu”, “elu”, “selu”, “rrelu”, “tanh”, “hard_tanh”, “sigmoid”, “hard_sigmoid”, “swish”, “hard_swish”, “soft_plus”, “mish”, “soft_sign”, “tanh_shrink”, “soft_shrink”, “hard_shrink”}

  • obj_name (str, default="AS") – Current supported objective functions, please check it here: https://github.com/thieu1995/permetrics

  • optimizer (str or instance of Optimizer class (from Mealpy library), default = "BaseGA") – The Metaheuristic Algorithm that use to solve the feature selection problem. Current supported list, please check it here: https://github.com/thieu1995/mealpy. If a custom optimizer is passed, make sure it is an instance of Optimizer class.

  • optimizer_paras (None or dict of parameter, default=None) – The parameter for the optimizer object. If None, the default parameters of optimizer is used (defined in https://github.com/thieu1995/mealpy.) If dict is passed, make sure it has at least epoch and pop_size parameters.

  • verbose (bool, default=False) – Whether to print progress messages to stdout.

  • [Optional] (obj_weights) – The objective weights for multiple objective functions

Examples

>>> from reflame import Data, MhaFlnnClassifier
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=100, random_state=1)
>>> data = Data(X, y)
>>> data.split_train_test(test_size=0.2, random_state=1)
>>> data.X_train_scaled, scaler = data.scale(data.X_train, method="MinMaxScaler")
>>> data.X_test_scaled = scaler.transform(data.X_test)
>>> opt_paras = {"name": "GA", "epoch": 10, "pop_size": 30}
>>> print(MhaFlnnClassifier.SUPPORTED_CLS_OBJECTIVES)
{'PS': 'max', 'NPV': 'max', 'RS': 'max', ...., 'KLDL': 'min', 'BSL': 'min'}
>>> model = MhaFlnnClassifier(hidden_size=10, act_name="elu", obj_name="BSL", optimizer="BaseGA", optimizer_paras=opt_paras)
>>> model.fit(data.X_train_scaled, data.y_train)
>>> pred = model.predict(data.X_test_scaled)
>>> print(pred)
array([1, 0, 1, 0, 1])
CLS_OBJ_LOSSES = ['CEL', 'HL', 'KLDL', 'BSL']
create_network(X, y)[source]
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")) – You can get metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

objective_function(solution=None)[source]

Evaluates the fitness function for classification metric

Parameters

solution (np.ndarray, default=None) –

Returns

result – The fitness value

Return type

float

score(X, y, method='AS')[source]

Return the metric on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.

  • method (str, default="AS") – You can get all metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

result – The result of selected metric

Return type

float

scores(X, y, list_methods=('AS', 'RS'))[source]

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

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.

  • list_methods (list, default=("AS", "RS")) – You can get all metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

set_fit_request(*, lb: Union[bool, None, str] = '$UNCHANGED$', save_population: Union[bool, None, str] = '$UNCHANGED$', ub: Union[bool, None, str] = '$UNCHANGED$') reflame.model.mha_flnn.MhaFlnnClassifier

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters
  • lb (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for lb parameter in fit.

  • save_population (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for save_population parameter in fit.

  • ub (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for ub parameter in fit.

Returns

self – The updated object.

Return type

object

set_predict_request(*, return_prob: Union[bool, None, str] = '$UNCHANGED$') reflame.model.mha_flnn.MhaFlnnClassifier

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

return_prob (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_prob parameter in predict.

Returns

self – The updated object.

Return type

object

set_score_request(*, method: Union[bool, None, str] = '$UNCHANGED$') reflame.model.mha_flnn.MhaFlnnClassifier

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in score.

Returns

self – The updated object.

Return type

object

class reflame.model.mha_flnn.MhaFlnnRegressor(expand_name='chebyshev', n_funcs=4, act_name='elu', obj_name='MSE', optimizer='BaseGA', optimizer_paras=None, verbose=False, obj_weights=None)[source]

Bases: reflame.base_flnn.BaseMhaFlnn, sklearn.base.RegressorMixin

Defines the general class of Metaheuristic-based FLNN model for Regression problems that inherit the BaseMhaFlnn and RegressorMixin classes.

Parameters
  • expand_name (str, default="chebyshev") – The expand function that will be used. The supported expand functions are: {“chebyshev”, “legendre”, “gegenbauer”, “laguerre”, “hermite”, “power”, “trigonometric”}

  • n_funcs (int, default=4) – The first n_funcs in expand functions list will be used. Valid value from 1 to 10.

  • act_name (str, default='sigmoid') – Activation function for the hidden layer. The supported activation functions are: {“relu”, “prelu”, “gelu”, “elu”, “selu”, “rrelu”, “tanh”, “hard_tanh”, “sigmoid”, “hard_sigmoid”, “swish”, “hard_swish”, “soft_plus”, “mish”, “soft_sign”, “tanh_shrink”, “soft_shrink”, “hard_shrink”}

  • obj_name (str, default="MSE") – Current supported objective functions, please check it here: https://github.com/thieu1995/permetrics

  • optimizer (str or instance of Optimizer class (from Mealpy library), default = "BaseGA") – The Metaheuristic Algorithm that use to solve the feature selection problem. Current supported list, please check it here: https://github.com/thieu1995/mealpy. If a custom optimizer is passed, make sure it is an instance of Optimizer class.

  • optimizer_paras (None or dict of parameter, default=None) – The parameter for the optimizer object. If None, the default parameters of optimizer is used (defined in https://github.com/thieu1995/mealpy.) If dict is passed, make sure it has at least epoch and pop_size parameters.

  • verbose (bool, default=False) – Whether to print progress messages to stdout.

  • [Optional] (obj_weights) – The objective weights for multiple objective functions

Examples

>>> from reflame import MhaFlnnRegressor, Data
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=200, random_state=1)
>>> data = Data(X, y)
>>> data.split_train_test(test_size=0.2, random_state=1)
>>> data.X_train_scaled, scaler = data.scale(data.X_train, method="MinMaxScaler")
>>> data.X_test_scaled = scaler.transform(data.X_test)
>>> opt_paras = {"name": "GA", "epoch": 10, "pop_size": 30}
>>> model = MhaFlnnRegressor(expand_name="chebyshev", n_funcs=4, act_name="elu", obj_name="RMSE", optimizer="BaseGA", optimizer_paras=opt_paras)
>>> model.fit(data.X_train_scaled, data.y_train)
>>> pred = model.predict(data.X_test_scaled)
>>> print(pred)
create_network(X, y)[source]
Returns

  • network (FLNN, an instance of FLNN network)

  • obj_scaler (ObjectiveScaler, the objective scaler that used to scale output)

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

Return the list of performance metrics of the prediction.

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=("MSE", "MAE")) – You can get metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

objective_function(solution=None)[source]

Evaluates the fitness function for regression metric

Parameters

solution (np.ndarray, default=None) –

Returns

result – The fitness value

Return type

float

score(X, y, method='RMSE')[source]

Return the metric of the prediction.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

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

  • method (str, default="RMSE") – You can get all metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

result – The result of selected metric

Return type

float

scores(X, y, list_methods=('MSE', 'MAE'))[source]

Return the list of metrics of the prediction.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

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

  • list_methods (list, default=("MSE", "MAE")) – You can get all metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

set_fit_request(*, lb: Union[bool, None, str] = '$UNCHANGED$', save_population: Union[bool, None, str] = '$UNCHANGED$', ub: Union[bool, None, str] = '$UNCHANGED$') reflame.model.mha_flnn.MhaFlnnRegressor

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters
  • lb (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for lb parameter in fit.

  • save_population (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for save_population parameter in fit.

  • ub (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for ub parameter in fit.

Returns

self – The updated object.

Return type

object

set_predict_request(*, return_prob: Union[bool, None, str] = '$UNCHANGED$') reflame.model.mha_flnn.MhaFlnnRegressor

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

return_prob (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_prob parameter in predict.

Returns

self – The updated object.

Return type

object

set_score_request(*, method: Union[bool, None, str] = '$UNCHANGED$') reflame.model.mha_flnn.MhaFlnnRegressor

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in score.

Returns

self – The updated object.

Return type

object

reflame.model.standard_flnn module

class reflame.model.standard_flnn.FlnnClassifier(expand_name='chebyshev', n_funcs=4, act_name='relu', obj_name='NLLL', max_epochs=1000, batch_size=32, optimizer='SGD', optimizer_paras=None, verbose=False, **kwargs)[source]

Bases: reflame.base_flnn_torch.BaseFlnn

Defines the class for traditional FLNN network for Classification problems that inherit the BaseFlnn and ClassifierMixin classes.

Parameters
  • expand_name (str, default="chebyshev") – The expand function that will be used. The supported expand functions are: {“chebyshev”, “legendre”, “gegenbauer”, “laguerre”, “hermite”, “power”, “trigonometric”}

  • n_funcs (int, default=4) – The first n_funcs in expand functions list will be used. Valid value from 1 to 10.

  • act_name ({"relu", "leaky_relu", "celu", "prelu", "gelu", "elu", "selu", "rrelu", "tanh", "hard_tanh", "sigmoid",) – “hard_sigmoid”, “log_sigmoid”, “silu”, “swish”, “hard_swish”, “soft_plus”, “mish”, “soft_sign”, “tanh_shrink”, “soft_shrink”, “hard_shrink”, “softmin”, “softmax”, “log_softmax” }, default=’sigmoid’ Activation function for the hidden layer.

  • obj_name (str, default=NLLL) – The name of objective for classification problem (binary and multi-class classification)

  • max_epochs (int, default=1000) – Maximum number of epochs / iterations / generations

  • batch_size (int, default=32) – The batch size

  • optimizer (str, default = "SGD") – The gradient-based optimizer from Pytorch. List of supported optimizer is: [“Adadelta”, “Adagrad”, “Adam”, “Adamax”, “AdamW”, “ASGD”, “LBFGS”, “NAdam”, “RAdam”, “RMSprop”, “Rprop”, “SGD”]

  • optimizer_paras (dict or None, default=None) – The dictionary parameters of the selected optimizer.

  • verbose (bool, default=True) – Whether to print progress messages to stdout.

Examples

>>> from reflame import FlnnClassifier, Data
>>> from sklearn.datasets import make_regression
>>>
>>> ## Make dataset
>>> X, y = make_regression(n_samples=200, n_features=10, random_state=1)
>>> ## Load data object
>>> data = Data(X, y)
>>> ## Split train and test
>>> data.split_train_test(test_size=0.2, random_state=1, inplace=True)
>>> ## Scale dataset
>>> data.X_train, scaler = data.scale(data.X_train, scaling_methods=("minmax"))
>>> data.X_test = scaler.transform(data.X_test)
>>> ## Create model
>>> model = FlnnClassifier(expand_name="chebyshev", n_funcs=4, act_name="elu",
>>>                          obj_name="CEL", max_epochs=100, batch_size=32, optimizer="SGD", verbose=True)
>>> ## Train the model
>>> model.fit(data.X_train, data.y_train)
>>> ## Test the model
>>> y_pred = model.predict(data.X_test)
>>> ## Calculate some metrics
>>> print(model.score(X=data.X_test, y=data.y_test, method="RMSE"))
>>> print(model.scores(X=data.X_test, y=data.y_test, list_methods=["R2", "NSE", "MAPE"]))
>>> print(model.evaluate(y_true=data.y_test, y_pred=y_pred, list_metrics=["R2", "NSE", "MAPE", "NNSE"]))
CLS_OBJ_BINARY_1 = ['PNLLL', 'HEL', 'BCEL', 'CEL', 'BCELL']
CLS_OBJ_BINARY_2 = ['NLLL']
CLS_OBJ_LOSSES = ['CEL', 'HEL', 'KLDL']
CLS_OBJ_MULTI = ['NLLL', 'CEL']
SUPPORTED_LOSSES = {'BCEL': <class 'torch.nn.modules.loss.BCELoss'>, 'BCELL': <class 'torch.nn.modules.loss.BCEWithLogitsLoss'>, 'CEL': <class 'torch.nn.modules.loss.CrossEntropyLoss'>, 'GNLLL': <class 'torch.nn.modules.loss.GaussianNLLLoss'>, 'HEL': <class 'torch.nn.modules.loss.HingeEmbeddingLoss'>, 'KLDL': <class 'torch.nn.modules.loss.KLDivLoss'>, 'NLLL': <class 'torch.nn.modules.loss.NLLLoss'>, 'PNLLL': <class 'torch.nn.modules.loss.PoissonNLLLoss'>}
create_network(X, y) Tuple[skorch.classifier.NeuralNetClassifier, reflame.utils.data_toolkit.ObjectiveScaler][source]
Returns

  • network (FLNN, an instance of FLNN network)

  • obj_scaler (ObjectiveScaler, the objective scaler that used to scale output)

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")) – You can get metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

fit(X, y)[source]
score(X, y, method='AS')[source]

Return the metric on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.

  • method (str, default="AS") – You can get all of the metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

result – The result of selected metric

Return type

float

scores(X, y, list_methods=('AS', 'RS'))[source]

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

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.

  • list_methods (list, default=("AS", "RS")) – You can get all of the metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

set_predict_request(*, return_prob: Union[bool, None, str] = '$UNCHANGED$') reflame.model.standard_flnn.FlnnClassifier

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

return_prob (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_prob parameter in predict.

Returns

self – The updated object.

Return type

object

set_score_request(*, method: Union[bool, None, str] = '$UNCHANGED$') reflame.model.standard_flnn.FlnnClassifier

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in score.

Returns

self – The updated object.

Return type

object

class reflame.model.standard_flnn.FlnnRegressor(expand_name='chebyshev', n_funcs=4, act_name='elu', obj_name='MSE', max_epochs=1000, batch_size=32, optimizer='SGD', optimizer_paras=None, verbose=False, **kwargs)[source]

Bases: reflame.base_flnn_torch.BaseFlnn

Defines the class for traditional FLNN network for Regression problems that inherit the BaseFlnn and RegressorMixin classes.

Parameters
  • expand_name (str, default="chebyshev") – The expand function that will be used. The supported expand functions are: {“chebyshev”, “legendre”, “gegenbauer”, “laguerre”, “hermite”, “power”, “trigonometric”}

  • n_funcs (int, default=4) – The first n_funcs in expand functions list will be used. Valid value from 1 to 10.

  • act_name ({"relu", "leaky_relu", "celu", "prelu", "gelu", "elu", "selu", "rrelu", "tanh", "hard_tanh", "sigmoid",) – “hard_sigmoid”, “log_sigmoid”, “silu”, “swish”, “hard_swish”, “soft_plus”, “mish”, “soft_sign”, “tanh_shrink”, “soft_shrink”, “hard_shrink”, “softmin”, “softmax”, “log_softmax” }, default=’sigmoid’ Activation function for the hidden layer.

  • obj_name (str, default=None) – The name of objective for the problem, also depend on the problem is classification and regression.

  • max_epochs (int, default=1000) – Maximum number of epochs / iterations / generations

  • batch_size (int, default=32) – The batch size

  • optimizer (str, default = "SGD") – The gradient-based optimizer from Pytorch. List of supported optimizer is: [“Adadelta”, “Adagrad”, “Adam”, “Adamax”, “AdamW”, “ASGD”, “LBFGS”, “NAdam”, “RAdam”, “RMSprop”, “Rprop”, “SGD”]

  • optimizer_paras (dict or None, default=None) – The dictionary parameters of the selected optimizer.

  • verbose (bool, default=True) – Whether to print progress messages to stdout.

Examples

>>> from reflame import FlnnRegressor, Data
>>> from sklearn.datasets import make_regression
>>>
>>> ## Make dataset
>>> X, y = make_regression(n_samples=200, n_features=10, random_state=1)
>>> ## Load data object
>>> data = Data(X, y)
>>> ## Split train and test
>>> data.split_train_test(test_size=0.2, random_state=1, inplace=True)
>>> ## Scale dataset
>>> data.X_train, scaler = data.scale(data.X_train, scaling_methods=("minmax"))
>>> data.X_test = scaler.transform(data.X_test)
>>> ## Create model
>>> model = FlnnRegressor(expand_name="chebyshev", n_funcs=4, act_name="elu",
>>>                          obj_name="MSE", max_epochs=100, batch_size=32, optimizer="SGD", verbose=True)
>>> ## Train the model
>>> model.fit(data.X_train, data.y_train)
>>> ## Test the model
>>> y_pred = model.predict(data.X_test)
>>> ## Calculate some metrics
>>> print(model.score(X=data.X_test, y=data.y_test, method="RMSE"))
>>> print(model.scores(X=data.X_test, y=data.y_test, list_methods=["R2", "NSE", "MAPE"]))
>>> print(model.evaluate(y_true=data.y_test, y_pred=y_pred, list_metrics=["R2", "NSE", "MAPE", "NNSE"]))
SUPPORTED_LOSSES = {'MAE': <class 'torch.nn.modules.loss.L1Loss'>, 'MSE': <class 'torch.nn.modules.loss.MSELoss'>}
create_network(X, y)[source]
Returns

  • network (FLNN, an instance of FLNN network)

  • obj_scaler (ObjectiveScaler, the objective scaler that used to scale output)

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

Return the list of performance metrics of the prediction.

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=("MSE", "MAE")) – You can get metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

score(X, y, method='RMSE')[source]

Return the metric of the prediction.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

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

  • method (str, default="RMSE") – You can get all metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

result – The result of selected metric

Return type

float

scores(X, y, list_methods=('MSE', 'MAE'))[source]

Return the list of metrics of the prediction.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

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

  • list_methods (list, default=("MSE", "MAE")) – You can get all metrics from Permetrics library: https://github.com/thieu1995/permetrics

Returns

results – The results of the list metrics

Return type

dict

set_predict_request(*, return_prob: Union[bool, None, str] = '$UNCHANGED$') reflame.model.standard_flnn.FlnnRegressor

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

return_prob (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_prob parameter in predict.

Returns

self – The updated object.

Return type

object

set_score_request(*, method: Union[bool, None, str] = '$UNCHANGED$') reflame.model.standard_flnn.FlnnRegressor

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in score.

Returns

self – The updated object.

Return type

object