Creating features
Date parts, cyclical encodings, ratios, interactions and text features.
Lesson 17 of 6612 minIntermediate
You will learn to
- Extract date parts and encode cycles
- Build ratio and interaction features
- Turn text into numbers (TF-IDF, embeddings)
Before this lesson
Intuition
A model only sees the columns you give it. A raw timestamp such as 2026-03-14 23:00 is one column, but it hides several facts that matter for the answer: it is a Saturday, it is late in the evening, it is the first quarter of the year. A person reads those facts at a glance. A model that receives the timestamp as a single number cannot.
Feature engineering means creating new columns that make the useful facts easy to see. You are not adding information that was not in the data. You are changing its shape so that a simple model can use it. Four families cover most cases:
- Date parts. Split a date into year, month, day, day of the week, hour and quarter.
- Cycles. Hours, days of the week and months repeat. They need an encoding that knows that the end of the cycle is next to its start.
- Ratios and interactions. Divide or multiply columns when the relationship between two values matters more than each value alone: debt divided by income, price per square metre, age times number of visits.
- Text. Words are turned into numbers, either by counting them or by using a model that places similar texts close together.
Good features often help more than a more complex algorithm. But every new column is also one more chance to add noise or to leak the answer (see the lesson on data leakage), so create features for a reason you can state.
Picture it
On the left, the 24 hours sit on a clock. Hour 23 and hour 0 are next to each other. A model that receives the plain integers 0 to 23 sees a long ramp with a cliff between 23 and 0, and it has to learn that the cliff is not real. The sin and cos pair gives it the circle directly.
The math
Cyclical encoding. For a value in a cycle of length (24 for hours, 12 for months, 7 for days of the week), create two columns:
You need both. Sine alone gives the same value for two different hours (for example hour 3 and hour 9 on a 24-hour clock), and the cosine tells them apart.
Ratio and interaction. A ratio is and an interaction is . A linear model computes , a weighted sum, so it cannot represent either one unless you add the column. Tree models can approximate them with many splits, but an explicit feature is cheaper and more stable.
TF-IDF (term frequency, inverse document frequency) gives a word in a document the weight
where is how often the word appears in the document, is the number of documents and is how many documents contain the word. Implementations add smoothing details, so exact values differ a little between libraries, but the idea is the same: frequent in this document and rare in the others means informative.
Embeddings are the other route. A language model turns a whole sentence into a vector of numbers (for example 384 of them) in which similar meanings are close together. Embeddings capture meaning that word counts miss, such as two different words with the same sense, at the price of more columns and more computing.
Worked example
Take a delivery order placed at 2026-03-14 23:00.
Date parts. The timestamp gives year 2026, month 3, day 14, hour 23, quarter 1 and day of the week 5 (the day-of-week numbers start at 0 for Monday, so 5 is Saturday).
Cycle. Encode the hour with period 24. The values below are computed with and .
| Hour | sin | cos |
|---|---|---|
| 23 | -0.259 | 0.966 |
| 0 | 0.000 | 1.000 |
| 6 | 1.000 | 0.000 |
| 12 | 0.000 | -1.000 |
Now measure the straight-line distance between points. Hours 23 and 0: . Hours 0 and 12 (the opposite side of the clock): 2.00. Hours 0 and 6: 1.41. As raw numbers hours 23 and 0 are 23 apart and hours 0 and 12 only 12 apart, which is the wrong order. After encoding, the closest pair is the pair that really is closest.
Ratio. Suppose a customer has debt 12,000 and income 40,000. The new column debt / income is 0.30. A second customer with debt 30,000 and income 150,000 has a ratio of 0.20. The second customer owes two and a half times as much, yet carries a lighter burden. The ratio shows this at once, and the two raw columns do not.
Check the effect. To know whether a feature helps, train with and without it using the same cross-validation folds and compare the scores. If the score does not move, drop the feature.
How mlflow does it
In Pipeline Studio the Choose... list groups these transformations by what they do. Under Dates, Split into date parts derives year, month, day, day of week, hour and quarter columns from a date column; by default it extracts year, month, day and day of week (a date column that no step handles gets those four parts automatically). Treat as a cycle encodes a periodic number column as a sin and cos pair and needs a period (the default is 12). Under Text, Word importance (TF-IDF) and Word counts turn a text column into word weights or counts (by default the 200 most frequent words), Topics (SVD) compresses those columns into fewer ones (50 by default), and Meaning (embeddings) produces embeddings with a language model. Ratio and interaction columns are not steps today; create them in your data before you add it. See the preprocessing reference in the docs.
Common mistakes
- Feeding a cycle as a plain number. The model sees a cliff between the end and the start of the cycle. Encode hours, weekdays and months with a sin and cos pair, or one-hot encode them when the cycle is short.
- Using the wrong period. A period of 12 for an hour column folds the day in half and mixes morning and evening. Use 24 for hours, 7 for days of the week, 12 for months.
- Building features from the target. An average of the target per customer, computed over all the rows, leaks the answer into the training data. Compute any target-based feature inside each training fold only.
- Adding hundreds of text columns to a small dataset. Thousands of word columns for a few hundred rows invite overfitting. Limit the number of words or compress them with SVD.
- Keeping every new feature. A feature that does not improve the cross-validated score adds noise and cost. Test each one and remove the ones that do not help.
Try it in mlflow
Add date features
- Open Pipelines and pick a pipeline whose dataset has a date or time column.
- In the Columns part, find the date column, press Choose... and, under Dates, pick Split into date parts. Press its chip to choose the parts you want in Settings (year, month, day, dayofweek, hour, quarter).
- Add Cyclical Variables after it for a column such as the month, and set its period to 12 (use 24 for an hour, 7 for a day of the week).
- Train from Review & run (Train N models) and compare the run with one that has no date features.