Your First Machine Learning Application: Iris Classification
The first point of trouble is the import that pulls tools from model_selection.
A Correct Program That Still Fails
Imagine you copy a working example from a tutorial onto your own computer, run it, and get an error about a module that the tutorial clearly imports. Nothing in the code itself is wrong. The difference is in the library. Machine learning code depends on a specific library, and that library changes over time. A program can be perfectly written and still fail if the library installed on your machine does not provide the pieces the program expects.
This article is about that dependency. The library in question is scikit-learn, the machine learning toolkit used throughout the examples that follow. Before you write a single line of modeling code, you need to know which version of scikit-learn your code expects, and whether the version on your machine is recent enough to supply it.
Checking Which scikit-learn You Have
Before running the course examples, you can ask Python which scikit-learn version it is actually using. The check imports the library and prints its version string. You then compare the printed value against the 0.18 minimum. Treat this as the first step of every session: a version check costs seconds, while a confusing import error can cost much longer to diagnose.
The printed value is a version string such as 0.18 or a later release number. Compare it against the minimum of 0.18.The 0.18 Floor and the model_selection Module
The course assumes that you have scikit-learn version 0.18 or later. Notice what this requirement does and does not say. It is a floor, not an exact match. The course does not ask you to install one particular release. It asks that your version be at least as recent as the one used when the material was written.
The reason 0.18 is the threshold is a specific feature: the model_selection module was added in that version. Many of the tools you will use to split data and evaluate models live inside this module, so the course code reaches for it directly. If your installed version predates it, the import statements that refer to that module will not match what your library provides.
The version rule is a minimum, so any release at or above 0.18 satisfies it. What matters is whether the module the code imports exists in your installed release.
What Changes on an Older Version
Now follow a learner whose installed version is older than 0.18. The learner keeps the course code exactly as written. The first point of trouble is the import that pulls tools from model_selection. Because that module did not exist yet in the older release, the import no longer matches the library. The learner is not expected to rewrite the modeling logic. The source's advice is narrower: the imports from this module must be adjusted.
from sklearn.model_selection import train_test_splitInstalled scikit-learn is 0.18 or later, so model_selection exists and the course import works as written.
# Adjust this import so it matches the tools your older version provides.
# The modeling logic that follows stays the same.Installed scikit-learn is older than 0.18, so model_selection is missing and the import must be adjusted.
What do you think happens?
You have scikit-learn 0.17 installed and run the course import from model_selection. Will that import succeed?
Reveal answer
No, because model_selection was added in version 0.18, so an older release does not provide it.
The course's version floor exists for exactly this reason. The import refers to a module that older releases do not contain, so the import fails even though the code itself is correct.
The Botanist's Iris Problem
Version compatibility is about running the right code, but the code itself solves a particular kind of problem. The course's first application comes from a hobby botanist who has measured irises. Each flower has measurements of petal length and width and sepal length and width, all in centimeters. Some of these irises have already been identified by an expert as belonging to one of three species: setosa, versicolor, or virginica. The botanist can be certain of those species, and she assumes these are the only species she will meet in the wild.
The goal is a model that learns from the measurements of irises with known species, so it can predict the species of a new iris. Because the correct answer is known for every measured flower in the training set, this is called a supervised learning problem. The answers supervise the learning: the model is shown both the measurements and the correct species.
A classification problem is one where the outputs to predict are one of several options. The possible outputs are called classes. The iris problem has three classes, one for each species, so it is a three-class classification problem. Every iris in the dataset belongs to exactly one of those classes.
A label is the correct output attached to one data point. Here, once a particular flower is known to belong to a particular species, that species is its label. A label is not a separate kind of measurement. It is the category the model learns to predict for new flowers.
Rows, Features, and Labels Line Up
The labeled dataset works because each row of measurements is paired with a label at the same position. Sample 0 is the first flower, and its four measurements sit in the first row of the feature data. The label for sample 0 sits at position 0 of the label array. Position i in the labels array therefore refers to the same flower as row i of the feature data. Nothing in the data links them except that shared position, which is why the order of rows must never be shuffled out of step with the labels.
Identifying Sample, Features, and Label
A dataset holds 150 flowers, each with four measurements and one species. For the flower at position 2, name its sample, its features, its label, and its class.
- 1Identify the sample
The flower at position 2 is one data point, so it is sample 2 and occupies one row of the dataset.
- 2Identify the features
Its four measurements (petal length, petal width, sepal length, sepal width, all in centimeters) form its feature values.
- 3Identify the label
The correct species recorded for this flower is its label, the output the model learns to predict.
- 4Identify the class
The species is one of the three possible classes: setosa, versicolor, or virginica.
Sample 2 has four feature values, one label naming its species, and that label is one of the three classes.
Mistakes Beginners Make with Versions and Labels
Common mistake Assuming the code is wrong when an import from model_selection fails
What it looks likeAn import from sklearn.model_selection raises an error on an older installation.
Why it failsThe course code is correct for version 0.18 or later. The problem is that the installed library does not yet provide the module.
Do this insteadRun the version check first, then adjust the imports from model_selection if your version is older than 0.18.
Common mistake Treating 0.18 as an exact version you must install
What it looks likeUninstalling a newer scikit-learn to match 0.18 exactly.
Why it failsThe requirement is a floor. Any version at least as recent as the one used when the material was written is acceptable.
Do this insteadKeep any release of 0.18 or later, and only change imports if your version is older.
Common mistake Thinking a label is an extra measurement
What it looks likeAdding the species as a fifth feature column alongside the four measurements.
Why it failsA label is the correct output for one data point, not an input measurement. The model learns to predict it.
Do this insteadKeep the four measurements as features and store the species separately as the label for each row.
Common mistake Shuffling feature rows without shuffling labels
What it looks likeReordering the feature rows but leaving the label array in its original order.
Why it failsPosition i in the labels array refers to row i of the features. Reordering one without the other breaks the pairing.
Do this insteadKeep the feature rows and the label array aligned at every position.
Practice: Reading the Iris Setup
A classmate says the iris problem is a regression problem because it predicts a number of outcomes. Using the vocabulary from this article, explain why the iris problem is a classification problem and name its classes.
Hints
- Ask what kind of output the model predicts: one of several named options, or a continuous value.
- The source names the three possible species as the classes.
Your course code imports from model_selection and you have scikit-learn 0.16 installed. Describe the two steps you would take, in order, before running any modeling code.
Hints
- Start by checking which version Python is actually using.
- Compare the version against the 0.18 floor, then decide which imports to adjust.
Key Takeaways
- The course assumes scikit-learn 0.18 or later, which is a minimum version, not an exact release.
- The 0.18 threshold exists because the
model_selectionmodule was added in that version, and imports from it fail on older releases. - Checking sklearn.
__version__before running the course code tells you whether your installed library matches the course's assumptions. - The iris problem is supervised and has three classes; each sample's label is its correct species, stored at the same position as its feature row.