Cross Validation Using KFold With Scikit Learn (original) (raw)

Last Updated : 26 Mar, 2026

K‑Fold Cross Validation is a model evaluation technique that divides the dataset into K equal parts (folds) and trains the model multiple times, each time using a different fold as the test set and the remaining folds as training data. This approach provides a more reliable estimate of model performance compared to a single train‑test split.

k-fold

K-fold validation

Step 1: Import Required Libraries

import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import KFold, cross_val_score from sklearn.linear_model import LogisticRegression

`

Step 2: Load the Dataset

We load the Iris dataset and separate features (X) and target labels (y).

data = load_iris() X = data.data y = data.target

`

Step 3: Initialize the Model

Here we will use Logistic Regression as our base model.

Python `

model = LogisticRegression(max_iter=200)

`

Step 4: Configure K Fold Strategy

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

`

Step 5: Perform Cross Validation

Now we execute cross validation and compute accuracy for each fold.

scores = cross_val_score(model, X, y, cv=kfold, scoring='accuracy')

`

Step 6: Print Results

print("Accuracy scores for each fold:", scores) print("Mean Accuracy:", np.mean(scores))

`

**Output:

output

Output

Download full code from here