graforvfl.shared package

graforvfl.shared.activator module

This module provides a comprehensive collection of activation functions used in machine learning and deep learning. Activation functions play a critical role in neural networks by introducing non-linearity, enabling the network to learn and approximate complex patterns in data.

Functions:

  • none(x):

    A no-op function that returns the input as is.

  • relu(x):

    Rectified Linear Unit (ReLU), returns the input if positive, otherwise returns zero.

  • leaky_relu(x, alpha=0.01):

    Leaky ReLU allows a small gradient when the input is negative.

  • celu(x, alpha=1.0):

    Continuously Differentiable Exponential Linear Unit, a smooth alternative to ReLU.

  • prelu(x, alpha=0.5):

    Parametric ReLU, where the slope for negative inputs is a learnable parameter.

  • gelu(x, alpha=0.044715):

    Gaussian Error Linear Unit, combines tanh approximation for smooth activation.

  • elu(x, alpha=1):

    Exponential Linear Unit, returns an exponential for negative inputs.

  • selu(x, alpha=1.67326324, scale=1.05070098):

    Scaled Exponential Linear Unit, normalizes outputs for self-normalizing networks.

  • rrelu(x, lower=1./8, upper=1./3):

    Randomized Leaky ReLU, introduces randomized slopes for negative inputs.

  • tanh(x):

    Hyperbolic tangent function, outputs values between -1 and 1.

  • hard_tanh(x, lower=-1., upper=1.):

    A clipped version of the tanh function.

  • sigmoid(x):

    Logistic sigmoid function, outputs values between 0 and 1.

  • hard_sigmoid(x, lower=-2.5, upper=2.5):

    A piecewise linear approximation of the sigmoid function.

  • log_sigmoid(x):

    Logarithmic sigmoid function for numerical stability.

  • swish(x):

    Swish (or SiLU), smooth and bounded non-linearity.

  • hard_swish(x, lower=-3., upper=3.):

    A piecewise linear approximation of the swish function.

  • soft_plus(x, beta=1.0):

    Smooth approximation of the ReLU function.

  • mish(x, beta=1.0):

    Mish activation, smooth non-monotonic function.

  • soft_sign(x):

    Smooth approximation of the sign function.

  • tanh_shrink(x):

    Difference between input and tanh, providing a shrinkage effect.

  • soft_shrink(x, alpha=0.5):

    Threshold-based shrinkage operator with soft boundaries.

  • hard_shrink(x, alpha=0.5):

    Hard thresholding function with a predefined alpha.

  • softmin(x):

    Normalizes the negative inputs into a probability distribution.

  • softmax(x):

    Converts inputs into a probability distribution over multiple classes.

  • log_softmax(x):

    Numerically stable logarithmic version of softmax.

Aliases:

  • silu(x): Alias for swish(x).

graforvfl.shared.activator.celu(x, alpha=1.0)[source]
graforvfl.shared.activator.elu(x, alpha=1)[source]
graforvfl.shared.activator.gelu(x, alpha=0.044715)[source]
graforvfl.shared.activator.hard_shrink(x, alpha=0.5)[source]
graforvfl.shared.activator.hard_sigmoid(x, lower=-2.5, upper=2.5)[source]
graforvfl.shared.activator.hard_swish(x, lower=-3.0, upper=3.0)[source]
graforvfl.shared.activator.hard_tanh(x, lower=-1.0, upper=1.0)[source]
graforvfl.shared.activator.leaky_relu(x, alpha=0.01)[source]
graforvfl.shared.activator.log_sigmoid(x)[source]
graforvfl.shared.activator.log_softmax(x)[source]
graforvfl.shared.activator.mish(x, beta=1.0)[source]
graforvfl.shared.activator.none(x)[source]
graforvfl.shared.activator.prelu(x, alpha=0.5)[source]
graforvfl.shared.activator.relu(x)[source]
graforvfl.shared.activator.rrelu(x, lower=0.125, upper=0.3333333333333333)[source]
graforvfl.shared.activator.selu(x, alpha=1.67326324, scale=1.05070098)[source]
graforvfl.shared.activator.sigmoid(x)[source]
graforvfl.shared.activator.silu(x)
graforvfl.shared.activator.soft_plus(x, beta=1.0)[source]
graforvfl.shared.activator.soft_shrink(x, alpha=0.5)[source]
graforvfl.shared.activator.soft_sign(x)[source]
graforvfl.shared.activator.softmax(x)[source]
graforvfl.shared.activator.softmin(x)[source]
graforvfl.shared.activator.swish(x)[source]
graforvfl.shared.activator.tanh(x)[source]
graforvfl.shared.activator.tanh_shrink(x)[source]

graforvfl.shared.boundary_controller module

graforvfl.shared.boundary_controller.check_bool(name: str, value: bool, bound=(True, False))[source]
graforvfl.shared.boundary_controller.check_float(name: str, value: None, bound=None)[source]
graforvfl.shared.boundary_controller.check_int(name: str, value: None, bound=None)[source]
graforvfl.shared.boundary_controller.check_str(name: str, value: str, bound=None)[source]
graforvfl.shared.boundary_controller.check_tuple_float(name: str, values: tuple, bounds=None)[source]
graforvfl.shared.boundary_controller.check_tuple_int(name: str, values: None, bounds=None)[source]
graforvfl.shared.boundary_controller.is_in_bound(value, bound)[source]
graforvfl.shared.boundary_controller.is_str_in_list(value: str, my_list: list)[source]

graforvfl.shared.data_processor module

class graforvfl.shared.data_processor.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 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 graforvfl.shared.data_processor.DataTransformer(scaling_methods=('standard',), list_dict_paras=None)[source]

Bases: BaseEstimator, TransformerMixin

The class is used to transform data using different scaling techniques.

Parameters:
  • scaling_methods (str, tuple, list, or np.ndarray) – The name of the scaler you want to use. Supported scaler names are: ‘standard’, ‘minmax’, ‘max-abs’, ‘log1p’, ‘loge’, ‘sqrt’, ‘sinh-arc-sinh’, ‘robust’, ‘box-cox’, ‘yeo-johnson’.

  • list_dict_paras (dict or list of dict) – The parameters for the scaler. If you have only one scaler, please use a dict. Otherwise, please use a list of dict.

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

Fit the sequence of scalers on the data.

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

  • y (Ignored) – Not used, exists for compatibility with sklearn’s pipeline.

Returns:

self – Fitted transformer.

Return type:

object

inverse_transform(X)[source]

Reverse the transformations applied to the data.

Parameters:

X (array-like) – Transformed data to invert.

Returns:

X_original – Original data before transformation.

Return type:

array-like

transform(X)[source]

Transform the input data using the sequence of fitted scalers.

Parameters:

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

Returns:

X_transformed – Transformed data.

Return type:

array-like

class graforvfl.shared.data_processor.FeatureEngineering[source]

Bases: object

A class for performing custom feature engineering on numeric datasets.

create_threshold_binary_features(X, threshold)[source]

Add binary indicator columns to mark values below a given threshold. Each original column is followed by a new column indicating whether each value is below the threshold (1 if True, 0 otherwise).

Parameters:
  • X (numpy.ndarray) – The input 2D matrix of shape (n_samples, n_features).

  • threshold (float) – The threshold value used to determine binary flags.

Returns:

A new 2D matrix of shape (n_samples, 2 * n_features), where each original column is followed by its binary indicator column.

Return type:

numpy.ndarray

Raises:

ValueError – If X is not a NumPy array or not 2D. If threshold is not a numeric type.

class graforvfl.shared.data_processor.TimeSeriesDifferencer(interval=1)[source]

Bases: object

A class for applying and reversing differencing on time series data.

Differencing helps remove trends and seasonality from time series for better modeling.

difference(X)[source]

Apply differencing to the input time series.

Parameters:

X (array-like) – The original time series data.

Returns:

The differenced time series of length (len(X) - interval).

Return type:

np.ndarray

inverse_difference(diff_data)[source]

Reverse the differencing transformation using the stored original data.

Parameters:

diff_data (array-like) – The differenced data to invert.

Returns:

The reconstructed original data (excluding the first interval values).

Return type:

np.ndarray

Raises:

ValueError – If the original data is not available.

graforvfl.shared.randomer module

graforvfl.shared.randomer.get_correct_shape(shape)[source]
graforvfl.shared.randomer.get_generator(seed=None)[source]
graforvfl.shared.randomer.glorot_normal_initializer(shape, seed=None)[source]
graforvfl.shared.randomer.glorot_uniform_initializer(shape, seed=None)[source]
graforvfl.shared.randomer.he_normal_initializer(shape, seed=None)[source]
graforvfl.shared.randomer.he_uniform_initializer(shape, seed=None)[source]
graforvfl.shared.randomer.lecun_normal_initializer(shape, seed=None)[source]
graforvfl.shared.randomer.lecun_uniform_initializer(shape, seed=None)[source]
graforvfl.shared.randomer.orthogonal_initializer(shape, gain=1.0, seed=None)[source]
graforvfl.shared.randomer.random_normal_initializer(shape, mean=0.0, stddev=1.0, seed=None)[source]
graforvfl.shared.randomer.random_uniform_initializer(shape, minval=0.0, maxval=1.0, seed=None)[source]

graforvfl.shared.scaler module

class graforvfl.shared.scaler.BoxCoxScaler(lmbda=None)[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class graforvfl.shared.scaler.LabelEncoder[source]

Bases: object

Encode categorical labels as integer indices and decode them back.

This class maps unique categorical labels to integers from 0 to n_classes - 1.

fit(y)[source]

Fit the encoder by finding unique labels in the input data.

Parameters:

y (array-like) – Input labels.

Returns:

self – Fitted LabelEncoder instance.

Return type:

LabelEncoder

fit_transform(y)[source]

Fit the encoder and transform labels in one step.

Parameters:

y (array-like of shape (n_samples,)) – Input labels.

Returns:

Encoded integer labels.

Return type:

np.ndarray

inverse_transform(y)[source]

Transform integer indices back to original labels.

Parameters:

y (array-like of int) – Encoded integer labels.

Returns:

original_labels – Original labels.

Return type:

np.ndarray

Raises:

ValueError – If the encoder has not been fitted or index is out of bounds.

transform(y)[source]

Transform labels to integer indices.

Parameters:

y (array-like) – Labels to encode.

Returns:

encoded_labels – Encoded integer labels.

Return type:

np.ndarray

Raises:

ValueError – If the encoder has not been fitted or unknown labels are found.

class graforvfl.shared.scaler.Log1pScaler[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class graforvfl.shared.scaler.LogeScaler[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class graforvfl.shared.scaler.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 graforvfl.shared.scaler.OneHotEncoder[source]

Bases: object

A simple implementation of one-hot encoding for 1D categorical data.

categories_

Sorted array of unique categories fitted from the input data.

Type:

np.ndarray

fit(X)[source]

Fit the encoder to the unique categories in X.

Parameters:

X (array-like) – 1D array of categorical values.

Returns:

Fitted OneHotEncoder instance.

Return type:

self

fit_transform(X)[source]

Fit the encoder to X and transform X.

Parameters:

X (array-like) – 1D array of categorical values.

Returns:

One-hot encoded array of shape (n_samples, n_categories).

Return type:

np.ndarray

inverse_transform(one_hot)[source]

Convert one-hot encoded data back to original categories.

Parameters:

one_hot (np.ndarray) – 2D array of one-hot encoded data.

Returns:

1D array of original categorical values.

Return type:

np.ndarray

Raises:

ValueError – If the encoder has not been fitted or shape mismatch occurs.

transform(X)[source]

Transform input data into one-hot encoded format.

Parameters:

X (array-like) – 1D array of categorical values.

Returns:

One-hot encoded array of shape (n_samples, n_categories).

Return type:

np.ndarray

Raises:

ValueError – If the encoder has not been fitted or unknown category is found.

class graforvfl.shared.scaler.SinhArcSinhScaler(epsilon=0.1, delta=1.0)[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class graforvfl.shared.scaler.SqrtScaler[source]

Bases: BaseEstimator, TransformerMixin

fit(X, y=None)[source]
inverse_transform(X)[source]
transform(X)[source]
class graforvfl.shared.scaler.YeoJohnsonScaler(lmbda=None)[source]

Bases: BaseEstimator, TransformerMixin

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

graforvfl.shared.scorer module

graforvfl.shared.scorer.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

graforvfl.shared.scorer.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

graforvfl.shared.scorer.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

graforvfl.shared.scorer.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.