iclltuning Class

class iclltuning.iclltuning(model_l1=None, model_l2=None, cv_obj_l1=None, cv_obj_l2=None, smote_obj_l1=None, smote_obj_l2=None, sample_wt_l1=None, sample_wt_l2=None, custom_distance_threshold=None)[source]

Bases: object

This is the class representation of this library. Invoke the methods of this class to perform hyperparameter tuning on the models fed to the Imbalanced Classification via Layered Learning (ICLL) model.

Layer 1 follows the same definition as mentioned within the original paper: “Layer 1 (L1) denotes the event that the observation belongs to the Mixed or Pure Minority Group.”

Layer 2 follows the same definition as mentioned within the original paper: “Given that L1 occurs (i.e. observation belongs to the mixed or pure minority group), Layer 2 (L2) denotes the event that the observation belongs to the pure minority class.”

Parameters:
  • model_l1 (object, optional) –

    ML model to be fit on Layer 1. A model must be passed if you don’t want hyperparameter tuning to be performed on Layer 1 task.


    Requirements:
    1. This model must have a callable fit() method.

    2. This model must have a callable predict() in the case of hard classification or predict_proba() in the case of soft classification. Either one provided is fine.


    In the situation where both the model and cross-validation object are both provided, the model assigned to the cross-validation object will take precedence in the ICLL algorithm.

  • model_l2 (object, optional) –

    ML model to be fit on Layer 2. A model must be passed if you don’t want hyperparameter tuning to be performed on Layer 2 task.


    Requirements:
    1. This model must have a callable fit() method.

    2. This model must have a callable predict() in the case of hard classification or predict_proba() in the case of soft classification. Either one provided is fine.


    In the situation where both the model and cross-validation object are both provided, the model assigned to the cross-validation object will take precedence in the ICLL algorithm.

  • cv_obj_l1 (GridSearchCV or RandomizedSearchCV object, optional) –

    GridSearchCV or RandomizedSearchCV object to be fit on Layer 1.

    Note

    refit parameter of the GridSearchCV or RandomizedSearchCV object must NOT be initialized with a value of False. If it is instantiated a value of False, then the ICLL model will NOT be trained, and a ValueError will be raised instead.

  • cv_obj_l2 (GridSearchCV or RandomizedSearchCV object, optional) –

    GridSearchCV or RandomizedSearchCV object to be fit on Layer 2.

    Note

    refit parameter of the GridSearchCV or RandomizedSearchCV object must be NOT be initialized with a value of False. If it is instantiated a value of False, then the ICLL model will NOT be trained, and a ValueError will be raised instead.

  • smote_obj_l1 (imblearn.over_sampling._smote.base.SMOTE, optional) – SMOTE object to be passed for resampling on Layer 1.

  • smote_obj_l2 (imblearn.over_sampling._smote.base.SMOTE, optional) – SMOTE object to be passed for resampling on Layer 2.

  • sample_wt_l1 (numpy.ndarray/ List / Pandas Series, optional) –

    Array of sample weights to be assigned for Layer 1 training task.

    Note

    1. If cv_obj_l1 parameter is passed, then hyperparameter tuning will be carried out with sample weights passed, and as a result, the best estimator as found out by cv_obj_l1 will be trained with the provided sample weights. Note that both the cv_obj_l1 object and its attached estimator must both have the sample_weight attribute for the fit() method. Otherwise, hyperparameter tuning will be carried out without supplying the sample weights.

    2. If cv_obj_l1 parameter is not passed, then model_l1 parameter will be trained with sample weights of sample_wt_l1. Note that the estimator must have the sample_weight attribute for the fit() method. Otherwise, estimator training will be carried out wihtout supplying the sample weights.

    3. If both parameters smote_obj_l1 and sample_wt_l1 are passed, then resampling will NOT be performed on Layer 1, and sample weights will be passed during model training/ hyperparameter tuning.

  • sample_wt_l2 (numpy.ndarray/ List / Pandas Series, optional) –

    Array of sample weights to be assigned for Layer 2 training task.

    Note

    1. If cv_obj_l2 parameter is passed, then hyperparameter tuning will be carried out with sample weights passed, and as a result, the best estimator as found out by cv_obj_l2 will be trained with the provided sample weights. Note that both the cv_obj_l2 object and its attached estimator must both have the sample_weight attribute for the fit() method. Otherwise, hyperparameter tuning will be carried out wihtout supplying the sample weights.

    2. If cv_obj_l2 parameter is not passed, then model_l2 parameter will be trained with sample weights of sample_wt_l2. Note that the estimator must have the sample_weight attribute for the fit() method. Otherwise, estimator training will be carried out wihtout supplying the sample weights.

    3. If both parameters smote_obj_l2 and sample_wt_l2 are passed, then resampling will NOT be performed on Layer 2, and sample weights will be passed during model training/ hyperparameter tuning.


    IMPORTANT

    In ICLL algorithm, Layer 2 is trained to distinguish instances belonging to Mixed clusters from those belonging to Pure Minority clusters. Instances assigned to Pure Majority clusters are excluded from the training process in this layer. Consequently, only the sample weights corresponding to instances in Mixed or Pure Minority clusters are passed to the estimator or the GridSearchCV or RandomizedSearchCV object in Layer 2.

    WARNING: Only use this parameter if you fully understand its effect on the training process.

  • custom_distance_threshold (float, optional) – Provide a value for this parameter if you want to override the original implementation of the computation of distance threshold (flat clusters are obtained at a threshold of the log-transformed intra-cluster distance of µ + σ)

fit(X, y)[source]

Fit the ICLL model on your dataset.

Parameters:
  • X (Pandas DataFrame or numpy.ndarray) –

    Independent variables data for model training

    Note

    Since the first step of ICLL model is to perform the agglomerative hierarchical clustering, it is expected that all elements of X will only contain non-null integer or floating data types. Failure to adhere to this will result in a ValueError being raised instead.

  • y (Pandas Series or numpy.ndarray) – Target variable. Target variable values must be in 1/0 format

Returns:

Fitted estimator.

Return type:

Object

predict(X)[source]

Predict class labels for samples in X.

This class method is only valid when both the models for Layer 1 and Layer 2 training have a valid predict() method.

Let \(f_{L1}(x_i)\) and \(f_{L2}(x_i)\) denote the models trained in the layers L1 and L2, respectively. The the prediction by ICLL model is given by: \(f_{L1}(x_i) \cdot f_{L2}(x_i)\)

Thus, a sample instance would only receive a predicted class label of 1 only when the predicted class label of that instance for both the models \(f_{L1}(x_i)\) and \(f_{L2}(x_i)\) is 1.

Note

The ICLL method predict() will not work if any of the following scenarios arise by clustering:

Scenario 1: No pure majority clusters are formed.
Scenario 2: No mixed clusters are formed.
Scenario 3: Only mixed clusters are formed.

A ValueError will be raised instead if any of the above three scenarios is encountered.

Parameters:

X (Pandas DataFrame or numpy.ndarray) – The data matrix for which we want to get the predictions.

Returns:

Vector containing the class labels for each sample. This would be an ndarray of shape (n_samples,)

Return type:

numpy.ndarray

predict_proba(X)[source]

Predict probability estimates.

This class method is only valid when both the models for Layer 1 and Layer 2 training have a valid predict_proba() method.

Let \(f_{L1}(x_i)\) and \(f_{L2}(x_i)\) denote the models trained in the layers L1 and L2, respectively. The the prediction by ICLL model is given by: \(f_{L1}(x_i) \cdot f_{L2}(x_i)\)

Note

The ICLL method predict_proba() will not work if any of the following scenarios arise by clustering:

Scenario 1: No pure majority clusters are formed.
Scenario 2: No mixed clusters are formed.
Scenario 3: Only mixed clusters are formed.

A ValueError will be raised instead if any of the above three scenarios is encountered.


IMPORTANT

The ICLL model will always return the final probabilities based upon the probabilites of the positive class retured by each layer’s estimator. So, to identify the probabilities returned by the positive class, the estimator corresponding to both the layers must necessarily have a classes_ attribute.

If the method does not have a classes_ attribute, a ValueError will be raised instead.

Parameters:

X (Pandas DataFrame or numpy.ndarray) – The data matrix for which we want to get the predictions.

Returns:

Probability of the sample for belonging to the positive class

Return type:

numpy.ndarray

get_computed_dist_threshold()[source]

Returns the log-transformed intra-cluster distance of µ + σ

Returns:

log-transformed intra-cluster distance of µ + σ

Return type:

float

get_cluster_type_info()[source]

Get the count of each type of cluster formed (pure majority, mixed and pure minority).

Returns:

A dictionary where the key is the name of the type of cluster, and its corresponding value is the count of such clusters.

Return type:

dict

get_layer_search_time(layer_name)[source]

Pass ‘L1’ to parameter layer_name to return the time taken for hyperparameter tuning on Layer 1 when a GridSearchCV or RandomizedSearchCV object is provided. Otherwise, it returns the time taken to train the Layer 1 model.

Pass ‘L2’ to parameter layer_name to return the time taken for hyperparameter tuning on Layer 2 when a GridSearchCV or RandomizedSearchCV object is provided. Otherwise, it returns the time taken to train the Layer 2 model.

Note

The ICLL method get_layer_search_time() will not work if any of the following scenarios arise by clustering:

Scenario 1: No pure majority clusters are formed.
Scenario 2: No mixed clusters are formed.
Scenario 3: Only mixed clusters are formed.

A ValueError will be raised instead if any of the above three scenarios is encountered.

Parameters:

layer_name (str) – The name of the layer for which functionalities are to be accessed.

Returns:

Time measured in seconds.

Return type:

float

get_fitted_cv_layer_object(layer_name)[source]

Specify ‘L1’ or ‘L2’ to parameter layer_name to retrieve the fitted object for that layer. If a GridSearchCV/RandomizedSearchCV object was provided, the fitted CV object is returned.

Note

The ICLL method get_fitted_cv_layer_object() will not work if any of the following scenarios arise by clustering:

Scenario 1: No pure majority clusters are formed.
Scenario 2: No mixed clusters are formed.
Scenario 3: Only mixed clusters are formed.

A ValueError will be raised instead if any of the above three scenarios is encountered.

Parameters:

layer_name (str) – The name of the layer for which functionalities are to be accessed.

Returns:

Fitted GridSearchCV or RandomizedSearchCV object if provided at the time of object initialization.

Return type:

object

get_fitted_model_layer_object(layer_name)[source]

Specify ‘L1’ or ‘L2’ for layer_name to retrieve the fitted model from that layer. If the layer uses a GridSearchCV or RandomizedSearchCV object, the model from best_estimator_ is returned. Otherwise, the fitted model stored in model_l1 or model_l2 is returned.

Note

The ICLL method get_fitted_model_layer_object() will not work if any of the following scenarios arise by clustering:

Scenario 1: No pure majority clusters are formed.
Scenario 2: No mixed clusters are formed.
Scenario 3: Only mixed clusters are formed.

A ValueError will be raised instead if any of the above three scenarios is encountered.

Parameters:

layer_name (str) – The name of the layer for which functionalities are to be accessed.

Returns:

Fitted model

Return type:

object