MyMisPal
← Practical AI lessons
Lesson 2 of 11

Data: features, target and splitting

Every model starts with a table. Each row is an example (a customer, an invoice, a month), and the columns come in two kinds: features are what we know, and the target (or label) is what we want to predict.

StoreOrders / monthWeeks since last orderRegionLeft? (target)
Al Waha81RiyadhNo
Al Salam27JeddahYes
Al Noor54DammamNo
Past dataSplittrain / testTraining(fit)Evaluate ontest dataPredict newcasesNot good? Go back

Why split the data? Testing a model on the examples it learned from is like a student who memorised the exam with its answers: full marks, no understanding. So we hold back part of the data (usually 20%) as the test set, learn from the rest (the training set), and then see how the model does on examples it has never seen.

PYTHON
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct"]
cartons = [410, 395, 430, 450, 470, 520, 610, 590, 480, 505]

cut = int(len(cartons) * 0.8)   # 80% for training
train = cartons[:cut]
test = cartons[cut:]
print("Train:", len(train), "months")
print("Test: ", len(test), "months", test)
Watch outWith time-based data (monthly sales, say) keep the latest period for testing, as above, because in real life you predict the future from the past. If rows are independent (customers, say), shuffle them randomly before splitting.

Data matters more than the algorithm. A model learns from what you give it: if the sales data has duplicates, mistyped prices or missing months, it learns the mistakes. The classic rule: garbage in, garbage out. That's why most of an ML project's time goes into preparing and cleaning data, and where your SQL skills pay off.

Exercises

Exercise 1 · Multiple choice

We want to predict “will this invoice be paid late?”. Which column is the target?

Exercise 2 · Calculation

You have 500 customers split 80% training and 20% test. How many customers are in the test set?

customers
Exercise 3 · Multiple choice

A model scores 99% on training data and 60% on test data. What most likely happened?

Exercise 4 · Python · graded by running

sales holds 10 weeks of sales. Split it 70% training and 30% test (latest weeks for testing), print the test data, then print the number of training weeks.

Spotted a mistake on this page, or have an idea to improve the site?