← Courses

Intro to Machine Learning

AI / ML

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 y by convention. Here that is the exam score.
  • The columns you feed into the model are the features, called X by convention. You choose them, often by intuition at first.
Python

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:

  1. Define: choose the model type and its settings
  2. Fit: learn the patterns from the training data, the heart of modelling
  3. Predict: estimate the target for some rows
  4. Evaluate: measure how accurate those predictions are (the next lesson)
Python

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

Exercise — check your understanding

Define a DecisionTreeRegressor called model with random_state=1. The line that fits it on X and y is already written for you.

You’re reading for free. Sign in to keep your progress and earn a certificate when you finish.Sign in to keep my progress →