Time and text features
Quick answer
Time features describe the calendar or elapsed history available at prediction time. Text features convert available text into numeric representations such as token counts. Their definitions must match the intended deployment and be frozen or versioned.
Circular day encoding
from math import sin, cos, pi, distdef encode(d): return sin(2*pi*d/7), cos(2*pi*d/7)print(round(dist(encode(6), encode(0)), 3))print(round(dist(encode(0), encode(1)), 3))Expected output:
0.8680.868Adjacent days have equal chord distance across the week boundary.
How it works
For a weekly pattern, encode day index d as sin(2pid/7), cos(2pid/7) so days 6 and 0 are adjacent on a circle. Use the time zone relevant to the task; store timestamps with offsets and distinguish elapsed duration from local calendar days across daylight-saving transitions. Lag and rolling aggregates must exclude future and late-arriving records. Text counts depend on tokenization, case normalization and vocabulary; fit the vocabulary on training text and ignore or explicitly represent unknown tokens at inference. CountVectorizer creates a sparse document-term matrix. TF-IDF also learns document frequency from training documents, so fit inside the training fold. Keep vocabulary size and high-cardinality categories under control. Do not use post-resolution notes for a prediction at ticket creation.
Frozen text vocabulary
from sklearn.feature_extraction.text import CountVectorizerv = CountVectorizer().fit(['red box', 'blue box'])print(v.get_feature_names_out().tolist())print(v.transform(['box box green']).toarray().tolist())Expected output:
['blue', 'box', 'red'][[0, 2, 0]]Green is outside the vocabulary; the dense conversion is only for this tiny display.
Common mistakes
Fitting vocabulary or inverse document frequency on the whole dataset leaks corpus information. Raw integer hour makes midnight and 23:00 appear far apart. A calendar feature may still be useful even if periodicity is not established; compare with a baseline.
Practice
Construct a two-token frozen vocabulary and count incoming text with an unknown word. Then compare distances between day 6 and day 0 in raw and cyclic encodings. Complete the chapter exercise and knowledge check. Scientific examples are local scikit-learn 1.8.0 static snippets; ByteMind execution support is not verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Time and text features — knowledge check
Apply feature engineering and leakage controls to synthetic scenarios.
Why use sine and cosine for weekday?
Which text may be used for ticket creation prediction?
What happens to unknown tokens in the demonstrated frozen vocabulary?
Where is IDF fitted?
Why record time zone?