Data, features and labels
Last updated
A model can only read numbers. Before any learning happens, a real problem has to become a table of numbers X, one row per example and one column per feature, and an array y of the answers. This lesson does that for the penguins: measurements, categories like the island, and even text, each turned into numbers in a way that doesn’t mislead the model.
By the end of this lesson you can
- Name the examples, features and labels in a real problem
- Turn a table into a feature matrix X and a label array y
- One-hot encode a category, and explain why numbering categories 1, 2, 3 misleads a model
- Turn short texts into word counts
Examples, features and labels
Three words describe every supervised dataset:
- An example (also a sample, an instance, a row) is one thing you have data about: one penguin, one message, one house sale.
- A feature is one thing you know about every example, which the model may use: bill length, the words in a message, a house's floor area.
- The label (also the target) is the answer you want the model to give: the species, spam or ok, the price.
Choosing features is a real decision, not a formality. A feature must be something you'll know when you need the prediction. A house's sale price can't be a feature for predicting its sale price; nor can "the date the house sold" if you want to price it before it's listed. Lesson 2.10 shows what goes wrong when a feature quietly carries the answer.
Numbers, categories and text
Features come in three common kinds, and a model needs all of them as numbers:
- Numbers on a scale, like bill length in mm, can be used as they are (though lesson 2.4 shows they often need scaling).
- Categories, like the island (Biscoe, Dream or Torgersen), have no natural number. Step 3 shows the standard way to turn them into numbers.
- Text has to be cut into pieces and counted. The simplest way, the bag of words, counts how often each word appears and forgets the order:
Each text becomes a row of counts, one column per word in the vocabulary. "free lunch for you" has a 1 under "for", "free", "lunch" and "you". Stage 11 does much better than this, but it's a real method, and spam filters used it for years.
One-hot encoding
One-hot encoding turns a category with possible values into columns of 0s and 1s. Each example has a 1 in the column for its value and 0 in the rest: exactly one column is "hot". Step through the figure to watch a table become and :
| species | island | bill length | bill depth | flipper | mass |
|---|---|---|---|---|---|
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 |
| Adelie | Biscoe | 37.8 | 18.3 | 174 | 3400 |
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 |
| Gentoo | Biscoe | 46.1 | 13.2 | 211 | 4500 |
The table, as it comes.
In pandas it's pd.get_dummies; in scikit-learn it's OneHotEncoder, which remembers the categories it saw so it can encode new data the same way later:
The first three penguins in the file are all from Torgersen, so each has a 1 in the third column only. (OneHotEncoder gives a "sparse" result that stores only the 1s, to save memory; .toarray() shows it in full.)
Wrong way: categories as 1, 2, 3
The tempting shortcut is to number the categories: Biscoe 1, Dream 2, Torgersen 3. It's short, and it's wrong, because a model treats numbers as numbers:
With codes, Biscoe is 1 away from Dream but 2 away from Torgersen, as if Dream sat between them; and the "average" of Biscoe and Torgersen is Dream. None of that is true: they're three islands, in no order. A model measuring distance (lesson 2.4) or multiplying by a weight (Stage 3) would take that made-up order seriously.
With one-hot encoding, every island is exactly from every other: no island is closer, bigger or "between". Use numbered codes only for categories that really have an order, like small, medium and large; scikit-learn's OrdinalEncoder is for those.
The data matrix
Put it all together and the data becomes a feature matrix with rows (examples) and columns (features), and a label vector with entries:
In words: row of , written , holds example 's features, and is its label. Row of and entry of always belong to the same example. In NumPy: a 2-D array of shape (n, d) and a 1-D array of shape (n,).
342 penguins, 4 features each: , . Capital for the matrix and small for the vector is the convention in scikit-learn's documentation and in this course. Every model from here on takes an X like this and a y like this.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- scikit-learn: Encoding categorical features: OneHotEncoder, and why OrdinalEncoder’s numbers imply an order
- pandas: get_dummies: one 0-or-1 column per category
- scikit-learn: Text feature extraction: the bag of words and CountVectorizer
- Horst, Hill and Gorman, palmerpenguins: Palmer Archipelago (Antarctica) penguin data, 2020: the penguins dataset, released under CC0