What learning is · lesson 3 of 13
Parts in this lesson
AX = penguins[features].to_numpy()the feature matrix (from Stage 0)
Bpd.get_dummies(penguins['island'])one-hot encoding with pandas
COneHotEncoder().fit_transform(...)one-hot encoding with scikit-learn
DCountVectorizer().fit_transform(texts)text as word counts

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:
text.ipynb

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 m possible values into m 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 X and y:

Table to matrixstep 1 of 4
Table
speciesislandbill lengthbill depthflippermass
AdelieTorgersen39.118.71813750
AdelieBiscoe37.818.31743400
ChinstrapDream46.517.91923500
GentooBiscoe46.113.22114500

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:

onehot.ipynb

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:

codes.ipynb

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 2≈1.414 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 X with n rows (examples) and d columns (features), and a label vector y with n entries:

X=(x11x12⋯x1d⋮⋮⋮xn1xn2⋯xnd),y=(y1⋮yn)

In words: row i of X, written xi, holds example i's features, and yi is its label. Row i of X and entry i of y always belong to the same example. In NumPy: a 2-D array of shape (n, d) and a 1-D array of shape (n,).

xy.ipynb

342 penguins, 4 features each: n=342, d=4. Capital X for the matrix and small y 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

A model predicts whether a student will pass, from hours studied and attendance. What is the label?
Predict, then check with pd.get_dummies: a column of payment methods holds cash, GCash, card and Maya. How many columns does one-hot encoding make?
Why is coding small, medium and large as 1, 2, 3 acceptable, when coding islands that way isn’t?

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.

All sources for Simple Machine Learning