Machine Learning ML-02.01
Data types and datasets
Tell numerical, categorical, ordinal, text, image, time-series and graph data apart, and pick a sensible file format for each.
The big idea
A model only ever sees numbers. Before you can turn your data into numbers, you have to know what kind of thing each piece of data is. The number 3 in a "bedrooms" column and the number 3 in a "zip code region" column look identical in a spreadsheet, but one means "more than 2" and the other is just a name. Treat them the same and your model learns nonsense.
Think of a kitchen. Flour, eggs and saffron all go into food, but you measure flour by weight, count eggs, and add saffron by the pinch. Knowing what an ingredient is tells you how to handle it. Data types are the same: the type decides which operations make sense, how to encode it for a model, and which family of models will do best.
How it works
Step 1: Numerical data
NumericalData measured as a quantity, where arithmetic makes sense: price, age, temperature, number of clicks. data is a quantity. Differences and averages mean something: a 90 square metre flat is 18 square metres bigger than a 72 square metre one. Numerical columns can be continuous (temperature, price) or counts (bedrooms, clicks). Most models can use them almost directly, often after scaling, which you will meet in Feature engineering .
Step 2: Categorical and ordinal data
CategoricalData that names a group with no natural order, such as city, colour or product type. data names a group: city, browser, product type. There is no order, so "Paris minus Lyon" means nothing even if you store them as codes 0 and 2. The number of distinct values is the column's cardinalityThe number of distinct values a categorical column takes. A country column has about 200; a user id column can have millions.. A country column has a couple of hundred values; a user id column can have millions, which changes how you encode it.
OrdinalCategorical data whose values have a meaningful order but uneven or unknown gaps, such as poor, good, excellent. data has an order but no reliable distance. "Excellent" beats "good" beats "poor", yet nobody can say the gap from poor to good equals the gap from good to excellent. Shoe sizes, star ratings and education levels are ordinal. Encoding them as 0, 1, 2 keeps the order, which is usually what you want.
Step 3: Unstructured data: text, images, audio
UnstructuredData without a fixed set of columns, such as free text, images, audio and video. Models must learn their own features from it. data has no fixed columns. A review is a sequence of words of any length; a photo is a grid of pixel values, often a million or more of them; audio is a long stream of amplitude samples. You cannot hand a model "column 3" of a photo and expect it to mean anything. Instead, deep neural networks learn their own features from the raw values. That is why deep learning dominates images, text and audio.
Step 4: Time series and graphs
Some data gets its meaning from structure between records. A time seriesMeasurements of the same thing taken in order over time, such as daily sales or a sensor reading every second. is a sequence of measurements in time order: yesterday's sales tell you a lot about today's, so you cannot shuffle the rows. A graphData made of items (nodes) and the connections between them (edges), such as users and who follows whom. is items plus links: users and who follows whom, or atoms and bonds. Order and links carry information that a plain table throws away.
Step 5: Tabular versus unstructured, and why it matters
A rule of thumb that holds up well in practice: for tabular data, tree-based models such as gradient boosting ( XGBoost, LightGBM and CatBoost in practice ) are usually the strongest starting point. For unstructured data, deep learning wins. Knowing which side you are on picks your first model before you write any code.
Step 6: Dataset formats
- CSV: plain text, one row per line. Readable by everything, but types are lost (everything is a string until parsed), files are large, and reading one column means scanning every row.
- ParquetA column-oriented file format that stores each column together, compressed and with its type, so analytical queries read only the columns they need.: stores each column together, compressed, with types. Similar values sit next to each other, so they compress well, and a query that needs two of fifty columns reads only those two. The default for large tables.
- Image folders: one file per image (JPEG, PNG), labels in folder names or a separate CSV.
- JSONLJSON Lines: a text file with one JSON object per line. Good for records with nested fields or long text.: one JSON object per line. Good for nested records and text, such as chat logs or documents, and easy to stream or append to.
Here is one CSV parsed with explicit types in pandas, including a true ordinal column:
import io
import pandas as pd
# A tiny CSV export of house listings. In a CSV everything is text until parsed.
raw = """id,size_m2,city,condition,listed,description
1,72,Lyon,good,2026-03-02,bright flat near the river
2,115,Paris,excellent,2026-03-05,top floor with balcony
3,58,Lyon,poor,2026-03-09,needs work
4,90,Nantes,good,2026-03-11,quiet street garden
"""
df = pd.read_csv(io.StringIO(raw), parse_dates=["listed"])
# Tell pandas what each column really is.
df["city"] = df["city"].astype("category") # categorical
df["condition"] = pd.Categorical(df["condition"],
categories=["poor", "good", "excellent"],
ordered=True) # ordinal
print(df.dtypes)
print()
print("ordinal compare:", list(df["condition"] > "poor"))
print("city codes: ", list(df["city"].cat.codes))
print("days on market: ", list((pd.Timestamp("2026-03-15") - df["listed"]).dt.days))
# One record as JSON Lines: one JSON object per line, good for nested or text data.
print()
print(df.head(1).to_json(orient="records", lines=True, date_format="iso").strip())Output
id int64
size_m2 int64
city category
condition category
listed datetime64[us]
description str
dtype: object
ordinal compare: [True, True, False, True]
city codes: [0, 2, 0, 1]
days on market: [13, 10, 6, 4]
{"id":1,"size_m2":72,"city":"Lyon","condition":"good","listed":"2026-03-02T00:00:00.000","description":"bright flat near the river"} Notice that condition > "poor" works because the column is declared ordered, and the city codes 0, 2, 0, 1 are just labels: the code for Paris (2) is not "more" than Lyon (0) in any meaningful way.
Try it
Try this:
- Before checking, find the item that looks numerical but is not. Why would averaging it be a bug?
- Which items would you store in Parquet, and which in a folder or JSONL file?
- Pick any ordinal item and argue for treating it as numerical instead. When is that acceptable?
Trade-offs
| Option | Wins when | Loses when |
|---|---|---|
| CSV | Small data, sharing with humans, any tool must open it | Large tables, many columns, you need types preserved |
| Parquet | Large tables, analytics that read a few columns, repeated reads | You need to eyeball or hand-edit the file |
| JSONL | Nested records, text documents, appending streams of events | Wide numeric tables, where it is bulky and slow |
| Ordinal as integers | The order matters and gaps are roughly even | Gaps are very uneven; one-hot may be safer |
Common mistakes
- Feeding id numbers, postal codes or category codes to a model as if they were quantities.
- Shuffling time-series rows and then splitting randomly, which lets the model peek at the future (see Data leakage ).
- Letting a CSV reader guess types: leading zeros vanish from codes, and dates stay strings.
- Treating an ordinal column as plain categorical and throwing away its order.
- Storing a 50 GB table as CSV and wondering why every query takes minutes.
In the wild
Data warehouses and data lakes store analytical tables in columnar formats like Parquet, which is why a query over two columns of a huge table can return quickly. Image datasets such as ImageNet are distributed as folders of image files with label lists, and text datasets for training language models are commonly shipped as JSONL. Tabular data inside banks, retailers and insurers is still where gradient boosting models do much of the everyday work.
Check yourself
-
A column holds store ids like 1043, 2210 and 3307. How should you treat it?
Show answer
B. Categorical, because each value names a store The digits are a name. Store 3307 is not three times store 1043, so arithmetic on it would be meaningless.
-
Which is ordinal?
Show answer
C. Pain level: none, mild, moderate, severe Pain levels have a clear order but no guarantee that the steps are equal, which is exactly ordinal data.
-
Your analytics query reads 3 columns of a 200-column, 1-billion-row table. Which format helps most?
Show answer
B. Parquet Columnar storage lets the reader skip the other 197 columns, and compression makes the 3 it reads smaller.
-
You are predicting tomorrow's electricity demand from hourly readings. What is special about this data?
Show answer
B. It is a time series, so row order carries information and random shuffling is dangerous Each reading depends on the ones before it. Shuffling breaks that, and random splits leak future values into training.
Recap
- The data type decides which operations make sense and how to encode a column for a model.
- Numerical data is a quantity; categorical names a group; ordinal has an order but no fixed gaps.
- Text, images and audio are unstructured; deep learning learns features from them directly.
- Time series and graphs carry information in their order and links, so keep that structure.
- Use CSV for small and shareable, Parquet for large analytical tables, JSONL for nested records and text.
Next up
- From notebook to production learning path