Machine Learning · Chapter 1 of 26
Learning from examples, and your first model
By the end of this chapter you can say what it means for a model to learn from data, write a rule by hand and score it on past loans, split a table into features and a label, make a scikit-learn model, fit it and ask it for predictions, and measure it honestly on loans it has never seen. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Learning from data
- 4 tasks
- Python
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
Dobara buys used scooters and motorbikes across Madhya Pradesh, refurbishes them in its workshop in Indore, and sells them from six branches. Most buyers pay in monthly EMIs, and Dobara lends the money itself. Between July 2024 and June 2025 it gave 2,400 loans, ₹12.26 crore in all. 339 of them defaulted (the buyer stopped paying for 90 days or more), and ₹1.94 crore had been lent to those 339 buyers. Some of it comes back when Raju, who runs the workshop and goes out when an EMI bounces, brings the bike back. Much of it does not.
Farida, who runs finance and credit, decides every application with a rule she wrote herself from years of experience: a loan whose EMI is a large part of the buyer's income, or a buyer who already pays several other EMIs, looks risky. Rachna, the founder, wants to know whether Dobara could decide better, and Tanvi, Dobara's staff data scientist, says the answer is in the 2,400 loans Dobara already has: each one is an example of a buyer, what was known about them on the day they applied, and what happened next.
With pandas you can check one rule against those loans. But a rule has numbers in it, and someone has to choose them: an income limit of ₹12,000 or ₹16,000? Two other EMIs or three? Income first, or EMIs first? There are thousands of combinations, and trying them by hand is guessing. Machine learning is a program that tries them for you, against the examples, and keeps the ones that work. You build Dobara's first model with Tanvi in this chapter.
The lesson
Dobara's past loans
The loans are in /workspace/data/loans.csv, one row a loan:
import pandas as pd
loans = pd.read_csv("/workspace/data/loans.csv")
print(loans.shape)
print(list(loans.columns))
cols = ["loan_id", "monthly_income_rs", "emi_rs", "existing_emis", "defaulted"]
print(loans[cols].head())
print(loans["defaulted"].value_counts())
(2400, 20)
['loan_id', 'applied_on', 'branch', 'age', 'occupation', 'monthly_income_rs', 'bureau_score', 'existing_emis', 'segment', 'bike_price_rs', 'down_payment_rs', 'loan_rs', 'tenure_months', 'emi_rs', 'years_at_address', 'has_guarantor', 'channel', 'dealer', 'collection_calls', 'defaulted']
loan_id monthly_income_rs emi_rs existing_emis defaulted
0 L00001 14500 4539 0 0
1 L00002 23000 8704 2 0
2 L00003 23500 5733 0 0
3 L00004 15000 3657 0 0
4 L00005 24000 3061 1 0
defaulted
0 2061
1 339
Name: count, dtype: int64
defaulted is 1 for a loan that defaulted and 0 for one that was repaid. existing_emis counts the other EMIs the buyer was already paying, and emi_rs is this loan's EMI in rupees a month.
A rule written by hand
A rule takes what you know about a loan and gives a guess: 1 for "will default", 0 for "will be repaid". Here is a simpler rule than Farida's, to see how any rule is checked: guess that everyone earning under ₹12,000 a month defaults.
low_income = loans["monthly_income_rs"] < 12000
guess = low_income.astype(int).to_numpy()
print(guess[:8])
print(loans["defaulted"].to_numpy()[:8])
print((guess == loans["defaulted"].to_numpy()).mean())
[0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 1 0]
0.7875
The comparison gives True where the guess matches what really happened, and the mean of True and False is the share of True: this rule is right for 78.75 % of the past loans. That share is called accuracy. .to_numpy() turns the column into a NumPy array, which is what scikit-learn's models give back too, so a hand rule and a model can be compared the same way.
The rule's number, 12,000, was a guess. Is 15,000 better? Should the other EMIs count? Each question is another rule to try.
Learning the rule instead
A model is a rule with blanks in it, plus a way to fill the blanks from examples. The examples are split into two parts:
- The features, called X: the facts known on the day a buyer applies, one column each. X is a table, two-dimensional: a row a loan.
- The label, called y: what happened later, the thing to guess. y is one column: a value a loan.
Here X is two columns, the income and the other EMIs, and the model is a decision tree from scikit-learn, Python's machine learning library (imported as sklearn):
from sklearn.tree import DecisionTreeClassifier, export_text
X = loans[["monthly_income_rs", "existing_emis"]]
y = loans["defaulted"]
print(X.shape, y.shape)
model = DecisionTreeClassifier(max_depth=2, random_state=0)
print(model)
model.fit(X, y)
print(export_text(model, feature_names=list(X.columns)))
(2400, 2) (2400,)
DecisionTreeClassifier(max_depth=2, random_state=0)
|--- monthly_income_rs <= 15750.00
| |--- existing_emis <= 1.50
| | |--- class: 0
| |--- existing_emis > 1.50
| | |--- class: 1
|--- monthly_income_rs > 15750.00
| |--- monthly_income_rs <= 22250.00
| | |--- class: 0
| |--- monthly_income_rs > 22250.00
| | |--- class: 0
export_text prints the rule the tree learned. Nobody gave it 15,750 or 1.5. Inside fit, the tree tried every column and every place to cut it, and kept the one question that best separated the loans that defaulted from the ones that did not. Then it did the same again inside each half, twice in all, because max_depth=2 allowed two questions. What it found reads like a rule Farida could have written: an income of ₹15,750 or less with two or more other EMIs looks like a default. That is what learning from data means: the rule's numbers come from the examples, not from someone's guess.
A model that answers with a class, such as 0 or 1, is a classifier: DecisionTreeClassifier. One that answers with a number, such as the price a used bike will sell for, is a regressor (chapter 11). Both learn from examples with a label; chapter 21 meets learning without one.
Make, fit, predict
Every model in scikit-learn, called an estimator, works the same three steps:
- Make it, with its settings:
DecisionTreeClassifier(max_depth=2, random_state=0). Settings you choose are hyperparameters.random_statefixes the model's tie-breaking, so it learns the same tree every run. - Fit it:
model.fit(X, y)learns from the examples. What it learned is kept on the model in attributes that end with an underscore. - Predict:
model.predict(new_X)guesses the label for rows it is given, from their features alone.
print(model.classes_, model.n_features_in_)
print(model.feature_names_in_)
applicants = pd.DataFrame({
"monthly_income_rs": [12500, 31000, 15000],
"existing_emis": [2, 0, 1],
})
print(model.predict(applicants))
[0 1] 2
['monthly_income_rs' 'existing_emis']
[1 0 0]
classes_ holds the labels it saw, and feature_names_in_ the columns it learned from. predict gives a NumPy array, one guess a row: the first applicant, on ₹12,500 with two other EMIs, gets a 1.
The mistakes everyone makes first, with the last line of each error:
- Predicting before fitting:
NotFittedError: This DecisionTreeClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator. - One column given as a Series,
loans["monthly_income_rs"], instead of a table:ValueError: Expected a 2-dimensional container but got <class 'pandas.Series'> instead.X is always two-dimensional, so one feature isloans[["monthly_income_rs"]], with two brackets. - A column of text among the features:
ValueError: could not convert string to float: 'gig worker'. Models learn from numbers; chapter 8 turns text into numbers. - New rows with the columns in another order:
ValueError: The feature names should match those that were passed during fit.
Grading your own homework
score gives the model's accuracy on the rows you give it, exactly what you worked out by hand for the rule:
print(model.score(X, y))
print((model.predict(X) == y).mean())
0.8591666666666666
0.8591666666666666
But there is a problem with this number. The tree chose its questions by looking at these very loans, so scoring it on them is like setting an exam from the answer sheet you studied. What Rachna needs to know is how it will do on next month's applicants, whom no model has seen.
Keeping loans back: train_test_split
So you hold some of the past loans back. The model learns from the training rows only, and is scored on the test rows, which stand in for applicants it has never seen. train_test_split shuffles the rows and cuts them in two:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=1
)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
print(X_test.head(3))
model = DecisionTreeClassifier(max_depth=2, random_state=0)
model.fit(X_train, y_train)
print(model.score(X_train, y_train))
print(model.score(X_test, y_test))
(1800, 2) (600, 2) (1800,) (600,)
monthly_income_rs existing_emis
1348 26000 1
468 11500 0
1463 20500 0
0.8672222222222222
0.835
- It gives back four things, always in this order: X_train, X_test, y_train, y_test. Each row of X keeps its label in y.
test_size=0.25holds back a quarter: 600 of the 2,400.- The rows are shuffled first (the test rows start with 1348, 468, 1463), so the test rows are a fair mix of the year.
random_state=1fixes the shuffle: the same number gives the same split every run, and another number another split.
On the training rows the tree is right 86.7 % of the time; on loans it never saw, 83.5 %. The second number is the one to report.
A tree that remembers
With no max_depth, a tree keeps asking questions until every leaf holds loans of one kind:
X = loans[["monthly_income_rs", "emi_rs", "age"]]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=1
)
deep = DecisionTreeClassifier(random_state=0)
deep.fit(X_train, y_train)
print(deep.get_depth(), deep.get_n_leaves())
print(deep.score(X_train, y_train))
print(deep.score(X_test, y_test))
23 347
1.0
0.7633333333333333
23 questions deep and 347 leaves for 1,800 loans: a leaf for every handful of buyers. It is right about every loan it learned from, 1.0, because it has memorised them, including the ones that defaulted for reasons no column shows. On new loans it does worse than the small tree. A model is useful only for what it does on rows it has not seen; chapter 6 is about finding the right size between remembering and learning.
And is 0.835 good? It sounds good. Chapter 3 compares it with the simplest guess there is, and the answer will surprise you.
Your turn
Four tasks, one file each. Task 1 writes Farida's rule by hand and scores it on the past loans: mind the words "more than" and "or more". Task 2 splits the loans into X and y by column name. Task 3 is your first model: split with the task's test_size and random_state, fit a tree of depth 3 on the training rows only, and score it on the test rows; the Check fits its own copy on the same rows and compares. Task 4 is an interview question: Raju's tree claims to be right every time, and Tanvi will ask why, and which number you would give Rachna. Try each function in ipython first (from first_model import train_and_score).
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
hand_rule.py: Farida's rule, written by hand
Farida guesses that a loan will default when its EMI is more than 40 % of the buyer's monthly income (emi_rs more than 0.4 times monthly_income_rs), or when the buyer already pays 3 or more other EMIs (existing_emis 3 or more). Write two functions in hand_rule.py. Each is given loans, a DataFrame with the columns of loans.csv as pd.read_csv reads it, and must not change it.
farida_rule(loans) returns a NumPy array of whole numbers, one per loan in the table's order: 1 where the rule guesses the loan will default, 0 where it guesses it will be repaid. A loan with emi_rs 4001 and monthly_income_rs 10000 gets 1; an EMI of exactly 40 % (4000 on 10000) gets 0, and exactly 3 other EMIs gets 1.
rule_accuracy(loans) returns the share of loans whose guess equals their defaulted value, as one float from 0 to 1, not a percent.
The Check tries all 2,400 past loans, the last 300 of them, and 5 loans of its own.
- Task 2
features.py: features and a label
Write features_and_label(loans) in features.py. loans is a DataFrame with the columns of loans.csv; do not change it. Return two values, X and y, separated by a comma:
X is a DataFrame of exactly these five columns, in this order: monthly_income_rs, emi_rs, existing_emis, down_payment_rs, age, with every row and the rows' own labels.
y is the column defaulted as a Series, with its name, defaulted, kept.
The label must not be one of the features. The Check also gives a table of 5 loans whose columns are in another order, so pick the columns by name.
- Task 3
first_model.py: your first model
Write train_and_score(X, y) in first_model.py. X is a DataFrame of features and y the label, one value for each row of X. Split the rows with train_test_split, test_size=0.25 and random_state=7. Make a DecisionTreeClassifier with max_depth=3 and random_state=0, fit it on the training rows only, and score it on the test rows.
Return two values separated by a comma: the fitted model, and its score on the test rows (accuracy, a float from 0 to 1).
The Check fits a fresh copy of your model on the same training rows and compares the two, so the model must never see the test rows. It tries the loans' five features from Task 2, three other columns of 200 made-up loans, and a table of 40 rows.
- Task 4 · interview question
Interview: memorise.py, a perfect score
Raju trained a tree with no depth limit on all the loans, scored it on the same loans, and told Rachna it is right every time. His train_and_test_scores(X, y) returns the same perfect number twice. Fix it so that it splits the rows with train_test_split, test_size=0.25 and random_state=7, fits the same DecisionTreeClassifier(random_state=0), with no depth limit, on the training rows only, and scores it on the training rows and on the test rows.
Return two floats from 0 to 1, separated by a comma: the score on the training rows first, then the score on the test rows. The Check tries the loans' five features from Task 2 and three other columns of 200 made-up loans.
Tanvi will ask: why does the tree score 1.0 on the loans it learned from, and which of the two numbers do you give Rachna before Dobara lends by it?
What you will learn
- Rules by hand and rules learned: what a model is, and what it means for it to learn from examples.
- Features and a label: X, the facts known when a buyer applies, and y, what happened later.
- The estimator: make a scikit-learn model, fit it on examples, and predict new ones.
- train_test_split: keep some loans back that the model never sees.
- score: the share of guesses that were right, and why it only means something on rows the model has not seen.
More in Learning from data
Build "Learning from examples, and your first model" yourself
Dobara refurbishes used bikes in Indore and lends to its buyers. You build the models that decide whom to lend to and at what price to sell. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.