Bot TutorialsGrok BotChatGPTClaudeGeminiCopilotPerplexityMore botsGuidesArticlesPapers
Topics
CookingHome automationFitnessCreative writingMusic productionCodingIndustrialPros (Professional track)
Search

Part 5 of 12

Your First scikit-learn Model, Built with a Bot

WalkthroughBy Bot Tutorials · Published · Facts last verified Nov 3, 2026

ML libraries series complete. "Review Your Training Code with a Bot: Checklist" was the finale. See every part in the series hub.

Part 5: train and test a scikit-learn pipeline on the Iris dataset, with real accuracy and cross-validation output and a bot prompt that avoids leakage.

Video: Your First scikit-learn Model, Built with a Bot (5:50). Narrated from this guide, with the real code and real output we ran. Captions included (caption file). Prefer reading? Everything in the video is in the guide below.

scikit-learn is the friendliest place to start with machine learning in Python. Every model has the same fit and predict methods, the library ships small datasets for practice, and its docs are clear about common mistakes. In this walkthrough a bot helps write a first model, we run it and show the real numbers, and then we check the bot's code against scikit-learn's own advice.

What the bot can and can't do here

It can:

It can't:

Check first, trust second

Run the code, read the numbers, and check them against a second measure such as cross-validation before you believe them.

Install

scikit-learn's install page gives pip install -U scikit-learn inside a virtual environment, and says recent versions need Python 3.10 or newer. Use the venv from Part 3.

The prompt

Write a scikit-learn script that loads the Iris dataset, splits it into train and test with stratification and a fixed random_state, puts StandardScaler and LogisticRegression in a pipeline, prints test accuracy, and prints 5-fold cross-validation scores on the training set only. No plotting.

The script

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

X, y = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
model.fit(X_tr, y_tr)
print("test accuracy:", round(accuracy_score(y_te, model.predict(X_te)), 3))
scores = cross_val_score(model, X_tr, y_tr, cv=5)
print("5-fold CV on train:", [round(float(s), 3) for s in scores], "mean", round(float(scores.mean()), 3))

Real output with scikit-learn 1.9.1:

test accuracy: 0.974
5-fold CV on train: [0.957, 0.957, 0.955, 0.955, 0.955] mean 0.955

Reading the output

Why the pipeline matters

scikit-learn's Common pitfalls page warns about data leakage: if you fit a scaler or other preprocessing on the whole dataset before splitting, information from the test set leaks into training and the score looks better than it should. Putting StandardScaler inside make_pipeline means it's fitted only on the data passed to fit, including inside each cross-validation fold. Part 12 shows a leaky version next to a fixed one, with a test that checks the fix.

A good follow-up prompt:

Review this script for data leakage, using scikit-learn's Common pitfalls page (pasted). Point to any line that fits on data it shouldn't see.

Each step in plain English

Saving the model, carefully

scikit-learn's Model persistence page compares formats. Pickle-based formats are fast but can run code when loaded, so the page asks whether you trust where a model file came from. It also describes skops.io as more secure than pickle-based formats. Only load model files from sources you trust, and save the library versions alongside them.

Try next

Words you'll see

When the bot's code differs

Bots sometimes produce older scikit-learn code, such as importing from modules that have moved. If an import fails, paste the error and your scikit-learn version, and ask the bot to check the current API reference. scikit-learn's Getting Started page is a good place to compare the basics.

Tested vs documented

Where to go next

The last part today does a similar job in PyTorch, on the CPU.

Safety, licensing and privacy: read this before you run anything

This series is about learning and small personal projects with machine learning libraries. A bot is a helper, not an authority.

FAQ

Why put the scaler in a pipeline?

So it's fitted on the training data only. scikit-learn's Common pitfalls page explains that fitting preprocessing on all the data leaks test information into training.

Is 97% accuracy good?

On Iris, a small and well-known teaching dataset, it's normal. It tells you nothing about how a model would do on your data.

Was this tested?

Yes. The output shown is from scikit-learn 1.9.1 on our box.

Sources

  1. scikit-learn: Installing scikit-learn
  2. scikit-learn: Getting Started
  3. scikit-learn: Common pitfalls and recommended practices
  4. scikit-learn: Model persistence
  5. NumPy: the absolute basics for beginners

More in this series

  1. Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
  2. Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
  3. Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
  4. Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
  5. Part 5: Your First scikit-learn Model, Built with a Bot (you are here) · Walkthrough
  6. Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
  7. Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
  8. Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
  9. Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
  10. Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
  11. Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
  12. Part 12: Review Your Training Code with a Bot: Checklist · Checklist

See the ML Libraries with a Bot series hub · Latest on Bot Tutorials

Bot advice can be wrong. We cite official docs; check them yourself before acting, especially for anything involving money, safety or your data.