Skip to content
tezvyn:
Menu

Transform a time series for a supervised learning model?

Source: scikit-learn.orgMediumHow cards are made

This tests your ability to reframe a time series problem for tabular models. A great answer explains creating features from lags, rolling windows, and calendar data, then emphasizes using a time-aware validation split. A red flag is forgetting validation.

What's really being asked

This question tests your practical understanding of feature engineering for time series data. The interviewer wants to see if you can correctly reframe a sequential problem into a tabular format that standard supervised models expect (an X_train, y_train structure). It specifically probes your knowledge of feature creation and, most importantly, your awareness of time-based data leakage and the correct validation strategies to prevent it.

The full answer

A strong answer has three parts. First, explain the core concept: creating a supervised learning dataset where the target y is the value at time t, and the features X are derived from data at times < t. Second, detail the three main categories of features you would create. These are lag features (the value at t-1, t-7), window features (rolling aggregates like a 7-day moving average or 30-day max), and calendar features (day of week, month, is_holiday). Third, and most critically, specify the validation strategy. You must state that a simple random split is incorrect and that you would use a time-ordered split (e.g., train on 2021-2022, test on 2023) or a forward-chaining procedure like scikit-learn's TimeSeriesSplit.

The mistakes people make

A candidate who only mentions lag features is giving a junior-level answer. The biggest red flag, however, is failing to mention the validation strategy. Suggesting standard k-fold cross-validation or a random train-test split is an immediate signal that the candidate doesn't understand the risk of data leakage in time series, where the model would be trained on future data to predict the past, leading to inflated performance metrics and a useless model in production.

What usually comes next

Expect follow-up questions like: "How do you choose the right lag or window size?" (Answer: domain knowledge, autocorrelation plots, or treat it as a hyperparameter). "What if you have multiple related time series?" (Answer: use their lags and window features as input features for the main series). "How would this change for a multi-step forecast?" (Answer: adjust the target to be a future value, e.g., y is value(t+7)).

A concrete example

To forecast daily user signups, for a given date t, the target y is signups(t). The feature matrix X would include: lag features like signups(t-1) and signups(t-7); window features like mean_signups(t-7 to t-1) and stddev_signups(t-30 to t-1); and calendar features like day_of_week(t) and is_holiday(t). We would train the model on data up to Dec 31, 2023, and test its performance on data from Jan 1, 2024, onwards.

Interview question

When transforming a time series for supervised learning, what is the most critical consideration for model validation?

  • a.Performing standard k-fold cross-validation to get robust performance estimates.
  • b.Using a time-ordered split, training on past data and testing on future data, to avoid data leakage.Correct
  • c.Normalizing all features to a common scale before splitting to improve model convergence.
  • d.Randomly splitting the data into training and testing sets to ensure unbiased evaluation.
Why?

The card emphasizes that using a time-ordered split is "most critically" important to prevent data leakage, where a model might otherwise be trained on future data to predict the past. Standard k-fold or random splits are explicitly identified as major red flags that lead to inflated performance metrics.

Just read this? Test yourself on what you have been reading.

Read the original → scikit-learn.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on time series — each one lists the topics its interview covers.

See open roles