Python + AI/ML · Tutorial 5 of 5
Train Your First Machine Learning Model with scikit-learn
By the FUTEAC Team · 6 November 2026 · 15 min read · Beginner
In short
Training a model means showing a program labelled examples and letting it learn the pattern. You train a scikit-learn decision tree on iris flowers: load the dataset (150 flowers), split it into 120 training and 30 test rows, fit the model, and check it on unseen flowers. Test accuracy is 1.0, perfect on this small, clean teaching dataset.
- A model learns from examples, not from rules you write. You never tell the model "if the petal is short, it is a setosa". It works that out from 120 labelled flowers.
- The five-step workflow is the whole job: load the data, split it into training and test sets, train, evaluate, predict. Every machine learning project you ever do follows this shape.
- The test set is what makes the accuracy honest. The model never sees those 30 flowers during training, so a good score means it learned a real pattern, not memorised answers.
- A perfect score on a teaching dataset is normal, not a triumph. Iris is small and clean; on messy real data the number will be lower, and that is expected.
- This tutorial feeds the AI & Machine Learning: First Steps course (/course/ai-ml-first-steps/) lesson "Training a Classifier with scikit-learn" (module "Training Your First Models"), and the Machine Learning Basics (/skill/machine-learning-basics/), Classification Models (/skill/classification/) and Model Evaluation (/skill/model-evaluation/) skills.
What does "training a model" actually mean?
Think of teaching a new colleague to sort post: no rulebook, just a pile of envelopes, "this one goes to Lahore, this one to Karachi", until they spot the pattern themselves. Then you test them on envelopes they have never seen and count how many they get right.
A machine learning model is the same arrangement, in code. The training data is the pile of labelled examples, training is finding the pattern, the test data is the fresh pile, and accuracy is how many it gets right.
Three words you will meet constantly: features. The measurements the model may look at (here, four numbers per flower); labels, the correct answers attached to the training examples (here, the species); prediction. The model's answer for a flower it has never seen.
Because the answer is one of three categories, this is a classification problem. The model assigns each record to a category, rather than predicting a number (that would be regression). Classification is the gentlest first model to build, because the result is easy to check: right or wrong, flower by flower.
What is the iris dataset: and why start with flowers?
The iris dataset is the oldest teaching dataset in machine learning: 150 iris flowers, 50 of each of three species, each described by four measurements in centimetres. It ships inside scikit-learn itself, so there is nothing to download and no file to find. Load_iris() just works, offline.
That is exactly why it is the right first dataset. Your first model should teach you the workflow, not fight you over data. Real datasets come with missing values, wrong types and duplicates. You already learned to handle those in Cleaning Data with pandas. Here the data is clean on purpose, so every surprise in the output is about the model, not the mess. One honest warning: iris is almost too easy. The species separate cleanly on petal measurements, so a simple model scores perfectly. Learn the moves here, load, split, train, evaluate, predict, and expect real data to be harder. The moves are the same; only the score changes.
What are the five steps of training a model?
Every classification project, from this flower example to a spam filter, follows the same five steps: load the features and labels; split off a test set the model never trains on (usually 80% train, 20% test); train by calling fit() on the training data; evaluate by predicting the test labels and counting how many are right; predict on genuinely new examples.
Step 2 is the one beginners most often skip, and it matters most. A model tested on the data it trained on will look brilliant and tell you nothing. It may simply have memorised the answers. The test set is the only honest judge.
A decision tree is a good first algorithm because you can picture it: a sequence of yes/no questions about the measurements ("is the petal length less than 2.5 cm?") leading down to a species. You do not need its internals yet. DecisionTreeClassifier does the work; your job is the workflow around it.
Worked example: train and test a flower classifier
Install scikit-learn first, if you have not already:
pip install scikit-learnThen save this as first_model.py and run it with python first_model.py:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# 1. Load the data
iris = load_iris()
print("Flowers in dataset:", iris.data.shape[0])
print("Measurements per flower:", iris.data.shape[1])
print("Classes:", ", ".join(iris.target_names))
# 2. Split into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42
)
print("Train on", X_train.shape[0], "| test on", X_test.shape[0])
# 3. Train the model
model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)
# 4. Evaluate on data the model has never seen
accuracy = accuracy_score(y_test, model.predict(X_test))
print("Test accuracy:", accuracy)
# 5. Predict one new flower
new_flower = [[5.1, 3.5, 1.4, 0.2]]
predicted = model.predict(new_flower)[0]
print("Prediction for", new_flower[0], "->", iris.target_names[predicted])Exact output, as run for this tutorial:
Flowers in dataset: 150
Measurements per flower: 4
Classes: setosa, versicolor, virginica
Train on 120 | test on 30
Test accuracy: 1.0
Prediction for [5.1, 3.5, 1.4, 0.2] -> setosaRead the code line by line. load_iris() returns the dataset; .data is the 150-by-4 table of measurements and .target is the list of 150 labels. The ", ".join(iris.target_names) is deliberate. Printing the raw array shows NumPy string wrappers in current versions, which is confusing in a beginner tutorial.
train_test_split with test_size=0.2 holds back exactly 30 flowers for testing, and random_state=42 fixes the shuffle so the split is identical on every run. The naming is a convention worth learning: X for features, y for labels, _train and _test for the two halves.
DecisionTreeClassifier(random_state=42) creates the model; model.fit(X_train, y_train) trains it. model.predict(X_test) produces 30 guesses, and accuracy_score compares them with the truth: 30 out of 30 correct is 1.0. Finally, one new flower, [5.1, 3.5, 1.4, 0.2]. Goes in as a list inside a list, because predict always expects a table of rows, even for one row. The model answers setosa: short petals, wide sepals, a classic setosa.
How do I read the accuracy score: and why is 1.0 not the whole story?
Accuracy is the share of test examples the model labelled correctly: 1.0 means all 30, 0.9 would mean 27 of 30. It is the simplest honest number in machine learning, and the right first metric to learn.
But a perfect score deserves suspicion, not celebration. Ask three questions whenever accuracy looks too good:
- Did the test data leak into training? Here it did not, the split happened before training, and
fitonly ever sawX_train. Fit on the full dataset and "test" on the same rows, and the score is meaningless. - Is the dataset too easy? Iris is. The species separate almost perfectly on petal length alone, so a 1.0 here says more about the dataset than about your skill.
- Is the test set big enough? Thirty flowers is small: one wrong answer drops the score to 0.97. Real projects use larger test sets and more metrics. Precision, recall and the confusion matrix, covered in the course lesson "Is My Model Any Good? Accuracy and Confusion Matrix".
There is also overfitting: a model that memorises the training data so closely that it performs worse on new data. The defence is the test set you already have. If training accuracy is perfect but test accuracy drops, the model memorised instead of learning. The course lesson "Overfitting: Why Perfect Scores Can Lie" takes this further.
What should I try next?
Change one thing at a time and re-run:
- Change
random_stateto 7 and watch the accuracy. The split changes; the score usually stays 1.0 on iris, which tells you the result is stable, not a lucky split. - Change
test_sizeto 0.3 and see the train/test counts move to 105 and 45. - Try a different new flower,
[6.0, 2.2, 4.0, 1.0]should come back versicolor. - Print the training accuracy too (
accuracy_score(y_train, model.predict(X_train))). If it ever disagrees sharply with the test accuracy, you have found overfitting in the wild.
When you are ready for a real dataset, go back to Cleaning Data with pandas and clean something messy first, then train on it. Clean data first, model second: that order is the whole discipline of applied machine learning.
Try it yourself
Take the script above and extend it: after training, print the model's accuracy on the training set as well as the test set, using accuracy_score(y_train, model.predict(X_train)). Then change random_state in both train_test_split and DecisionTreeClassifier to 7, re-run, and write down the two accuracy numbers. If both stay at 1.0, explain in one sentence why that is expected for iris, and in one more sentence why you would not expect the same on a real customer dataset.
Continue in the free course: AI & Machine Learning: First Steps →
Common mistakes
- Testing on the training data. Calling
model.predict(X_train)and celebrating a perfect score proves nothing. The model has already seen those rows. Always evaluate onX_test. - Forgetting the double brackets on a single prediction.
model.predict([5.1, 3.5, 1.4, 0.2])raises an error becausepredictexpects a table of rows. One flower is a table with one row:[[5.1, 3.5, 1.4, 0.2]]. - Skipping
random_stateand wondering why results change. Without it, every run shuffles differently. Set it while you are learning so your results are repeatable. - Reading 1.0 as "my model is perfect". On iris, 1.0 is the dataset being easy. The honest reading is "the workflow works and the model learned the pattern", nothing more.
- Jumping to a model before cleaning the data. Missing values will stop most scikit-learn models with an error, and duplicates will quietly bias the result. Clean first, train second.
Frequently asked questions
Do I need to know the maths behind machine learning to train my first model?
No. You need the workflow, load, split, train, evaluate, predict, and the discipline of the test set. The maths matters later, when you tune models and diagnose failures, and the Python + AI/ML Beginner roadmap introduces it gradually in Weeks 14–17.
Why a decision tree, and not something more powerful?
Because you can picture what it does: a sequence of yes/no questions about the measurements. More powerful models exist, but they hide their reasoning. Learn the workflow on a transparent model first; swapping the algorithm later is one line of code.
What if my accuracy is not 1.0?
Then you have learned something real. Check first that you split before training and evaluated on the test set. If the code is right and the score is lower, the dataset is harder than iris. That is normal, and it is the point at which evaluation metrics and better data preparation start to matter.
Can I use this same script on my own data?
Yes, with two changes: replace load_iris() with your own features and labels (a pandas DataFrame works well. See Cleaning Data with pandas), and make sure there are no missing values first, because scikit-learn will refuse to train on them. The five steps stay exactly the same.
Where does this sit in the FUTEAC Python roadmap?
This is the last tutorial in the Python cluster's Doc 28 series and the practical payoff of Weeks 14–17 ("Machine learning basics") of the Python + AI/ML Beginner roadmap (/roadmap/python-ai-ml-beginner/). The next step is the ML Project Workflow skill (/skill/ml-project-workflow/): a full project from problem definition to presentation.
Follow the Python + AI/ML Beginner Roadmap
Learn Python from zero and take your first steps into AI and machine learning. Free, step by step, at your pace.
View the Roadmap