Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 40 ยท Getting Started

Your First Model

Build your first scikit-learn model on the iris dataset: split the data, fit a classifier, predict new flowers, score the result and inspect mistakes.

In this lesson you build a complete model from start to finish. The task: given four measurements of an iris flower, predict its species. The recipe is the same one you will use all course: get data, split it, create a model, fit it, predict, score.

Step 1: load the data

scikit-learn ships small datasets for learning. load_iris returns the measurements X (the features) and the species y (the target or label).

Python Look at the data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

By convention X is a 2-D array with one row per sample and one column per feature, and y is a 1-D array with one answer per sample.

Step 2: hold some data back

If you test a model on the same flowers it learned from, a score tells you how well it memorised, not how well it generalises. So split the data first. The model sees the training set; the test set is kept hidden until the end.

Python Train and test split

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

random_state=42 makes the shuffle repeatable. stratify=y keeps the class proportions equal in both parts. More on splitting in Train, Validation and Test Sets.

Step 3: create, fit, predict, score

We use a decision tree classifier. Creating the object chooses the algorithm and its settings; fit is where learning happens.

Python The whole workflow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For classifiers, score returns accuracy: the fraction of test samples predicted correctly. For regressors it returns R-squared instead (see Linear Regression).

Predicting a brand-new flower

predict needs a 2-D array, even for one sample. A common beginner error is passing a flat list.

Python Predict a new sample

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

predict_proba shows how confident the model is in each class.

Looking at the mistakes

A single accuracy number hides where the model fails. A confusion matrix counts, for each true class (rows), how many were predicted as each class (columns).

Python Confusion matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Numbers on the diagonal are correct predictions; anything off the diagonal is a mistake. Evaluation gets its own chapter later in the course.

Try a different algorithm

Because of the shared interface, swapping the algorithm is a one-line change:

Python
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=5)   # everything else stays identical

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why do we keep a test set that the model never sees during fit?

    Scoring on training data rewards memorisation. A held-out test set estimates real-world performance.
  2. 2. What shape must the input to predict have?

    predict always expects a 2-D array. A single sample is a list inside a list.
  3. 3. In the line X_train, X_test, y_train, y_test = train_test_split(...), what does y hold?

    X are the features and y is the target the model learns to predict.
  4. 4. Which call starts the learning?

    fit is where the algorithm learns from the training data.
  5. 5. What does score return for a classifier?

    The default classifier score is accuracy, the fraction of correct predictions.

Summary

  • The workflow: load data, split, create, fit, predict, score.
  • X is a 2-D feature array, y a 1-D label array.
  • Never evaluate only on the training data.
  • predict_proba gives class probabilities and a confusion matrix shows which classes get mixed up.
  • Swapping algorithms usually changes one line.

Next: Datasets and Data Formats