NumPy Arrays for Machine Learning
The Iris dataset is split into two NumPy arrays: X for the data and y for the correct outputs.
Why a Model Needs Two Separate Arrays
Before a machine learning model can learn anything, the data has to arrive in a form the model can read. Many introductory examples in scikit-learn use the Iris dataset, and the way that dataset is organized is a good template for thinking about almost any labeled dataset. Instead of one tangled object, the Iris data is packaged as two separate pieces that work together.
The question for this article is simple: what are those two pieces, and how do they relate to each other? By the end, you should be able to look at the two arrays, say what each one contains, and follow a single flower from one array to the other.
Inputs Kept Apart From Answers
The Iris dataset consists of two NumPy arrays. The first holds the data, which scikit-learn refers to as X. The second holds the correct or desired outputs, which is called y. The division is deliberate: the inputs the model looks at are kept apart from the answers it is supposed to predict.
Keeping the two arrays separate makes the roles clear. X is the question the model looks at, and y is the answer it is supposed to predict. When you read code that works with this dataset, the name tells you the role: anything called X is input, and anything called y is the target.
X holds the inputs (features) and y holds the correct outputs (class labels). Separating them makes clear which part is the question and which part is the answer.
Reading the Shape of X
X is a two-dimensional array of features, with one row per data point and one column per feature. Reading the shape of X is a useful habit. If X has $n$ rows and $d$ columns, then it has $n$ data points and $d$ features per data point.
Two dimensions are needed because the data is a table. One axis selects which sample you are looking at, and the other selects which feature of that sample. The rows run across samples and the columns run across measurements taken on each sample. So reading across a row gives you every measurement for one flower, while reading down a column gives you one measurement for every flower.
Reading the shape of a small X
Suppose X is a small array with 3 rows, where each row holds 4 feature values. What is the shape of X, and what does each number count?
- 1Identify the rows
Each row is one data point, so the first number in the shape counts the rows. There are 3 rows, so n = 3.
- 2Identify the columns
Each row has 4 values, one per feature, so the second number counts the columns. There are 4 columns, so d = 4.
- 3Write the shape
The shape is written as (n, d), which here is (3, 4). That means 3 data points with 4 features each.
The shape of X is (3, 4): 3 data points (rows) and 4 features (columns).
Running the Split in Code
The example below builds a tiny version of the same structure by hand, so you can check every number. The first array X holds three samples with four features each, and the array y holds one integer label per sample.
(3, 4)
(3,)
[7. 3.2 4.7 1.4]
1- X is two-dimensional: its shape has two numbers, rows and columns.
- y has a shape with one number only, because it has no columns.
- X[2] selects row 2 of X, which holds the four features of one sample.
- y[2] selects entry 2 of y, which holds the label for that same sample.
Tracing One Sample Through Both Arrays
The easiest way to see how X and y fit together is to follow one data point through both arrays. The row of X for that data point gives its features, and the entry of y at the same position gives its class label. The position is the link between the two arrays.
What do you think happens?
If you pick out sample 2 from X, which entry of y describes that same flower, and what label does it hold?
Reveal answer
Entry at position 2 of y, which holds the label 1 in the tiny example.
Samples are matched by position, not by value. Row 2 of X and entry 2 of y describe the same sample.
The diagram shows the pairing for the first three samples. Each row of X is linked to the entry of y at the same position. When you pick out sample 2, you take row 2 from X for its features and entry 2 from y for its label, and both belong to one flower. The pairing depends on position, so the arrays stay aligned only as long as their order is kept.
Why y Holds Categories, Not Measurements
Because y is one-dimensional, it has no columns. It is a single list of answers, one per data point. The integers 0, 1, and 2 are class labels, which means they name categories rather than measuring quantities. They mark which of three categories a sample belongs to.
| Property | X | y |
|---|---|---|
| Dimensions | Two-dimensional, with rows and columns | One-dimensional, a single list |
| Contents | Feature measurements for each sample | One class label per sample |
| Each row or entry | One data point with several features | One integer label for one data point |
| Meaning of numbers | Quantities measured on each sample | Category names encoded as 0, 1, and 2 |
| Role for the model | Input the model looks at | Correct answer the model should predict |
How the two arrays differ in structure and meaning
Mistakes Learners Make With X and y
Common mistake Treating y as a set of numeric measurements
What it looks likeAveraging the values in y to describe how large the flowers are.
Why it failsEach entry in y names a category. The numbers 0, 1, and 2 are labels, not quantities.
Do this insteadRead each integer as category membership, such as which of the three groups a sample belongs to.
Common mistake Expecting y to have two dimensions like X
What it looks likeAssuming y has rows and columns and indexing it with two numbers.
Why it failsy is one-dimensional. It has one entry per data point and no columns.
Do this insteadIndex y with a single position, such as y[2], to read the label of one sample.
Common mistake Matching samples by value instead of by position
What it looks likeSearching y for the number that looks like a particular flower's measurements.
Why it failsSamples are matched by position. Row i of X and entry i of y describe the same sample.
Do this insteadUse the same index for both arrays when you want the features and the label of one sample.
Common mistake Reading the shape of X in the wrong direction
What it looks likeSaying the second number in the shape counts the data points.
Why it failsThe first number counts rows, which are data points. The second counts columns, which are features.
Do this insteadRead the shape as (n, d): n data points, each with d features.
Checking Your Reading of X and y
Using the tiny X and y from the code example, where X has shape (3, 4) and y = [0, 0, 1], answer these questions. Which entry of y belongs to the features in X row 0? How many features does each sample have? Is the label 1 twice the label 0 in any meaningful sense?
Hints
- Samples are matched by position, so row 0 of X pairs with entry 0 of y.
- The second number in the shape of X counts columns, which are features.
- Labels are category names. The value 1 marks a second category, not a larger amount.
Key Takeaways
- The Iris dataset is split into two NumPy arrays: X holds the data (inputs) and y holds the correct outputs (answers).
- X is two-dimensional, with one row per data point and one column per feature, so its shape is read as (n, d).
- y is one-dimensional, holding one integer class label per sample; the labels 0, 1, and 2 name categories rather than measure quantities.
- Samples are matched across the arrays by position: row i of X and entry i of y describe the same sample.