xanfis.helpers package

xanfis.helpers.membership_family module

class xanfis.helpers.membership_family.BaseMembership[source]

Bases: Module

Base class for membership functions.

This class defines the interface for all membership functions. Subclasses must implement the forward method to calculate membership values.

forward(X: Tensor) Tensor[source]

Calculate membership values for the given input tensor.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

Raises:

NotImplementedError – If not implemented by subclass.

get_parameters() Dict[str, Tensor][source]

Retrieve the current parameters of the membership function.

Returns:

Dictionary containing parameter names and values.

Return type:

Dict[str, torch.Tensor]

name()[source]
training: bool
class xanfis.helpers.membership_family.GBellMembership(input_dim)[source]

Bases: BaseMembership

Generalized Bell-shaped membership function implementation.

This membership function is defined by a smooth, symmetric bell curve.

Parameters:

input_dim (int) – Number of input features.

a

Width of the bell curve.

Type:

nn.Parameter

b

Slope of the bell curve.

Type:

nn.Parameter

c

Center of the bell curve.

Type:

nn.Parameter

forward(x)[source]

Calculate membership values for the given input tensor.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

Raises:

NotImplementedError – If not implemented by subclass.

training: bool
class xanfis.helpers.membership_family.GaussianMembership(input_dim: int)[source]

Bases: BaseMembership

Gaussian membership function implementation.

forward(X: Tensor) Tensor[source]

Calculate Gaussian membership values.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

training: bool
class xanfis.helpers.membership_family.LinearMembership(input_dim)[source]

Bases: BaseMembership

Linear membership function implementation.

This membership function is defined by a linear ramp between two points.

Parameters:

input_dim (int) – Number of input features.

a

Start of the ramp.

Type:

nn.Parameter

b

End of the ramp.

Type:

nn.Parameter

forward(x)[source]

Calculate membership values for the given input tensor.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

Raises:

NotImplementedError – If not implemented by subclass.

training: bool
class xanfis.helpers.membership_family.PiShapedMembership(input_dim)[source]

Bases: BaseMembership

Pi-shaped membership function implementation.

This membership function is characterized by a rising edge, a plateau, and a falling edge, forming a shape similar to the Greek letter Pi (π).

Parameters:

input_dim (int) – Number of input features.

a

Start of the rising edge.

Type:

nn.Parameter

b

Start of the plateau.

Type:

nn.Parameter

c

End of the plateau.

Type:

nn.Parameter

d

End of the falling edge.

Type:

nn.Parameter

forward(x)[source]

Calculate membership values for the given input tensor.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

Raises:

NotImplementedError – If not implemented by subclass.

training: bool
class xanfis.helpers.membership_family.SShapedMembership(input_dim)[source]

Bases: BaseMembership

S-shaped membership function implementation.

This membership function is characterized by a smooth transition from 0 to 1, forming an S-like shape.

Parameters:

input_dim (int) – Number of input features.

a

Start of the transition.

Type:

nn.Parameter

b

End of the transition.

Type:

nn.Parameter

forward(x)[source]

Calculate membership values for the given input tensor.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

Raises:

NotImplementedError – If not implemented by subclass.

training: bool
class xanfis.helpers.membership_family.SigmoidMembership(input_dim: int)[source]

Bases: BaseMembership

Sigmoid membership function implementation.

forward(X: Tensor) Tensor[source]

Calculate Sigmoid membership values.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

training: bool
class xanfis.helpers.membership_family.TrapezoidalMembership(input_dim: int)[source]

Bases: BaseMembership

Trapezoidal membership function implementation.

forward(x: Tensor) Tensor[source]

Calculate Trapezoidal membership values.

Parameters:

x (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

training: bool
class xanfis.helpers.membership_family.TriangularMembership(input_dim: int)[source]

Bases: BaseMembership

Triangular membership function implementation.

forward(X: Tensor) Tensor[source]

Calculate Triangular membership values.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

training: bool
class xanfis.helpers.membership_family.ZShapedMembership(input_dim)[source]

Bases: BaseMembership

Z-shaped membership function implementation.

This membership function is characterized by a smooth transition from 1 to 0, forming a Z-like shape.

Parameters:

input_dim (int) – Number of input features.

a

Start of the transition.

Type:

nn.Parameter

b

End of the transition.

Type:

nn.Parameter

forward(x)[source]

Calculate membership values for the given input tensor.

Parameters:

X (torch.Tensor) – Input tensor of shape (batch_size, input_dim).

Returns:

Tensor of membership values of shape (batch_size,).

Return type:

torch.Tensor

Raises:

NotImplementedError – If not implemented by subclass.

training: bool

xanfis.helpers.metric_util module

xanfis.helpers.metric_util.get_all_classification_metrics()[source]

Gets a dictionary of all supported classification metrics.

This function returns a dictionary where keys are metric names and values are their optimization types (“min” or “max”).

Returns:

A dictionary containing all supported classification metrics.

Return type:

dict

xanfis.helpers.metric_util.get_all_regression_metrics()[source]

Gets a dictionary of all supported regression metrics.

This function returns a dictionary where keys are metric names and values are their optimization types (“min” or “max”).

Returns:

A dictionary containing all supported regression metrics.

Return type:

dict

xanfis.helpers.metric_util.get_metric_sklearn(task='classification', metric_names=None)[source]

Creates a dictionary of scorers for scikit-learn cross-validation.

This function takes the task type (classification or regression) and a list of metric names. It creates an appropriate metrics instance (ClassificationMetric or RegressionMetric) and iterates through the provided metric names. For each metric name, it checks if it exists in the metrics instance and retrieves the corresponding method. Finally, it uses make_scorer to convert the method to a scorer and adds it to a dictionary.

Parameters:
  • task (str, optional) – The task type, either “classification” or “regression”. Defaults to “classification”.

  • metric_names (list, optional) – A list of metric names. Defaults to None.

Returns:

A dictionary of scorers for scikit-learn cross-validation.

Return type:

dict

xanfis.helpers.metric_util.get_metrics(problem, y_true, y_pred, metrics=None, testcase='test')[source]

Calculates metrics for regression or classification tasks.

This function takes the true labels (y_true), predicted labels (y_pred), problem type (regression or classification), a dictionary or list of metrics to calculate, and an optional test case name. It returns a dictionary containing the calculated metrics with descriptive names.

Parameters:
  • problem (str) – The type of problem, either “regression” or “classification”.

  • y_true (array-like) – The true labels.

  • y_pred (array-like) – The predicted labels.

  • metrics (dict or list, optional) – A dictionary or list of metrics to calculate. Defaults to None.

  • testcase (str, optional) – An optional test case name to prepend to the metric names. Defaults to “test”.

Returns:

A dictionary containing the calculated metrics with descriptive names.

Return type:

dict

Raises:

ValueError – If the metrics parameter is not a list or dictionary.

xanfis.helpers.preprocessor module

class xanfis.helpers.preprocessor.Data(X=None, y=None, name='Unknown')[source]

Bases: object

The structure of our supported Data class

Parameters:
  • X (np.ndarray) – The features of your data

  • y (np.ndarray) – The labels of your data

SUPPORT = {'scaler': ['standard', 'minmax', 'max-abs', 'log1p', 'loge', 'sqrt', 'sinh-arc-sinh', 'robust', 'box-cox', 'yeo-johnson']}
static check_y(y)[source]
static encode_label(y)[source]
static scale(X, scaling_methods=('standard',), list_dict_paras=None)[source]
set_train_test(X_train=None, y_train=None, X_test=None, y_test=None)[source]

Function use to set your own X_train, y_train, X_test, y_test in case you don’t want to use our split function

Parameters:
  • X_train (np.ndarray) –

  • y_train (np.ndarray) –

  • X_test (np.ndarray) –

  • y_test (np.ndarray) –

split_train_test(test_size=0.2, train_size=None, random_state=41, shuffle=True, stratify=None, inplace=True)[source]

The wrapper of the split_train_test function in scikit-learn library.

class xanfis.helpers.preprocessor.FeatureEngineering[source]

Bases: object

create_threshold_binary_features(X, threshold)[source]

Perform feature engineering to add binary indicator columns for values below the threshold. Add each new column right after the corresponding original column.

Args: X (numpy.ndarray): The input 2D matrix of shape (n_samples, n_features). threshold (float): The threshold value for identifying low values.

Returns: numpy.ndarray: The updated 2D matrix with binary indicator columns.

class xanfis.helpers.preprocessor.LabelEncoder[source]

Bases: object

Encode categorical features as integer labels.

static check_y(y)[source]
fit(y)[source]

Fit label encoder to a given set of labels.

Parameters:

yarray-like

Labels to encode.

fit_transform(y)[source]

Fit label encoder and return encoded labels.

Parameters:

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

Returns:

y – Encoded labels.

Return type:

array-like of shape (n_samples,)

inverse_transform(y)[source]

Transform integer labels to original labels.

Parameters:

yarray-like

Encoded integer labels.

Returns:

original_labelsarray-like

Original labels.

transform(y)[source]

Transform labels to encoded integer labels.

Parameters:

yarray-like (1-D vector)

Labels to encode.

Returns:

encoded_labelsarray-like

Encoded integer labels.

class xanfis.helpers.preprocessor.OneHotEncoder[source]

Bases: object

fit(X)[source]

Fit the encoder to unique categories in X.

fit_transform(X)[source]

Fit the encoder to X and transform X.

inverse_transform(one_hot)[source]

Convert one-hot encoded format back to original categories.

transform(X)[source]

Transform X into one-hot encoded format.

class xanfis.helpers.preprocessor.TimeSeriesDifferencer(interval=1)[source]

Bases: object

difference(X)[source]
inverse_difference(diff_data)[source]

xanfis.helpers.scaler_util module

class xanfis.helpers.scaler_util.BoxCoxScaler(lmbda=None)[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class xanfis.helpers.scaler_util.DataTransformer(scaling_methods=('standard',), list_dict_paras=None)[source]

Bases: BaseEstimator, TransformerMixin

SUPPORTED_SCALERS = {'box-cox': <class 'xanfis.helpers.scaler_util.BoxCoxScaler'>, 'log1p': <class 'xanfis.helpers.scaler_util.Log1pScaler'>, 'loge': <class 'xanfis.helpers.scaler_util.LogeScaler'>, 'max-abs': <class 'sklearn.preprocessing._data.MaxAbsScaler'>, 'minmax': <class 'sklearn.preprocessing._data.MinMaxScaler'>, 'robust': <class 'sklearn.preprocessing._data.RobustScaler'>, 'sinh-arc-sinh': <class 'xanfis.helpers.scaler_util.SinhArcSinhScaler'>, 'sqrt': <class 'xanfis.helpers.scaler_util.SqrtScaler'>, 'standard': <class 'sklearn.preprocessing._data.StandardScaler'>, 'yeo-johnson': <class 'xanfis.helpers.scaler_util.YeoJohnsonScaler'>}
fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class xanfis.helpers.scaler_util.Log1pScaler[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class xanfis.helpers.scaler_util.LogeScaler[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class xanfis.helpers.scaler_util.ObjectiveScaler(obj_name='sigmoid', ohe_scaler=None)[source]

Bases: object

For label scaler in classification (binary and multiple classification)

inverse_transform(data)[source]
transform(data)[source]
class xanfis.helpers.scaler_util.SinhArcSinhScaler(epsilon=0.1, delta=1.0)[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class xanfis.helpers.scaler_util.SqrtScaler[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class xanfis.helpers.scaler_util.YeoJohnsonScaler(lmbda=None)[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]

xanfis.helpers.validator module

xanfis.helpers.validator.check_bool(name: str, value: bool, bound=(True, False))[source]
xanfis.helpers.validator.check_float(name: str, value: None, bound=None)[source]
xanfis.helpers.validator.check_int(name: str, value: None, bound=None)[source]
xanfis.helpers.validator.check_str(name: str, value: str, bound=None)[source]
xanfis.helpers.validator.check_tuple_float(name: str, values: tuple, bounds=None)[source]
xanfis.helpers.validator.check_tuple_int(name: str, values: None, bounds=None)[source]
xanfis.helpers.validator.is_in_bound(value, bound)[source]
xanfis.helpers.validator.is_str_in_sequence(value: str, my_list: any)[source]