Date and text features
Turn dates into parts and durations, keep cycles circular, and turn text into numbers a model can use.
You will be able to
- Extract date parts and durations
- Encode a cycle so its ends meet
- Choose between simple text features and term columns
Read first
A date holds many features
ReadA date is only useful once you ask something of it. In Prepare (⌘3):
- Date parts gives year, quarter, month, day, ISO weekday and week, hour, day of year and a weekend flag.
- Time between dates gives days, hours or weeks from a reference to a date.
"Days since signup" often predicts more than "signup date", because it's what the date means for that customer today.
Is 11 pm far from midnight?
PredictPredict first
You store hour of day as a number from 0 to 23. In reality, how far apart are 11 pm (hour 23) and midnight (hour 0)?
Reveal
The answer: Close, one hour apart
They're an hour apart, but the numbers say 23, and a linear model believes the numbers. Cyclical encoding replaces the hour with a sine and a cosine, so the ends of the cycle meet.
Try a date feature
In the appAdd the customer list, then start a recipe with these steps:
- Parse dates on
signup_date - Time between dates from
signup_dateto a fixed date - Date parts for the weekday
Look at the output grid. Then ask which of the new columns has a reason to matter for your question.
Simple text features
ReadLong before a language model, text gives you easy numbers. Text features adds length, word count and counts of digits, capitals and punctuation. Flag text matching a pattern adds a true/false column, such as "mentions a refund".
These are cheap, explainable and often surprisingly useful.
Term columns
ExploreText to term columns makes one column per frequent term: counts, presence or TF-IDF, with a vocabulary learned from the data.
Try it on the notes column, and look at which terms become columns. Most rows there are empty, which is itself a finding, so consider a Flag missing values step too.
The vocabulary is a learned value. For a model, learn it on training rows only.
Look ahead
ReadIn code
Readimport numpy as np
import pandas as pd
df = pd.DataFrame({"when": pd.to_datetime(["2024-01-05 23:00", "2024-01-06 00:00"])})
df["weekday"] = df["when"].dt.dayofweek
hour = df["when"].dt.hour
df["hour_sin"] = np.sin(2 * np.pi * hour / 24)
df["hour_cos"] = np.cos(2 * np.pi * hour / 24)library(dplyr)
library(lubridate)
df <- tibble(when = ymd_hm(c("2024-01-05 23:00", "2024-01-06 00:00")))
df <- df |>
mutate(
weekday = wday(when, week_start = 1),
hour_sin = sin(2 * pi * hour(when) / 24),
hour_cos = cos(2 * pi * hour(when) / 24)
)Check your understanding
CheckpointCheck yourself
With a 24-hour cycle, what is ? That's the sine feature at 6 am.
Show the answer
Answer: 1
, and .
Check yourself
Which feature is safe for predicting next month's churn?
Show the answer
Answer: a
The other two are computed from the outcome or from the future. They won't exist when you need the prediction.
Recap
Recap- Dates become useful as parts and durations.
- Encode cycles with a sine and a cosine.
- Start with simple text features, and learn any vocabulary on training rows.