Loading the Iris Dataset with scikit-learn
The Iris dataset is made of two NumPy arrays: X holds the data and y holds the correct outputs.
Tracing What the Loading Code Produces
Every machine learning project begins with data, and the first practical skill is getting that data into your program. This article follows one small, complete step: loading a classical dataset called the Iris dataset so that Python can work with it. Before reading the explanation, run the code below and watch what each line does. The explanation will make more sense once you have seen the real execution.
What do you think happens?
After the line iris_dataset = load_iris() runs, what kind of object does the variable iris_dataset hold?
Reveal answer
A Bunch object, which is similar to a dictionary and contains keys and values.
The function load_iris returns a Bunch object. That object is not the two arrays themselves. The two arrays sit inside it, and you reach them through its keys.
The surprise in that reveal is the key idea of this article. The call does not hand you the measurements directly. It hands you one container, and the measurements and the correct answers are stored inside that container in separate places. Keep that in mind as the rest of the article traces where each half lives.
What the Iris Dataset Is and Where It Comes From
The Iris dataset is a classical dataset in machine learning and statistics. scikit-learn, a Python machine learning library, includes it in its datasets module.
You do not have to find, download, and clean up the Iris data yourself in this lesson. The library ships with the data, so retrieving it takes a single function call. The goal here is to understand that retrieval step and the structure of what it returns. Exploring the contents of the dataset in depth comes later.
Because the Iris dataset appears constantly in scikit-learn tutorials, it is a common first dataset for learning how supervised data is organized. Its importance comes from its shape and its simplicity, not from any special behavior of the code that loads it.
Two Separate Actions: Import and Call
There are two separate actions in the loading code, and it helps to keep them apart. The first is the import statement, from sklearn.datasets import load_iris. It tells Python where to find the name load_iris. Importing does not run the function. It only makes the name available.
The second action is the call, load_iris(). The parentheses are what make the call happen. The function then returns a value, and the assignment statement iris_dataset = load_iris() stores that returned value in the variable iris_dataset. Reading the line from right to left matches the order of events: the function runs first, then the result is stored under a name.
The Bunch Container
The object returned by load_iris is a Bunch object. It is very similar to a dictionary: it contains keys and values. A key is a name you use to look something up, and the value is what that name points to. In this dataset, the two keys you need are data, which holds the measurements, and target, which holds the correct answers.
The diagram shows the split that happens next. The data key supplies the X array, and the target key supplies the y array. You access a key by writing the object name, a dot, and the key name, so iris_dataset.data gives you X and iris_dataset.target gives you y. Both come from the same returned object, which is why they describe the same samples.
The Data Array X
The first array is called X. It holds the data itself, the measurements that describe each example. X is two-dimensional: it has one row per data point and one column per feature. For the Iris dataset, each row is one flower, and the columns are four measurements taken from that flower. That gives a total of 150 rows, one for each flower, and 4 columns, one for each measurement.
The diagram uses a made-up array with four rows and two columns, so that every number is easy to follow by hand. Each row is one sample, and the two values in that row are its two features. The same reading applies to the real Iris array, where each row carries four feature values instead of two. Reading the index tells you the sample: X[2] is the third row, which is the third sample.
Reading the shape of a small X and y
Given X = [[1, 2], [3, 4], [5, 6], [7, 8]] and y = [0, 1, 2, 1], report the shape of each array and explain what each number means.
- 1Count the rows of X
X has four inner lists, so it has four rows. Each row is one data point.
- 2Count the columns of X
Each inner list has two values, so X has two columns. Each column is one feature.
- 3Count the entries of y
y has four values, one for each sample. It has no inner lists and no column structure.
- 4Read the shape
A two-dimensional array reports two numbers, rows then columns. A one-dimensional array reports one number, its length.
X has shape (4, 2), meaning four rows and two columns. y has shape (4,), meaning one dimension of length four.
The Label Array y
The second array is called y. It holds the correct or desired outputs that go with each piece of data. y is one-dimensional: it has one entry per sample and no column structure. Each entry is one integer class label from 0 to 2, so each flower's correct answer is a single whole number chosen from three possible classes.
Position 2 in y is the answer for row 2 in X. In the made-up example, X[2] is [5, 6] and y[2] is 2, so together they describe one sample and its correct label. The two arrays must be read together, because neither one means much alone. The label 2 is meaningless without the row it belongs to, and the row [5, 6] is only a training example once its answer is attached.
Mistakes Beginners Make When Loading Iris
Common mistake Leaving off the parentheses after load_iris
What it looks likeiris_dataset = load_irisWhy it failsThis line stores the function itself under the name
iris_dataset. Nothing is executed, so no data is returned, and the value is not the Bunch object you expected.Do this insteadiris_dataset = load_iris()Common mistake Treating y as if it had columns
What it looks likey.shape is read as a pair of numbers such as (4, 1)
Why it failsy is one-dimensional. It has one entry per sample and no column structure, so its shape has a single number.
Do this insteadExpect y.shape to contain one number, its length, and X.shape to contain two numbers, rows and columns.
Common mistake Reading X and y as unrelated arrays
What it looks likeShuffling or slicing X without applying the same change to y
Why it failsX[i] and y[i] describe the same sample. Changing one without the other breaks the alignment that the model depends on.
Do this insteadKeep both arrays in the same order, and check that the entry at position i in y is the answer for the row at position i in X.
Common mistake Confusing the Bunch object with the arrays inside it, and expecting iris_dataset itself to behave like a NumPy array
When a result is not what you expected, trace the line where the value is created or stored. A missing set of parentheses is a common cause: the code runs without an error, yet the variable holds the wrong kind of object. Compare what the line actually does with what you expected it to do, and the mismatch usually points straight to the line at fault.
Finding the Line Where a Loading Mistake Happens
- Line 1: the import tells Python where
load_irislives - Line 3: the name is assigned without a call, so no Bunch is created
- Line 4: accessing .data on a function fails, because a function has no data key
Read the code above and find the first line where the actual behavior differs from what was intended. Intended behavior: iris_dataset holds the Bunch object that load_iris returns. Name the line, state what it actually stores, and write the corrected line.
Hints
- Compare the right-hand side of line 3 with the import on line 1. What does the name
load_irisrefer to there? - A function only runs when you call it, and a call needs parentheses.
- The corrected line should use
load_iris()on the right-hand side.
Checking Your Understanding of X and y
Suppose a loaded dataset has X.shape equal to (150, 4) and y.shape equal to (150,). Without running any code, answer three questions: how many flowers are there, how many measurements does each flower have, and what does the single number in y.shape count?
Hints
- The first number in X.shape counts rows, and each row is one data point.
- The second number in X.shape counts columns, and each column is one feature.
- y has one dimension, so its single number counts entries, one per sample.
Key Takeaways from the Loading Step
- 1The Iris dataset is made of two NumPy arrays: X holds the measurements, and y holds the correct integer class labels.
- 2X is two-dimensional, with one row per flower and one column per feature. y is one-dimensional, with one label per row.
- 3Import
load_irisfrom sklearn.datasets, then call it with parentheses to get a Bunch object that holds the data and target keys. - 4When something is wrong, check the line where the value is created, and compare what it actually does with what you expected.
At a glance
- The Iris dataset is made of two NumPy arrays: X holds the data and y holds the correct outputs.
- X is two-dimensional, with one row per data point and one column per feature. y is one-dimensional, with one integer class label from 0 to 2 per sample.
load_irisis imported from sklearn.datasets and must be called with parentheses. It returns a Bunch object, a dictionary-like container of keys and values.- When a loading result is wrong, trace the line where the value is created or stored, such as a missing set of parentheses.