<jupyter_start><jupyter_text>Out of Fold Prediction<jupyter_code>from sklearn.model_selection import KFold

def train_apps_all_with_oof(apps_all_train, apps_all_test, nfolds=5):
    ftr_app = apps_all_train.drop(['SK_ID_CURR', 'TARGET'], axis=1)
    target_app = apps_all_train['TARGET']

    # kFold 생성
    folds = KFold(n_splits=nfolds, shuffle=True, random_state=2021)

    # validation set 예측 결과 저장 array
    oof_preds = np.zeros(ftr_app.shape[0])

    # test dataset 예측 결과 저장 array
    test_preds = np.zeros(apps_all_test.shape[0])

    clf = LGBMClassifier(
                nthread=4,
                n_estimators=4000,
                learning_rate=0.01,
                max_depth = 11,
                num_leaves=58,
                colsample_bytree=0.613,
                subsample=0.708,
                max_bin=407,
                reg_alpha=3.564,
                reg_lambda=4.930,
                min_child_weight= 6,
                min_child_samples=165,
                silent=-1,
                verbose=-1,
                )
    
    # nfold번 cross validation iteration, Out of Fold 방식으로 학습 및 예측
    for fold_idx, (train_idx, valid_idx) in enumerate(folds.split(ftr_app)):
      print('#### iteration ', fold_idx)

      train_x = ftr_app.iloc[train_idx, :]
      train_y = target_app.iloc[train_idx]
      valid_x = ftr_app.iloc[valid_idx, :]
      valid_y = target_app.iloc[valid_idx]

      # 모델 학습
      clf.fit(train_x, train_y, eval_set=[(train_x, train_y), (valid_x, valid_y)], eval_metric='auc', verbose=200,
              early_stopping_rounds=200)
      
      # 검증 데이터 세트로 예측된 확률 저장
      oof_preds[valid_idx] = clf.predict_proba(valid_x, num_iteration=clf.best_iteration_)[:, 1]

      # 테스트 데이터 세트에 예측 확률 저장
      test_preds += clf.predict_proba(apps_all_test.drop('SK_ID_CURR', axis=1), num_iteration=clf.best_iteration_)[:, 1]/folds.n_splits
    
    return clf, test_preds        
# application, previous, bureau, bureau_bal 관련 데이터셋 가공 및 취합
apps_all = get_apps_all_with_all_agg(apps, prev, bureau, bureau_bal, pos_bal, install, card_bal)

# drop 대상 컬럼 삭제 
#apps_all = apps_all.drop(drop_list, axis=1)

# Category 컬럼을 모두 Label 인코딩 수행
apps_all = get_apps_all_encoded(apps_all)

# 학습과 테스트 데이터로 분리
apps_all_train, apps_all_test = get_apps_all_train_test(apps_all)
clf, test_preds = train_apps_all_with_oof(apps_all_train, apps_all_test, nfolds=5)
preds = clf.predict_proba(apps_all_test.drop(['SK_ID_CURR'], axis=1))[:, 1]
apps_all_test['TARGET'] = preds

apps_all_test[['SK_ID_CURR', 'TARGET']].to_csv(os.path.join(path, 'oof_all_02.csv'), index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10753922694221202, 'fraction_numerical': 0.022962112514351322, 'mean_word_length': 3.0339506172839505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '117308', 'license': 'MIT', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1155, 'n_words': 174, 'path': 'Home Credit Default Risk/6_Out of Fold Prediction.ipynb', 'repo_name': 'kimbyeolhee/Kaggle', 'size': '2613'}