# What Is Feature Engineering?

> What feature engineering is, the main techniques, a worked card-fraud example and the leakage mistake to avoid, explained for your AI interview.

**In short:** Feature engineering is turning raw data, like timestamps, amounts and merchant names, into numbers a model can learn from. Google calls it a vital part of machine learning, and the key rule is to learn its settings from training data only.

AI and LLM interview question Q428, Classic ML and deep-learning basics. Beginner, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-feature-engineering

## Say it in 30 seconds

Feature engineering means turning raw data into the inputs a model learns from. A bank's raw card record has a time, an amount and a merchant type. I turn the time into hour of day, the amount into a multiple of the card's usual spend, and the merchant into numbers. The one rule I hold to: any average I compute comes from training rows only, so the test rows never leak in.

## Say it in 2 minutes

Most classic models need a row of numbers, called a feature vector, and someone decides what goes in that row. That decision is feature engineering. Google's definition is that you work out which features might be useful, then convert the raw data into efficient versions of them. Some people call it feature extraction or featurization.

Take a card fraud model at a mid-size bank. A raw record says 02:47, 1,849 dollars, electronics. I turn the time into hour two, the amount into about forty times the card's usual spend, and the merchant into a one-hot slot. The model then learns the pattern from those numbers.

The common techniques are scaling numbers into a standard range, bucketing them into ranges, one-hot encoding categories, and pulling parts out of a date. Machine learning models need numerical input, so categories must become numbers, and one-hot encoding does that by giving each category its own slot. Linear models in particular benefit from scaling, as scikit-learn notes.

The trap is leakage. Any average or range I compute must come from training rows only. If test rows feed it, the test score looks better than the real performance. So I fit the scaler on training data, then apply that same scaler to the test rows.

## What is feature engineering, in plain words?

Think of a loan officer at a bank. She doesn't read a customer's whole statement line by line. She works out what matters: how much comes in each month, and how often the account goes overdrawn. Those summaries are her features.

A card fraud model at a mid-size bank gets raw records: a timestamp, an amount and a merchant type. Models need numbers, so the team turns each one into a number that shows the pattern. That turning is the feature engineering.

- **Feature:** One input the model learns from, such as hour of day or amount.
- **Feature vector:** The row of numbers the model reads for one example.
- **One-hot encoding:** Turning a category into zeros with a single 1 in its own slot.
- **Leakage:** When information from test rows sneaks into training, so the score looks better than it really is.
- **Training and test rows:** Training rows teach the model. Test rows are held back to check it on data it has not seen.

## What are the main feature engineering techniques?

- **Scaling:** Squeeze numbers into a standard range, so one big number doesn't drown out the rest.
- **Bucketing (binning):** Group numbers into ranges, such as night, morning, afternoon and evening for an hour of day.
- **One-hot encoding:** Give each category its own slot. Electronics becomes 0 0 1 0 across four merchant types.
- **Date and time parts:** Pull out the hour, weekday or month, which can carry a pattern the raw timestamp hides.
- **Ratios:** Divide one number by another, like this amount by the card's usual spend. The ratio often says more than either number alone.

Google's machine learning course puts the job in one line, and scikit-learn, a Python library for machine learning, explains why scaling matters for some models.

> "You must determine the best way to represent raw dataset values as trainable values in the feature vector." (Google)
>
> "many learning algorithms such as linear models benefit from standardization of the data set" (scikit-learn)

## How does it work, step by step?

1. **Pick what might help:** Ask which parts of the raw record could predict the outcome. Ask the people who know the business.
2. **Turn raw values into numbers:** Times become hours, categories become slots, and text becomes counts or codes.
3. **Scale or bucket the numbers:** Put numbers on a similar range. Models usually train faster and predict better that way.
4. **Test that it helped:** Measure on test rows the features never touched. If a feature doesn't move the score, drop it.

*Diagram, From a raw card record to model input: One card record goes through the feature code and comes out as one row of numbers. In this pipeline the model learns from that row.*
1. The raw record goes into your feature code.
2. The code turns each part into a number.
3. The model reads one row of numbers.

## What does a worked example look like?

Here is a small, simplified version of the card fraud step. The made-up numbers describe a card that usually spends about 46 dollars a purchase.

```python
# A simplified card-fraud feature step for a mid-size bank. Made-up numbers.
from datetime import datetime

CATEGORIES = ["grocery", "travel", "electronics", "fuel"]

def features(txn, usual_spend):
    # Turn a raw record into numbers the model can learn from.
    hour = datetime.fromisoformat(txn["time"]).hour
    avg = sum(usual_spend) / len(usual_spend)
    ratio = round(txn["amount"] / avg, 2)  # this amount vs the card's normal spend
    one_hot = [1.0 if txn["category"] == c else 0.0 for c in CATEGORIES]
    return [float(hour), ratio] + one_hot

usual = [42.50, 38.00, 55.20, 47.90]  # the card's last four normal purchases
night_buy = {"time": "2026-03-14T02:47:00", "amount": 1849.00, "category": "electronics"}
day_buy = {"time": "2026-03-14T12:10:00", "amount": 52.10, "category": "grocery"}

print("night purchase:", features(night_buy, usual))
print("day purchase:", features(day_buy, usual))
```

Output:

```text
night purchase: [2.0, 40.28, 0.0, 0.0, 1.0, 0.0]
day purchase: [12.0, 1.14, 1.0, 0.0, 0.0, 0.0]
```

Read the first line of output. The purchase happened at 2 a.m., and it is about forty times the card's usual spend, in electronics. The second line is a 52 dollar grocery purchase at noon. Its ratio stays close to 1, so it looks like the card's normal spending.

## What is the most common mistake?

The most common mistake is leakage. Scikit-learn defines it as "Data leakage occurs when information that would not be available at prediction time is used when building the model." Its scaling example is simple: the average has to come from the training rows.

Scikit-learn warns that "If the test subset is included in the average calculation, information from the test subset is influencing the model."

*Diagram, Where the scaling average comes from: Two ways to scale the same data. The wrong way lets test rows into the average, so the score looks better than it is. The right way learns the average from training rows only.*
1. Test rows get mixed into the average.
2. Only training rows feed the average.
3. The inflated average makes the score look better.
4. The honest score shows how the model does on new cards.

> **Tip:** Asked about leakage? Say that every average, range and scaler is learnt from training rows only, then applied to the test rows. Never fit anything on the test rows.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: turning raw data into numbers a model can learn from.
- [ ] The main techniques: scaling, bucketing, one-hot encoding and ratios.
- [ ] A worked example from a real problem, with the numbers.
- [ ] Leakage: every average and range learnt from training data only.
- [ ] The steps in order: pick the features, turn them into numbers, scale them, then test on rows the model never saw.

**Common traps**

- Trap: "More features always make the model better." Better: Extra features can add noise. Keep the ones that help on test rows.
- Trap: "Scale the whole dataset, then split it." Better: That leaks test rows into the average. Split first, then fit the scaler on training rows only.
- Trap: "Feature engineering is an old step we can skip." Better: Raw bank records are rarely ready for a model as they are. Skipping this step is a choice you must justify with test results.

## Can you spot the leak?

**Spot the leak:** Which step lets test rows leak into the model?

A. scaler = StandardScaler().fit(X_train)
B. scaler = StandardScaler().fit(X_all)  # before the split
C. encoder = OneHotEncoder().fit(X_train)
D. hour = timestamp.hour  # from each row

**Answer:** B. Fitting the scaler on all rows, test rows included, puts test information into the average. Fit on training rows only, then apply that same scaler to the test rows.

**Quick check:** A card fraud model sees a 1,849 dollar purchase. Which feature best shows it is unusual for this card?

A. The raw amount, unchanged, with no reference point
B. The amount as a multiple of the card's usual spend
C. The row number in the transaction file
D. The bank's branch name, spelled out

**Answer:** B. A big number means little on its own. Set against the card's usual spend, it stands out. Row numbers and branch names say nothing about how this card is used.

## Can you answer these from memory?

**Flash cards**

- **What is feature engineering?** Turning raw data into the numbers a model learns from, and choosing which to give it.
- **What is a feature vector?** The row of numbers the model reads for one example.
- **Name three techniques.** Any three of: scaling, bucketing (binning), one-hot encoding, ratios, date and time parts.
- **What is one-hot encoding?** A category gets its own slot: a 1 there, and 0 in every other slot.
- **Why does scaling help?** Models usually train faster and predict better when numeric features share a similar range.
- **What is leakage in this step?** Test rows feed the average or scaler, so the score looks better than the real result.

## Likely follow-up questions

- Q424 What is data leakage? https://designervenkat.online/interview/ai/what-is-data-leakage
- Q423 What is cross-validation? https://designervenkat.online/interview/ai/what-is-cross-validation
- Q425 How do you handle class imbalance? https://designervenkat.online/interview/ai/how-do-you-handle-class-imbalance
- Q430 What is a confusion matrix? https://designervenkat.online/interview/ai/what-is-a-confusion-matrix

## Learn it in depth

- [Feature engineering: turning raw columns into signals](https://designervenkat.online/learn/machine-learning/feature-engineering)
- [Feature scaling and normalization](https://designervenkat.online/learn/machine-learning/feature-scaling-normalization)
