Topics / 🤖 AI & Machine Learning / 📏 Level 22: Evaluate & Engineer

Python exercise · about 5 minutes · free

Cross-Validation

One lucky test split can flatter a model. Cross-validation tests it on five different splits.

cross_val_score(model, X, y, cv=5) trains and scores 5 times, each time holding out a different fifth of the data. Report the mean and the spread.

fold 1: test, train, train, train, train. fold 2: train, test, train, train, train. … fold 5: train, train, train, train, test. every row is used for testing exactly once; report mean and spreadfold 1testtraintraintraintrainfold 2traintesttraintraintrain… fold 5traintraintraintraintestevery row is used for testing exactly once; report mean and spread
Your quest: Run 5-fold cross-validation of LogisticRegression(max_iter=1000) on the iris data. Set scores and mean_score.
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hintscores = cross_val_score(LogisticRegression(max_iter=1000), X, y, cv=5); mean_score = scores.mean()
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 22: Evaluate & Engineer

Share on LinkedIn