Building a Model · Lesson 3 of 7
Your first model
Choosing what to model
A real dataset can have many columns, too many to hold in your head at once. So the first job is to decide what to predict and what to predict it from.
- See every column with
students.columns. - The column you want to predict is the prediction target, called
yby convention. Here that is the exam score. - The columns you feed into the model are the features, called
Xby convention. You choose them, often by intuition at first.
A single column like y is a Series, which is just a DataFrame with one column. Looking at X.head() (and X.describe()) before you model is part of the job, because you often spot surprises worth checking.
A note on missing data: real datasets have gaps. A quick option is students.dropna(), which drops rows with missing values. We cover handling missing data properly in a later lesson, so do not worry about it for now.
Define, fit, predict
You build models with scikit-learn (written as sklearn in code), the most popular library for modelling table data. Every model follows four steps:
- Define: choose the model type and its settings
- Fit: learn the patterns from the training data, the heart of modelling
- Predict: estimate the target for some rows
- Evaluate: measure how accurate those predictions are (the next lesson)
random_state=1 fixes the model's internal randomness so you get the same result every run. The exact number does not matter, but setting one is good practice.
Notice the model predicts the training students almost perfectly. That is expected, and the next lesson shows why it is also misleading.
Exercise
Define a DecisionTreeRegressor called model with random_state=1. The line that fits it on X and y is already written for you.