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:
objectThis 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:This model must have a callable
fit()method.This model must have a callable
predict()in the case of hard classification orpredict_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:This model must have a callable
fit()method.This model must have a callable
predict()in the case of hard classification orpredict_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 (
GridSearchCVorRandomizedSearchCVobject, optional) –GridSearchCVorRandomizedSearchCVobject to be fit on Layer 1.Note
refitparameter of theGridSearchCVorRandomizedSearchCVobject 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 aValueErrorwill be raised instead.cv_obj_l2 (
GridSearchCVorRandomizedSearchCVobject, optional) –GridSearchCVorRandomizedSearchCVobject to be fit on Layer 2.Note
refitparameter of theGridSearchCVorRandomizedSearchCVobject 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 aValueErrorwill 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
If
cv_obj_l1parameter is passed, then hyperparameter tuning will be carried out with sample weights passed, and as a result, the best estimator as found out bycv_obj_l1will be trained with the provided sample weights. Note that both thecv_obj_l1object and its attached estimator must both have thesample_weightattribute for thefit()method. Otherwise, hyperparameter tuning will be carried out without supplying the sample weights.If
cv_obj_l1parameter is not passed, thenmodel_l1parameter will be trained with sample weights ofsample_wt_l1. Note that the estimator must have thesample_weightattribute for thefit()method. Otherwise, estimator training will be carried out wihtout supplying the sample weights.If both parameters
smote_obj_l1andsample_wt_l1are 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
If
cv_obj_l2parameter is passed, then hyperparameter tuning will be carried out with sample weights passed, and as a result, the best estimator as found out bycv_obj_l2will be trained with the provided sample weights. Note that both thecv_obj_l2object and its attached estimator must both have thesample_weightattribute for thefit()method. Otherwise, hyperparameter tuning will be carried out wihtout supplying the sample weights.If
cv_obj_l2parameter is not passed, thenmodel_l2parameter will be trained with sample weights ofsample_wt_l2. Note that the estimator must have thesample_weightattribute for thefit()method. Otherwise, estimator training will be carried out wihtout supplying the sample weights.If both parameters
smote_obj_l2andsample_wt_l2are passed, then resampling will NOT be performed on Layer 2, and sample weights will be passed during model training/ hyperparameter tuning.
IMPORTANTIn 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
GridSearchCVorRandomizedSearchCVobject 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
Xwill only contain non-null integer or floating data types. Failure to adhere to this will result in aValueErrorbeing 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
ValueErrorwill 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
ValueErrorwill be raised instead if any of the above three scenarios is encountered.IMPORTANTThe 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, aValueErrorwill 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_nameto return the time taken for hyperparameter tuning on Layer 1 when aGridSearchCVorRandomizedSearchCVobject is provided. Otherwise, it returns the time taken to train the Layer 1 model.Pass ‘L2’ to parameter
layer_nameto return the time taken for hyperparameter tuning on Layer 2 when aGridSearchCVorRandomizedSearchCVobject 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
ValueErrorwill 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_nameto 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
ValueErrorwill 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
GridSearchCVorRandomizedSearchCVobject 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_nameto retrieve the fitted model from that layer. If the layer uses aGridSearchCVorRandomizedSearchCVobject, the model frombest_estimator_is returned. Otherwise, the fitted model stored inmodel_l1ormodel_l2is 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
ValueErrorwill 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