Concepts / Your First Machine Learning Application: Iris Classification

Your First Machine Learning Application: Iris Classification

The first point of trouble is the import that pulls tools from model_selection.

  • Programming

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.

python
Output (expected)
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.

Key point

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.

Before
from sklearn.model_selection import train_test_split

Installed scikit-learn is 0.18 or later, so model_selection exists and the course import works as written.

After
# 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?

  • Yes, because the import line is standard

  • No, because model_selection was added in version 0.18

Reveal answer
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.

Definition

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.

Definition

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.

Iris datasetall measured flowersSampleone flower, one rowFeaturesfour measurements in cmClassone of three speciesLabelcorrect species of thisflowercontainsdescribed byhastakes one value from
What contains what: a sample is one flower (one row), its features are its four measurements, its label is the correct species for that row, and a class is one of the three possible species.

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.

Feature row 0four measurements of sample0labels[0]species of sample 0Feature row 1four measurements of sample1labels[1]species of sample 1Feature row 2four measurements of sample2labels[2]species of sample 2same position 0same position 1same position 2
Each feature row at position i is matched to the label at position i of the label array. Sample 0 uses row 0 and labels[0].
Worked example

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.

  1. 1
    Identify the sample

    The flower at position 2 is one data point, so it is sample 2 and occupies one row of the dataset.

  2. 2
    Identify the features

    Its four measurements (petal length, petal width, sepal length, sepal width, all in centimeters) form its feature values.

  3. 3
    Identify the label

    The correct species recorded for this flower is its label, the output the model learns to predict.

  4. 4
    Identify the class

    The species is one of the three possible classes: setosa, versicolor, or virginica.

Result

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 like

    An import from sklearn.model_selection raises an error on an older installation.

    Why it fails

    The 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 instead

    Run 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 like

    Uninstalling a newer scikit-learn to match 0.18 exactly.

    Why it fails

    The requirement is a floor. Any version at least as recent as the one used when the material was written is acceptable.

    Do this instead

    Keep 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 like

    Adding the species as a fifth feature column alongside the four measurements.

    Why it fails

    A label is the correct output for one data point, not an input measurement. The model learns to predict it.

    Do this instead

    Keep 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 like

    Reordering the feature rows but leaving the label array in its original order.

    Why it fails

    Position i in the labels array refers to row i of the features. Reordering one without the other breaks the pairing.

    Do this instead

    Keep the feature rows and the label array aligned at every position.

Practice: Reading the Iris Setup

EASY

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.
MEDIUM

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_selection module 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.

Related concepts

More concepts starting with Y