Skip to content

[FAQ Bot] NEW: Homework 4 Q5: For 5-fold cross-validation, should KFold split `df_tra - #467

Open
github-actions[bot] wants to merge 1 commit into
mainfrom
faq-bot/issue-466
Open

github-actions[bot] wants to merge 1 commit into
mainfrom
faq-bot/issue-466

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

✨ FAQ NEW

Course: machine-learning-zoomcamp
Section: module-4 (This is about how to set up cross-validation folds for classification evaluation, which fits the Module 4 section on cross-validation and ROC AUC.)
Related Issue: #466

Question

Homework 4 Q5: For 5-fold cross-validation, should KFold split df_train or df_full_train?

Decision Rationale

No existing FAQ explicitly answers which dataframe to split for 5-fold cross-validation in Homework 4 Q5 (whether to use df_train or df_full_train).

Placement Details

  • Section ID: module-4
  • Sort Order: End of section
  • Filename Slug: homework-4-q5-kfold-split-df-full-train-vs-df-train

⚠️ Warnings

  • Order set to -1 (append) because no closely matching existing entry provides an exact placement spot.

🤖 Generated by FAQ Bot

Closes #466

@saeidsoheily

Copy link
Copy Markdown

For Homework 4 Q5, call kfold.split(...) on df_full_train, not df_train.

The reason is that cross-validation replaces the usual train/validation split. df_full_train is the full 80% training portion, and 5-fold CV repeatedly splits that same 80% into training and validation folds.

from sklearn.model_selection import KFold

kfold = KFold(n_splits=5, shuffle=True, random_state=1)

for train_idx, val_idx in kfold.split(df_full_train):
    df_train = df_full_train.iloc[train_idx]
    y_train = df_train.converted.values

    df_val = df_full_train.iloc[val_idx]
    y_val = df_val.converted.values

Then separate the target inside each fold, Also fit a new DictVectorizer inside each fold. Note that: Do not fit the vectorizer once outside the CV loop, because that would expose validation-fold categories to training and create leakage.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FAQ] : Homework 4 Q5-Q6 - do I cross-validate on df_train or df_full_train?

1 participant