Part 5 of 12
Your First scikit-learn Model, Built with a Bot
WalkthroughBy Bot Tutorials · Published · Facts last verified Nov 3, 2026
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.
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:
- Write a train-and-test script in a few seconds.
- Explain what each step does and why.
- Suggest a different model to try.
It can't:
- Know whether its code leaks test data into training unless you ask it to check.
- Tell you a score is "good" for your problem. That depends on the problem.
- Replace reading the output yourself.
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
- Test accuracy 0.974 means the model labelled 37 of the 38 held-out flowers correctly. With
test_size=0.25, Iris's 150 rows give 38 test rows. - Cross-validation trains and scores five times on different slices of the training set. The scores sit close together, around 0.955, which suggests the result isn't a fluke of one split.
- The two numbers differ slightly. That's normal. If they were far apart, you'd want to know why.
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
load_iris(return_X_y=True)gives the featuresX(150 rows, 4 measurements) and labelsy(3 species).train_test_split(..., stratify=y)keeps the same mix of species in train and test.make_pipeline(StandardScaler(), LogisticRegression(...))scales each feature, then fits a linear classifier.max_iter=1000gives the solver enough steps to converge on this data.cross_val_score(model, X_tr, y_tr, cv=5)runs five-fold cross-validation on the training rows only.
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
- Swap
LogisticRegressionforRandomForestClassifierand compare cross-validation scores, not just one test score. - Ask the bot to explain the confusion matrix for your model, after you've printed it.
- Try a dataset of your own, with only data you're allowed to use.
Words you'll see
- Features and labels: the inputs (
X) and the answers you want to predict (y). - Estimator: any scikit-learn object with
fit. Models and scalers are both estimators. - Pipeline: a chain of steps that is fitted and used as one estimator.
- Stratify: keep the same mix of classes in each split.
- Cross-validation: repeated train-and-score on different slices of the data, to see how stable a score is.
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
- Tested by us: the script and its output, on scikit-learn 1.9.1 with Python 3.13.5.
- Documented, not tested by us: the leakage and model persistence advice, from scikit-learn's docs.
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.
- Check bot advice against official docs. Version numbers, install commands and compatibility tables change often. Compare every answer with the PyTorch, TensorFlow, JAX, scikit-learn, NVIDIA, Hugging Face or ONNX Runtime pages we link. If the bot and the docs disagree, the docs win.
- Work in a virtual environment. Don't install or remove packages in your system Python, and don't run
sudo pip. - Run untrusted files carefully. Pickle-based model files can run code when loaded. Only load models from sources you trust, and prefer safer formats like safetensors where the library supports them.
- Respect licenses. Datasets and model weights carry their own licenses and terms. Read the model card and license before you use, share or sell anything built on them. We don't give legal advice; ask a qualified person if it matters.
- Protect personal data. Don't paste private, customer or medical data into a bot or a training set unless you have the right to, and check the bot's own privacy settings first.
- No hardware claims you didn't measure. Treat any speed figure from a bot as a guess until you've timed it on your own machine.
- No medical, legal or financial decisions from a toy model. These guides are for learning only.
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
- scikit-learn: Installing scikit-learn
- scikit-learn: Getting Started
- scikit-learn: Common pitfalls and recommended practices
- scikit-learn: Model persistence
- NumPy: the absolute basics for beginners
More in this series
- Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
- Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
- Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
- Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
- Part 5: Your First scikit-learn Model, Built with a Bot (you are here) · Walkthrough
- Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
- Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
- Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
- Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
- Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
- Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
- 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.