You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
After this unit you will: be able to correctly name the type of a feature — the first step in choosing the right way to handle it.
By the end of this lesson you can:
Key idea: A feature is one column describing each example. Knowing whether it is numerical (discrete or continuous) or categorical (nominal, ordinal or boolean) is the first step in choosing how to prepare it, especially how to turn categories into numbers.
A feature is a column of data that describes something about each example. The lecture illustrates two families: numbers (0, 2, 4, 6, 8) and categories (icons of a shopping bag, a monitor, a shopping cart and a t-shirt).
The difference matters because algorithms and preprocessing steps treat numbers and categories very differently.
| Subtype | Values | Examples from the lecture |
|---|---|---|
| Discrete | Separate, countable values, usually whole numbers (shown on a number line from -5 to 5 in whole steps) | Number of bedrooms, number of children, number of purchases |
| Continuous | Any value in a range, including decimals (conceptually from minus infinity to plus infinity) | Height (172.85 cm), weight, temperature (28.6 degrees), price |
You can have 2 or 3 bedrooms, but never 2.7 bedrooms, so bedrooms is discrete. A person can be 172.85 cm tall, so height is continuous. The distinction affects how you visualise a feature, how you preprocess it and sometimes which algorithm suits it best.
| Subtype | Meaning | Examples |
|---|---|---|
| Nominal | Categories with no natural order or ranking | Cities such as Hanoi, Ho Chi Minh City, Hue; colours |
| Ordinal | Categories with a natural order, but no exact numeric distance | Sizes S, M, L |
| Boolean | Only two possible values | Yes or no, true or false (shown as toggle switches) |
Hanoi does not come "before" Hue in any natural sense; they are just different labels, so city is nominal. Small is less than medium and medium is less than large, so size is ordinal, even though we never said how much bigger. Boolean features are the simplest case, with exactly two values.
Later in this module you will convert categorical data into numbers that a model can use. Nominal and ordinal features are converted in different ways: ordinal features can keep their order as numbers, while nominal features must not be given a fake order. Getting the type right now prevents a modelling mistake later.
Be careful with the storage format. A column can contain digits and still be categorical. A province code such as 01 for Hanoi or 79 for Ho Chi Minh City is a label, not a quantity: adding or averaging codes means nothing.
Classify the columns of a small housing table. pandas can tell you which columns are stored as numbers, but only you can decide what each column really means.
import pandas as pd
houses = pd.DataFrame({
"square_feet": [60.5, 45.0, 80.2],
"num_bedrooms": [3, 2, 4],
"province_code": [1, 79, 48],
"tier": ["Mid-range", "Affordable", "Luxury"],
"has_garden": ["Yes", "No", "Yes"],
})
print(houses.select_dtypes("number").columns.tolist())
feature_types = {
"square_feet": "numerical, continuous",
"num_bedrooms": "numerical, discrete",
"province_code": "categorical, nominal (digits are only labels)",
"tier": "categorical, ordinal (Affordable < Mid-range < Luxury)",
"has_garden": "categorical, boolean",
}
for col, kind in feature_types.items():
print(f"{col:14} -> {kind}")
Expected output: the first line lists ['square_feet', 'num_bedrooms', 'province_code'] because those columns are stored as numbers. The loop then prints the real type of each column. Notice that province_code is stored as numbers but is actually a nominal category, so the storage type alone is not enough.
Flashcards · 1.5 Feature Types
9 wordsA column of data that describes something about each example.
A feature whose values are measured quantities represented by numbers.
A feature whose values are categories or labels rather than measured amounts.
A numerical feature that takes only separate values, usually whole numbers.
A numerical feature that can take any value within a range, including decimals.
A categorical feature whose categories have no natural order.
A categorical feature whose categories have a natural order or ranking.
A categorical feature with only two possible values.
pandas method that selects columns by storage type; it cannot tell you a column's real meaning.