tabular_ml_toolkit/tmlt.py
def do_kfold_train_preds(self, X:object, y:object, n_splits:int, model:object, val_metric:object, eval_metric:str, oof:bool=False, X_test:object=None,tabnet_fit_params:dict=None, random_state=42):
        """
            This methods returns oof_preds and test_preds by doing kfold training on sklearn pipeline
            n_splits=5 by default, takes only int value
            random_sate=42, takes only int value
        """
        #create stratified K Folds instance
        kfold = StratifiedKFold(n_splits=n_splits, random_state=random_state, shuffle=True)

        # for OOF Predictions
        oof_preds = np.zeros(X.shape[0]) if oof else None

        # check whether test dataset exist before test preds
        test_preds = np.zeros(X_test.shape[0]) if X_test is not None else None

        # list contains metrics results for each fold
        kfold_metrics_results = []
        n=0
        for train_idx, valid_idx in kfold.split(X, y):
            if isinstance(X, np.ndarray):
                # create NUMPY based X_train, X_valid, y_train, y_valid
                # fix the stratification problem
                #train_idx,valid_idx = clip_splits(train_idx,valid_idx,self.dfl.X_full)
                X_train , X_valid, y_train, y_valid = X[train_idx], X[valid_idx], y[train_idx], y[valid_idx]
            else:
                # create PANDAS based X_train, X_valid, y_train, y_valid
                # fix the stratification problem
                #train_idx,valid_idx = clip_splits(train_idx,valid_idx,self.dfl.X_full)
                X_train , X_valid, y_train, y_valid = X.iloc[train_idx], X.iloc[valid_idx], y[train_idx], y[valid_idx]

            #TRAINING
            logger.info(f"Training Started!")
            #should be better way without string matching
            #change fit for tabnet
            if "tabnet" in str(model.__class__):
                if  tabnet_fit_params:
                    model.fit(X_train, y_train,
                                eval_set=[(X_train, y_train), (X_valid, y_valid)],
                                eval_metric=[eval_metric],
                                **tabnet_fit_params)
                else:
                    raise ValueError("tabnet model requires tabnet_fit_params!")

            #change fit for xgb
            elif "xgb" in str(model.__class__):
                model.fit(X_train, y_train,
                            eval_set=[(X_train, y_train), (X_valid, y_valid)],
                            eval_metric=eval_metric,
                            verbose=False)

            # assumes model will follow standard sklearn fit API
            else:
                model.fit(X_train, y_train)

            logger.info(f"Training Finished!")

            #VAL PREDICTIONS
            metric = val_metric
            metric_result = {}
            preds = None
            # getting either hyperplane distance or probablities from predictions
            if "svm" in str(model.__class__):
                logger.info(f"Predicting Val Decision!")
                preds = model.decision_function(X_valid)
            elif needs_predict_proba(metric):
                logger.info("Predicting Val Probablities!")
                preds = model.predict_proba(X_valid)[:, 1]
            else:
                logger.info("Predicting Val Score!")
                preds = model.predict(X_valid)

            if oof:
                #now add back out of fold predictions to that val set index
                oof_preds[valid_idx] = preds

            #METRICS
            metric_result[str(metric.__name__)] = metric(y_valid, preds)

            #now display value of all the given metrics
            for metric_name, metric_value in metric_result.items():
                logger.info(f"fold: {n+1} {metric_name} : {metric_value}")

            #now append each kfold metric_result dict to list
            kfold_metrics_results.append(metric_result)

            #TEST PREDICTIONS
            if X_test is not None:
                if "svm" in str(model.__class__):
                    test_preds += model.decision_function(X_test) / kfold.n_splits
                else:
                    if "classification" in self.problem_type:
                        logger.info(f"Predicting Test Set Probabilities!")
                        temp_preds_prob = model.predict_proba(X_test).argmax(axis=1)
                        logger.info(f"Fold: {n+1} Test Predictions Class Count is: {dict(pd.Series(temp_preds_prob).value_counts())}")
                        test_preds += temp_preds_prob / kfold.n_splits
                    else:
                        logger.info("Predicting Test Set Scores!")
                        test_preds += model.predict(X_test) / kfold.n_splits
            else:
                logger.warn(f"Trying to do OOF Test Predictions but No Test Dataset Provided!")

            # In order to better GC, del X_train, X_valid, y_train, y_valid df after each fold is done,
            # they will recreate again next time k-fold is called
            del [X_train, X_valid, y_train, y_valid]
            gc.collect()
            #trigger cuda cache clean
            torch.cuda.empty_cache()

            # increment fold counter label
            n += 1

        # inverse tranform labels predicted in test preds
        if "classification" in self.problem_type:
            test_preds = np.around(test_preds).astype(int) #because test_preds are divided by k-fold
            test_preds = self.pp.target_cols__encoder.inverse_transform(test_preds)

        # For mean k-fold score
        #logger.info(f"kfold_metrics_results: {kfold_metrics_results} ")
        mean_metrics_results = kfold_dict_mean(kfold_metrics_results)
        logger.info(f" Mean {str(metric.__name__)} from all Folds are: {mean_metrics_results}")

        # for explicit memory clean
        del [kfold, model]
        gc.collect()
        torch.cuda.empty_cache()

        return mean_metrics_results, test_preds, oof_preds

            This methods returns oof_preds and test_preds by doing kfold training on sklearn pipeline
            n_splits=5 by default, takes only int value
            random_sate=42, takes only int value
        
This methods returns oof_preds and test_preds by doing kfold training on sklearn pipeline
n_splits=5 by default, takes only int value
random_sate=42, takes only int value
This methods returns oof_preds and test_preds by doing kfold training on sklearn pipeline n_splits = 5 by default takes only int value random_sate = 42 takes only int value
def do_kfold_train_preds(self, X:object, y:object, n_splits:int, model:object, val_metric:object, eval_metric:str, oof:bool=False, X_test:object=None,tabnet_fit_params:dict=None, random_state=42):
        kfold = StratifiedKFold(n_splits=n_splits, random_state=random_state, shuffle=True)
        oof_preds = np.zeros(X.shape[0]) if oof else None
        test_preds = np.zeros(X_test.shape[0]) if X_test is not None else None
        kfold_metrics_results = []
        n=0
        for train_idx, valid_idx in kfold.split(X, y):
            if isinstance(X, np.ndarray):
                X_train , X_valid, y_train, y_valid = X[train_idx], X[valid_idx], y[train_idx], y[valid_idx]
            else:
                X_train , X_valid, y_train, y_valid = X.iloc[train_idx], X.iloc[valid_idx], y[train_idx], y[valid_idx]
            logger.info(f"Training Started!")
            if "tabnet" in str(model.__class__):
                if  tabnet_fit_params:
                    model.fit(X_train, y_train,
                                eval_set=[(X_train, y_train), (X_valid, y_valid)],
                                eval_metric=[eval_metric],
                                **tabnet_fit_params)
                else:
                    raise ValueError("tabnet model requires tabnet_fit_params!")
            elif "xgb" in str(model.__class__):
                model.fit(X_train, y_train,
                            eval_set=[(X_train, y_train), (X_valid, y_valid)],
                            eval_metric=eval_metric,
                            verbose=False)
            else:
                model.fit(X_train, y_train)
            logger.info(f"Training Finished!")
            metric = val_metric
            metric_result = {}
            preds = None
            if "svm" in str(model.__class__):
                logger.info(f"Predicting Val Decision!")
                preds = model.decision_function(X_valid)
            elif needs_predict_proba(metric):
                logger.info("Predicting Val Probablities!")
                preds = model.predict_proba(X_valid)[:, 1]
            else:
                logger.info("Predicting Val Score!")
                preds = model.predict(X_valid)
            if oof:
                oof_preds[valid_idx] = preds
            metric_result[str(metric.__name__)] = metric(y_valid, preds)
            for metric_name, metric_value in metric_result.items():
                logger.info(f"fold: {n+1} {metric_name} : {metric_value}")
            kfold_metrics_results.append(metric_result)
            if X_test is not None:
                if "svm" in str(model.__class__):
                    test_preds += model.decision_function(X_test) / kfold.n_splits
                else:
                    if "classification" in self.problem_type:
                        logger.info(f"Predicting Test Set Probabilities!")
                        temp_preds_prob = model.predict_proba(X_test).argmax(axis=1)
                        logger.info(f"Fold: {n+1} Test Predictions Class Count is: {dict(pd.Series(temp_preds_prob).value_counts())}")
                        test_preds += temp_preds_prob / kfold.n_splits
                    else:
                        logger.info("Predicting Test Set Scores!")
                        test_preds += model.predict(X_test) / kfold.n_splits
            else:
                logger.warn(f"Trying to do OOF Test Predictions but No Test Dataset Provided!")
            del [X_train, X_valid, y_train, y_valid]
            gc.collect()
            torch.cuda.empty_cache()
            n += 1
        if "classification" in self.problem_type:
            test_preds = np.around(test_preds).astype(int) 
            test_preds = self.pp.target_cols__encoder.inverse_transform(test_preds)
        mean_metrics_results = kfold_dict_mean(kfold_metrics_results)
        logger.info(f" Mean {str(metric.__name__)} from all Folds are: {mean_metrics_results}")
        del [kfold, model]
        gc.collect()
        torch.cuda.empty_cache()
        return mean_metrics_results, test_preds, oof_preds
def do_kfold_train_preds ( self , X : object , y : object , n_splits : int , model : object , val_metric : object , eval_metric : str , oof : bool = False , X_test : object = None , tabnet_fit_params : dict = None , random_state = 42 ) : kfold = StratifiedKFold ( n_splits = n_splits , random_state = random_state , shuffle = True ) oof_preds = np . zeros ( X . shape [ 0 ] ) if oof else None test_preds = np . zeros ( X_test . shape [ 0 ] ) if X_test is not None else None kfold_metrics_results = [ ] n = 0 for train_idx , valid_idx in kfold . split ( X , y ) : if isinstance ( X , np . ndarray ) : X_train , X_valid , y_train , y_valid = X [ train_idx ] , X [ valid_idx ] , y [ train_idx ] , y [ valid_idx ] else : X_train , X_valid , y_train , y_valid = X . iloc [ train_idx ] , X . iloc [ valid_idx ] , y [ train_idx ] , y [ valid_idx ] logger . info ( f"Training Started!" ) if "tabnet" in str ( model . __class__ ) : if tabnet_fit_params : model . fit ( X_train , y_train , eval_set = [ ( X_train , y_train ) , ( X_valid , y_valid ) ] , eval_metric = [ eval_metric ] , ** tabnet_fit_params ) else : raise ValueError ( "tabnet model requires tabnet_fit_params!" ) elif "xgb" in str ( model . __class__ ) : model . fit ( X_train , y_train , eval_set = [ ( X_train , y_train ) , ( X_valid , y_valid ) ] , eval_metric = eval_metric , verbose = False ) else : model . fit ( X_train , y_train ) logger . info ( f"Training Finished!" ) metric = val_metric metric_result = { } preds = None if "svm" in str ( model . __class__ ) : logger . info ( f"Predicting Val Decision!" ) preds = model . decision_function ( X_valid ) elif needs_predict_proba ( metric ) : logger . info ( "Predicting Val Probablities!" ) preds = model . predict_proba ( X_valid ) [ : , 1 ] else : logger . info ( "Predicting Val Score!" ) preds = model . predict ( X_valid ) if oof : oof_preds [ valid_idx ] = preds metric_result [ str ( metric . __name__ ) ] = metric ( y_valid , preds ) for metric_name , metric_value in metric_result . items ( ) : logger . info ( f"fold: {n+1} {metric_name} : {metric_value}" ) kfold_metrics_results . append ( metric_result ) if X_test is not None : if "svm" in str ( model . __class__ ) : test_preds += model . decision_function ( X_test ) / kfold . n_splits else : if "classification" in self . problem_type : logger . info ( f"Predicting Test Set Probabilities!" ) temp_preds_prob = model . predict_proba ( X_test ) . argmax ( axis = 1 ) logger . info ( f"Fold: {n+1} Test Predictions Class Count is: {dict(pd.Series(temp_preds_prob).value_counts())}" ) test_preds += temp_preds_prob / kfold . n_splits else : logger . info ( "Predicting Test Set Scores!" ) test_preds += model . predict ( X_test ) / kfold . n_splits else : logger . warn ( f"Trying to do OOF Test Predictions but No Test Dataset Provided!" ) del [ X_train , X_valid , y_train , y_valid ] gc . collect ( ) torch . cuda . empty_cache ( ) n += 1 if "classification" in self . problem_type : test_preds = np . around ( test_preds ) . astype ( int ) test_preds = self . pp . target_cols__encoder . inverse_transform ( test_preds ) mean_metrics_results = kfold_dict_mean ( kfold_metrics_results ) logger . info ( f" Mean {str(metric.__name__)} from all Folds are: {mean_metrics_results}" ) del [ kfold , model ] gc . collect ( ) torch . cuda . empty_cache ( ) return mean_metrics_results , test_preds , oof_preds
This methods returns oof_preds and test_preds by doing kfold training on sklearn pipeline
n_splits=5 by default, takes only int value
random_sate=42, takes only int value
This methods returns oof_preds and test_preds by doing kfold training on sklearn pipeline n_splits = 5 by default takes only int value random_sate = 42 takes only int value
"""
            This methods returns oof_preds and test_preds by doing kfold training on sklearn pipeline
            n_splits=5 by default, takes only int value
            random_sate=42, takes only int value
        """ #create stratified K Folds instance # for OOF Predictions # check whether test dataset exist before test preds # list contains metrics results for each fold # create NUMPY based X_train, X_valid, y_train, y_valid # fix the stratification problem #train_idx,valid_idx = clip_splits(train_idx,valid_idx,self.dfl.X_full) # create PANDAS based X_train, X_valid, y_train, y_valid # fix the stratification problem #train_idx,valid_idx = clip_splits(train_idx,valid_idx,self.dfl.X_full) #TRAINING #should be better way without string matching #change fit for tabnet #change fit for xgb # assumes model will follow standard sklearn fit API #VAL PREDICTIONS # getting either hyperplane distance or probablities from predictions #now add back out of fold predictions to that val set index #METRICS #now display value of all the given metrics #now append each kfold metric_result dict to list #TEST PREDICTIONS # In order to better GC, del X_train, X_valid, y_train, y_valid df after each fold is done, # they will recreate again next time k-fold is called #trigger cuda cache clean # increment fold counter label # inverse tranform labels predicted in test preds #because test_preds are divided by k-fold # For mean k-fold score #logger.info(f"kfold_metrics_results: {kfold_metrics_results} ") # for explicit memory clean
self
X
object
y
object
n_splits
int
model
object
val_metric
object
eval_metric
str
oof
bool
X_test
object
tabnet_fit_params
dict
random_state