04 · Features and representations
Choosing features
10 min read
A feature is one piece of information the model is given about the moment it is predicting. The amount of a payment is a feature, and so is how many times this card was tried in the last five minutes. Choosing features is deciding what the model is allowed to know, where each value comes from, and whether it can be fetched in time. The design decision it changes is which data sources the serving system has to reach within its time budget.
An everyday example
A librarian recommending a book to a regular visitor on a Friday afternoon draws on five kinds of knowledge without thinking about it. She knows the book, its subject and its length. She knows the visitor, roughly their age and that they read in English. She remembers what they borrowed before and whether they finished it, she knows their book group has been reading Scandinavian crime, and she knows it is the Friday before a long weekend, when people want something thick.
Those five kinds of knowledge are the five sources of features in almost every ML system. They are the item, the person, the person’s past behaviour, the people and things they are connected to, and the context of the request. Organising features by source stops the two common failures, which are forgetting a whole source and reciting fifty features with no reason attached to any of them.
The librarian can ask a question and get an answer in two seconds, and that is where the analogy breaks, because the model gets no conversation and must have every value fetched from stored records inside its time budget, which for a fraud check at checkout is about 100 milliseconds. The technical sections use the payment fraud system from the data topics, where the model has that long to decide whether to allow a card payment.
Five places a feature can come from
Item features describe the thing being decided about, which for a payment is the amount, the currency, the merchant and the type of goods. They are available in the request itself and cost nothing to fetch. They are also the easiest for a fraudster to choose, so they are necessary and never enough. The librarian’s equivalent is knowing the book without knowing the reader.
Actor features describe who is acting, such as how old the account is, whether the email address is at a disposable provider, and how many cards are on file. Behaviour features describe what the actor has done over time, and the clearest example is the number of payment attempts on this card in the last five minutes, which must be computed from recorded events and kept fresh to the minute. These two sources are where most of the predictive power in fraud systems lives, and they are also where the leakage questions from the previous topic have to be asked.
Network features describe connections, such as how many other accounts have used this device or this card. Context features describe the moment, such as the hour of the day, whether the shipping country matches the card’s country, and which device type made the request. Two or three features per source, each with the sentence that says why it should matter, is a complete answer in a design discussion, and the table below is that answer for the payment example.
| Source | Payment fraud examples | Why it should help |
|---|---|---|
| Item | Amount, merchant category | Fraud clusters in certain goods and amounts. |
| Actor | Account age, cards on file | New accounts with several cards are a known pattern. |
| Behaviour | Attempts in the last five minutes | Rapid retries suggest someone testing stolen cards. |
| Network | Accounts sharing this device | One device behind dozens of accounts is a ring. |
| Context | Hour, shipping and card country match | Mismatches raise the risk, more so at night. |
Define how each feature is computed
A feature definition is a short written contract that says whose information it describes, how it is calculated, over what time window, and what to do when the value is missing. For ‘attempts in the last five minutes’ the contract has to say whether failed attempts count, whether the current attempt is included, and whether the window ends at the request time or at the last run of the job that computes it. Each of those choices changes the number, and training and serving have to make the same choice or the model is reading a different quantity from the one it learned.
A missing value is a value too, and it is not zero. A customer with no purchase history and a customer with zero purchases this week look identical if both become zero, yet the first is a stranger and the second is a regular having a quiet week. The librarian would never confuse a new visitor with a regular who skipped a month. Store a default plus a flag that says the value was missing, so the model can tell them apart. Check too whether missingness differs across groups of users, since a feature that is missing for all customers in one country is a country feature in disguise.
Choose representations that fit the model
A representation is the form a value takes when it reaches the model. Amounts spanning 1 to 10,000 dollars are usually squashed with a logarithm so that a 10 dollar difference at the low end is not drowned out by the high end. Categories such as the merchant are turned into numbers either by a lookup table of known values, by hashing, a fixed recipe that turns the text into one of a set number of numbered slots, or by a learned list of numbers, which the embeddings topic covers. Tree-based models, which sort examples with a series of yes-or-no questions, cope with raw numbers, while neural networks usually need the scaling, so the representation choice follows the model choice.
Every category list has a first day for a new value, such as a merchant that signed up this morning. The representation has to say what happens then, whether the value maps to an ‘unknown’ bucket, is hashed like any other, or triggers a default. A model that has never seen the value will otherwise receive something arbitrary, and the failure will not show up in any log.
Measure the value of a dependency
Every feature that is fetched from somewhere has a cost in time and reliability. Suppose a lookup to an outside reputation service about the card’s history lifts recall, the share of frauds the model catches, by three points but takes 30 milliseconds of a 100 millisecond budget and times out one request in two hundred. The design options are to compute the slowly changing part of that reputation overnight and store it locally, or to use a stored copy that is allowed to be up to an hour old. A third option is to train a second model without the feature and serve it whenever the lookup fails.
An ablation is the measurement that justifies the cost, and it means training and evaluating the model with and without a feature or a group of features and recording the difference. Do it by group and by user segment, because related features stand in for each other, so a single feature’s importance score understates a group’s value and overstates one member’s. A feature that helps on new accounts and does nothing on old ones is a feature worth keeping and worth knowing about.
Features also need a review for sensitive information and unequal treatment. Removing an explicit attribute such as nationality does not remove it from the model if postcode and language carry the same information, so the check is on outcomes across groups rather than on the feature list. With the features chosen and justified, the next topic is how to keep each definition identical in the two separate programs that compute it, one for training and one for serving.
Where people get confused
More features are believed to mean a better model. Each feature is a dependency that has to be computed in two places, kept fresh, and defended against leakage, and a feature that adds nothing still adds all of that. A design with twelve justified features is stronger than one with eighty.
Listing features without sources or reasons is what interviewers hear as a feature dump. Naming the five sources and giving two or three per source with a sentence each shows that you know where data comes from and what it costs to fetch.
Missing values get treated as zero and never looked at again. Missingness is a pattern of its own, it differs by segment, and it is one of the first things to break when a system that feeds the data changes.
In the interview
Inside the ten-minute data and features block, about five minutes belong to features, and the way to open is with the sources. For example, ‘I would take features from the item, the actor, their behaviour, their network and the context. For this payment that is the amount and merchant, account age, attempts on the card in the last five minutes, accounts sharing the device, and the hour and country match’. Then stop and let the interviewer pick the one they want to probe, which is usually freshness or leakage.
Interview questions
A high-value feature becomes unavailable for 10 percent of traffic. What do you do?
First I would find out why it is missing and who is affected, because 10 percent that is all one country or one app version is a different problem from 10 percent at random. If the model was trained only on rows where the feature was present, filling in zero will give it an input it has never seen and the scores on that slice become unpredictable.
I would serve the fallback agreed in advance, which is either a default the model was trained to expect, with a flag saying the value is missing, or a second model trained without the feature. I would evaluate the degraded 10 percent as its own group, monitor the missing rate and the feature’s age, and compare whether a stored copy or a simpler local feature gives a better trade between quality and the 100 millisecond budget.
Which features would you use for a payment fraud model, and why those?
I would go source by source. From the item, the amount and the merchant category, because fraud clusters in certain goods and amounts. From the actor, account age and the number of cards on file, because a new account with several cards is a known pattern. From behaviour, attempts on this card in the last five minutes, because rapid retries look like someone testing stolen cards.
From the network, how many accounts share this device, because one device behind dozens of accounts is a ring, and from the context, the hour and whether the shipping country matches the card’s country. That is about ten features, and the behaviour and network ones are the expensive half, since they need counts kept fresh and fetched within the budget, so I would want each of them to earn its place in an ablation.
How do you decide whether an expensive feature is worth keeping?
I would run an ablation, training and evaluating with and without it, and put the gain next to the cost. If a reputation lookup adds three points of recall but takes 30 of my 100 milliseconds and times out one request in two hundred, the question is whether three points of recall is worth a third of the budget and a fallback path.
Usually the answer is to keep the information and lose the dependency, by computing the slow-moving part overnight or keeping a stored copy for an hour, and then re-measure, because a day-old reputation is usually nearly as good. I would also check the gain by segment, since a feature that only helps new accounts might be better served by a separate model for that slice.
What to practice next
Design these systems on the canvas and get your design graded.
These problems have you turn a category into a number using the label, encode an hour of day so that 23 and 0 sit next to each other, and build a value from an earlier time step, which are three of the representations this topic describes.