0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Why raw data can't go straight into a model
So far, our data has been tidy columns of numbers. Here's what real Brightcart data looks like. Payment methods written three different ways. Ratings missing. Times of day. Order values from twenty dollars to two thousand. And free text, typed in a hurry by an annoyed customer. A model can only multiply and add numbers. It can't multiply the word "card". And here's a model that looks spectacular: ninety-nine point eight percent accurate. It's completely useless, because of one column that should never have been there.
Turning raw data into good numerical inputs is called feature engineering. Experienced practitioners will tell you it often matters more than which algorithm you pick. Let's learn how to do it, and how not to.
1:00Map flight: feature engineering
Season One, Module Two, final stop: feature engineering.
1:05Categories: one-hot and ordinal encoding
Let's start with categories. Brightcart customers pay by card, wallet or cash on delivery. The tempting shortcut is to number them: card is one, wallet is two, cash is three. But now the model thinks cash is "bigger" than card, and that wallet sits exactly halfway between them. We've invented an order and distances that don't exist. The standard fix is one-hot encoding. We replace the column with one yes-or-no column per category: is it card, is it wallet, is it cash. Each row has a single one, the "hot" one, and zeros everywhere else.
One subtlety for linear models. Those three columns always add up to one, which duplicates the intercept. So we usually drop one of them, and it becomes the reference category the others are compared against. Some categories genuinely do have an order. Customer tiers go bronze, silver, gold. Here, numbering them zero, one, two is called ordinal encoding, and it's fine, because the order is real, especially for tree models. For linear models, it does assume equal steps between the tiers. And when a category has thousands of values, like postcodes, one-hot creates thousands of columns.
For those, there are better tools, like learned embeddings, which we'll meet in Season Two.
2:32Missing values
Real data has holes. Some Brightcart customers never leave a rating. We could drop every row with a blank, but here that would throw away two of our seven customers, and with real data it can wipe out a large share of it. A common alternative is imputation: filling the blank with a sensible value. For numbers, the median is a popular choice because extreme values don't drag it around. The known ratings sorted are three, four, four, five, five, so the median is four, and both blanks become four. But here's a subtle point.
The fact that a rating is missing can itself be a signal. Maybe the customers who don't bother to rate are the unhappy ones. So we often add a separate yes-or-no feature: "was this missing?" Then the model can learn whether missingness matters. And just like with scaling, the median has to be computed on the training data only.
3:33Dates and cyclical features
Dates and times are gold for a shop. A raw timestamp means little to a model, but we can break it into pieces that do: day of the week, whether it's a weekend, the hour, the month, how many days since the customer signed up. The hour hides a neat problem. If we store it as a number from zero to twenty-three, then eleven p.m. is twenty-three and midnight is zero. As far as the model can tell, they're as far apart as possible. In reality, they're neighbours. The fix is to wrap the hour around a circle, like a clock face, and describe each hour by two numbers: its sine and its cosine.
Now eleven p.m. sits at minus zero point two six and zero point nine seven, and midnight sits at zero and one, only about a quarter of a unit apart. Noon is on the opposite side of the clock, at a distance of two. The geometry finally matches reality. This cyclical trick works for anything that loops: days of the week, months of the year, even compass directions.
4:42New features: ratios, interactions and logs
Feature engineering isn't only about cleaning. Often the biggest wins come from creating new features. A ratio can reveal what a raw number hides. Total spend and number of orders, on their own, mix up big customers with frequent ones. Divide them, and you get the average basket: forty-seven dollars. An interaction combines two features, like distance multiplied by rain. That lets even a simple linear model learn something like "rain slows long trips more than short ones", if the data says so. Skewed values, like order amounts where most are small but a few are huge, can be tamed with a logarithm.
Twenty dollars to two thousand dollars becomes one point three to three point three, a much more even spread that many models find easier to learn from. And text can be turned into numbers too. The simplest way is a bag of words: count how often each word appears. "Arrived late, box crushed" becomes a vector of word counts. It ignores word order, which is a big limitation, and in Season Two we'll replace it with something far more powerful.
5:55Domain knowledge beats brute force
The best features usually come from understanding the problem, not from the algorithm. Dev knows things no dataset spells out. Deliveries slow down during festival weeks. Apartment blocks with no lift take longer. Rain changes everything. So before you reach for a fancier model, talk to the people who actually do the work. One good feature from someone who knows the business can beat weeks of tuning.
6:26Leakage: the feature that's too good to be true
Now, that suspicious model. Brightcart wanted to predict, at the moment a parcel leaves the warehouse, whether it'll arrive late. Someone included a column called "refund issued". Look at the timeline. A parcel arrives late, the customer complains, and then a refund is issued. The refund is a consequence of the lateness. Of course it predicts lateness almost perfectly: it's practically the answer, smuggled in from the future. In real use, at the moment of dispatch, no refund has happened yet, so that column is always empty, and the model's accuracy falls apart.
This is called target leakage. Leakage comes in a few flavours. Features that only exist after the outcome. Preprocessing, like scaling or imputing, done on all the data before splitting. Duplicate records that end up in both training and test sets. And, for anything over time, random shuffling that lets the model train on the future and test on the past. For every single feature, ask one question: would I actually know this value at the moment I make the prediction? If the answer is no, it doesn't belong in the model.
And whenever results look too good to be true, suspect leakage first.
7:44Try it yourself: encode one order
Let's encode a single order, step by step. Paid by wallet, a silver-tier customer, placed at six in the evening, worth two hundred dollars, and no rating yet. Pause and try each one. Payment by wallet becomes the one-hot pattern zero, one, zero, and dropping cash as the reference leaves zero, one. Silver tier, which has a real order, becomes one. Six p.m. is three-quarters of the way around the clock, so its sine is minus one and its cosine is zero. Two hundred dollars, through a base-ten log, becomes about two point three.
The missing rating is filled with the training median, four, plus a flag saying it was missing. Put together, a messy order becomes a clean vector of eight numbers, ready for any model. That's feature engineering in one picture.
8:45Weighing words: TF-IDF
When we turned text into word counts, every word counted equally. But "the" appears in almost every review, while "cracked" appears in only a few, and "cracked" tells you far more. A classic fix is called TF-IDF: multiply how often a word appears in a review by how rare it is across all reviews. With a hundred reviews, "cracked" appears in two of them. Its rarity score is the natural log of a hundred over two: about three point nine. "The" appears in all hundred: the log of one, which is zero, so it contributes nothing.
Rare, telling words get boosted; common, empty words fade away. Remember this idea: in Season Five, the same principle powers the keyword search inside Brightcart's AI assistant.
9:39Choosing which features to keep
With hundreds of possible features, which should you keep? There are three broad approaches. Filter methods score each feature on its own, say by its correlation with the target. They're fast, but they miss features that only matter in combination. Wrapper methods try different subsets and test each with cross-validation, for example adding the single most helpful feature, one at a time. They're accurate, but slow. Embedded methods let the model choose as it trains, like lasso setting weights to zero, or trees reporting which features they used most.
One trap: if you select features using all your data, including the validation folds, you leak information and inflate your scores. Selection must happen inside each cross-validation fold.
10:30Target encoding, carefully
For categories with thousands of values, like delivery postcodes, a popular trick is target encoding: replace each postcode with the average outcome for that postcode, say its rate of late deliveries. It packs a lot of information into one number. But it's a leakage trap. If a postcode has only one order, its "average lateness" is just that order's own answer. The model sees the answer hidden inside the feature. The fixes: compute each row's encoding using only other folds of the data, and smooth rare categories towards the overall average, so a postcode with two orders isn't treated as certain.
Used carefully, it's powerful; used carelessly, it produces a beautiful model that fails in real use.
11:22Train-serve skew
One last real-world problem: train–serve skew. In training, someone computes "days since last order" from a nightly snapshot. In the live system, a different piece of code computes it at the moment of the request, perhaps in a different time zone. The same customer gets slightly different feature values, and the model's predictions quietly drift from what you tested. The cure is to compute features with exactly the same code in training and in production. Many teams use a shared feature service, often called a feature store, and they log live feature values to compare with the training data.
We'll return to monitoring like this in the final season.
12:06Lags and rolling-window features
For anything that unfolds over time, some of the strongest features come from the past. Yesterday's sales. Sales on the same weekday last week. The average over the previous seven days, a rolling window that slides forward one day at a time. How many days until a known holiday. For customers: orders in the last thirty or ninety days, whether their baskets are growing or shrinking, how long since their last complaint. These features carry a built-in leakage risk. The window must end before the moment you're predicting.
A seven-day average that accidentally includes today's sales will make a forecasting model look brilliant in testing, and useless in real life, because tomorrow's sales are exactly what it doesn't know yet. Always ask the leakage question from earlier: would I know this value at prediction time?
13:02Myth vs Fact: does deep learning replace features?
Myth: deep learning has made feature engineering unnecessary. Partly true for images, sound and text, where neural networks learn useful features by themselves, as we'll see in Season Two. But for table-shaped business data, like Brightcart's orders and customers, carefully engineered features with tree-based models are still among the strongest approaches. And no model, however deep, can fix leakage or badly encoded categories for you.
13:33Recap
Let's recap. Categories become one-hot columns, while genuinely ordered categories can be numbered. Missing values can be imputed, often with a flag that records they were missing. Repeating quantities like hours are wrapped onto a circle with sine and cosine. New features, like ratios, interactions and logarithms, often bring the biggest gains. And every feature must pass the leakage test: would I know this at prediction time? One sentence to remember: good features make simple models smart, and leaked features make smart models useless.
14:11Next: linear regression from scratch
Mission complete. Brightcart's messy order table is now clean, numerical model food, with the leak plugged. And that completes Module Two: how models learn. You know how training works, how to test honestly, how to stop overfitting, and how to prepare data. Now we're ready to build complete models from scratch, starting with the most widely used one in all of statistics: linear regression. That's next. See you on the map.