Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tqdm in the cross validtion when using folds.split(train.values, target.values)

I am doing cross-validation with my lightgbm model as follows.
And I want to use tqdm in the for loop so I can check the process.

folds = KFold(n_splits=num_folds, random_state=2319)
oof = np.zeros(len(train))
getVal = np.zeros(len(train))
predictions = np.zeros(len(target))
feature_importance_df = pd.DataFrame()

print('Light GBM Model')
for fold_, (trn_idx, val_idx) in enumerate(folds.split(train.values, target.values)):

    X_train, y_train = train.iloc[trn_idx][features], target.iloc[trn_idx]
    X_valid, y_valid = train.iloc[val_idx][features], target.iloc[val_idx]


    print("Fold idx:{}".format(fold_ + 1))
    trn_data = lgb.Dataset(X_train, label=y_train, categorical_feature=categorical_features)
    val_data = lgb.Dataset(X_valid, label=y_valid, categorical_feature=categorical_features)

    clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
    oof[val_idx] = clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)
    getVal[val_idx]+= clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration) / folds.n_splits

    fold_importance_df = pd.DataFrame()
    fold_importance_df["feature"] = features
    fold_importance_df["importance"] = clf.feature_importance()
    fold_importance_df["fold"] = fold_ + 1
    feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)

    predictions += clf.predict(test[features], num_iteration=clf.best_iteration) / folds.n_splits

print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)))

I have tried to use tqdm(enumerate(folds.split(train.values, target.values)) or enumerate(tqdm(folds.split(train.values, target.values))), but it doesn't work.
I guess the reason why they didn't work because enumerate doesn't have length.
But I am wondering how to use tqdm in this situation.
Could anyone help me?
Thanks in advance.

like image 974
Bowen Peng Avatar asked Dec 13 '22 11:12

Bowen Peng


1 Answers

To do a progress bar over k-fold iterations(desc parameter is optional):

from tqdm import tqdm
for train, test in tqdm(kfold.split(x, y), total=kfold.get_n_splits(), desc="k-fold"):
   # Your code here

The output going to be something like this:

k-fold: 100%|██████████| 10/10 [02:26<00:00, 16.44s/it]
like image 69
lmlima Avatar answered Feb 16 '23 00:02

lmlima