Your First Machine Learning Application: Classifying Iris Species
The application is built around a hobby botanist who has found some iris flowers and wants to tell their species apart.
A Program That Fails on an Older Release
Before you write any machine learning code, you need the right tools installed. The tool used in this material is scikit-learn, a Python library for machine learning. Different releases of scikit-learn do not always offer the same modules or the same import paths. A program that runs cleanly on one release can fail on an older one, because a function or module it depends on was not yet available when that older release was published.
This article uses the version requirement that comes with your first real application: classifying iris species. The requirement is short, but it teaches a useful lesson about how library versions connect to the code you write. Once you see how that connection works, you can check any version requirement you meet in the same way.
Following One Botanist's Flowers
The application is built around a hobby botanist who has found some iris flowers and wants to tell their species apart. For every iris she has measured four quantities in centimeters: the length and the width of the petals, and the length and the width of the sepals. Those four numbers describe one flower. Together they form a single record, and the whole problem is built out of many records like it.
Keep this one flower in mind as you read. Her four measurements tell you what the flower looks like. They do not, by themselves, say what species it is. Separating those two roles is the central move of this article.
Labeled Data: Inputs Versus the Answer
The botanist's situation is a supervised learning problem. In supervised learning, we have measurements for which we already know the correct output. The known correct outputs are what set this setting apart: the model is given examples together with their answers, and it learns the relationship between them.
A label is the desired output for one data point. The measurements describe the flower; the label names what the flower is. In this application a label is one of the iris species.
The four measurements are the inputs: the features that describe each flower. The species name attached to that same flower is the label. When the model is trained, both pieces are visible together. When it is later asked to predict, the label is the thing it has to supply from the measurements alone. The diagram shows this split for a single record: four input cells that describe the flower, and one label cell that names it.
What do you think happens?
In a labeled iris record, which part is the answer the model must learn to predict?
Reveal answer
The species name, such as setosa
The measurements are the inputs that describe the flower. The species name is the label, the correct output the model learns to match.
Supervised Learning, Classification, and Classes
Within supervised learning, this application is a classification problem. In a classification problem we want to predict one of several options, and here those options are the species of iris. The possible outputs are called classes.
Every iris in the dataset belongs to one of three classes: setosa, versicolor, or virginica. Because there are exactly three possible outputs, the problem is described as a three-class classification problem. The number of classes is a property of the problem itself, and it tells you how many distinct answers the model is allowed to give.
| Term | What it describes | Question it answers | Applied to iris |
|---|---|---|---|
| Supervised learning | How the model is trained | Does it learn from data where correct outputs are known? | Yes, the flowers are labeled with species |
| Classification | What kind of output is wanted | Which one of several named categories applies? | Yes, one species out of three |
The two terms describe the same problem from different angles.
The two terms are easy to mix up because they describe the same problem from different angles. Supervised learning describes how the model is trained. Classification describes what kind of output is wanted. The iris application is both at once: it is trained on labeled examples, and its output is one class out of three.
Why 0.18 Is the Floor
The material assumes that you have version 0.18 or later of scikit-learn. Read that as a floor, not a target. Anything at 0.18 or above is what the material expects, and the material also recommends a version at least as recent as the one its authors used.
The reason 0.18 is the floor is the model_selection module. That module was added in version 0.18. Any code that imports from model_selection depends on the release that contains it. If you install an earlier version, imports from this module will not resolve as written, and you will need to adjust them to match what your installed version provides.
Checking Your Installed Version
A version check is a quick way to confirm that your environment meets the floor before you run any iris code. The first step is to import scikit-learn and print the version string it reports. The second step is to confirm that the model_selection module can be imported. If the second import fails, your installed release is older than the one this material assumes.
0.19.2
model_selection is available- The first print shows the installed version; compare it with the 0.18 floor.
- The import of
model_selectionsucceeds only on releases that contain the module. - The version number in the output is an example and will differ on your machine.
Reading the output works like this: if the version printed is 0.18 or higher and the second line runs, your environment meets the requirement. If the import raises an error, the release is older than 0.18, and the imports from model_selection will need to be adjusted to match what that release provides.
Mistakes That Blur the Problem
Common mistake Treating the label as one of the measurements
What it looks likeFeeding the species name in as a fifth feature while also trying to predict it
Why it failsThe measurements describe the flower; the label names what the flower is. The label is the answer, not part of the description.
Do this insteadKeep the four measurements as inputs and the species as the label to be predicted.
Common mistake Using classes and labels as if they were the same thing
Why it failsA label is the desired output for one data point, while classes are the possible outputs of the whole problem.
Do this insteadSay the flower's label is versicolor, and the problem's classes are setosa, versicolor, and virginica.
Common mistake Assuming any scikit-learn release will run the code
Why it failsReleases differ in which modules they offer, and
model_selectionwas added in 0.18.Do this insteadCheck the installed version first and confirm model_selection imports before running the iris code.
Sorting Records Into Inputs and Labels
A new record from the botanist reads: petal length, petal width, sepal length, sepal width, and then the species name. Identify which values are inputs the model sees, which value is the label, and how many classes this problem has. Then explain in one sentence whether this problem is supervised learning, classification, or both.
Hints
- Measurements describe the flower; the label names it.
- Count the distinct species names that the problem allows.
- Supervised learning refers to training on known correct outputs; classification refers to predicting one of several named categories.
Key Takeaways
- The material assumes scikit-learn 0.18 or later, treated as a floor; a version at least as recent as the authors' is recommended.
- The
model_selectionmodule was added in 0.18, so earlier releases need their imports from that module adjusted. - In the iris application, the four measurements are inputs and the species name is the label the model must predict.
- Supervised learning describes how the model is trained on known correct outputs; classification describes predicting one of several classes, here three iris species.
- Checking the installed version and confirming the
model_selectionimport is a quick way to verify your environment before running the code.