Đại học Kinh tế Quốc dân – National Economics UniversityMICRO4ASIA
🏆 Arena
Sign inSign up
MICRO4ASIAThe six micro-credentials

Courses are delivered on the Limio learning platform. Limio

← Fundamental of Machine Learning

You are viewing a public preview

Sign in to save your progress, do assignments, join discussions and get feedback.

Sign in
Module 3: Model Evaluation and Optimization

3.P5 Training a Random Forest and Predicting

Print / Save as PDF

3.P5 Training a Random Forest and Predicting

After this unit you will: train a scikit-learn model, predict on the test set, and know what you need in order to evaluate it.

Key points

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(random_state=100)
model.fit(x_train, y_train)      # learn from the training set
y_pred = model.predict(x_test)   # predict for the test set

report = classification_report(y_test, y_pred, output_dict=True)
  • The workflow for every scikit-learn model: create the object → fit → predict.
  • fit only receives the training data (x_train, y_train); the model must never see y_test.
  • y_pred holds one predicted label for each row of x_test (154 rows).
  • classification_report(y_test, y_pred) compares the true labels y_test with the predicted labels y_pred.

Study notes

By the end of this lesson you can:

  • Create a RandomForestClassifier and train it with fit
  • Produce predictions for the test set with predict and read them
  • Explain which data each step may use (training data for fit, test features for predict)
  • Prepare a classification_report by comparing y_test with y_pred

Key idea: Every scikit-learn model follows the same three steps: create the object, fit it on the training data, then predict on new data; evaluation compares those predictions with the true test labels.

Choosing and creating the model

With the data loaded, split and standardised, the next step is to choose a classification algorithm. The lecture uses a Random Forest, which in scikit-learn is RandomForestClassifier.

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(random_state=100)

Creating the object does not train anything yet. random_state=100 fixes the randomness inside the forest (the bootstrap samples and random feature choices), so training gives the same model on every run.

Training with fit

model.fit(x_train, y_train)

model.fit starts the training. The features are x_train and the ground truth (also called the label or target) is y_train. Only training data goes into fit: the model must never see y_test while learning.

What happens inside fit connects back to unit 2.5. The forest draws many bootstrap samples from the 614 training rows (sampling with replacement), grows one decision tree on each sample, and lets each tree consider a random subset of the 8 features at every split. By default scikit-learn grows 100 trees (n_estimators=100). When you later call predict, every tree votes 0 or 1 for each patient and the majority vote becomes the forest's prediction.

Predicting with predict

y_pred = model.predict(x_test)

predict takes the test features and returns one predicted label for each row. With 154 rows in x_test, y_pred contains 154 values. Printing y_pred shows a sequence of 0s and 1s: 0 for a patient predicted not to have diabetes, 1 for a patient predicted to have diabetes.

Preparing the evaluation

To know how good the model is, compare the predictions with the ground truth, y_test. You could print accuracy, precision, recall and F1 one by one, but the lecture introduces two more comprehensive tools:

  • classification_report: a whole set of metrics in one table;
  • confusion_matrix: the raw counts of correct and incorrect predictions.
from sklearn.metrics import classification_report

report = classification_report(y_test, y_pred, output_dict=True)

The two arguments are, in order, the ground truth (y_test) and the predictions (y_pred). output_dict=True returns the report as a Python dictionary instead of a text block, so it can be displayed as a neat table. Reading the report is the topic of the next lesson.

Worked example

The full training and prediction cell, with checks.

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report

model = RandomForestClassifier(random_state=100)
model.fit(x_train, y_train)

y_pred = model.predict(x_test)
print(len(y_pred))
print(y_pred[:10])

report = classification_report(y_test, y_pred, output_dict=True)
print(pd.DataFrame(report).transpose().round(2))

Expected output, step by step:

  1. len(y_pred) prints 154: one prediction per test patient.
  2. y_pred[:10] prints ten 0/1 labels (the exact values depend on the data and split).
  3. pd.DataFrame(report).transpose() turns the dictionary into a table with one row per class (0, 1) plus accuracy, macro avg and weighted avg, and the columns precision, recall, f1-score and support.

The order of the steps matters: create, fit, predict, then report. Because random_state is fixed both in the split (1009) and in the forest (100), you get the same numbers every time you rerun the notebook.

Common mistakes

  • Training on the test set. fit must receive x_train, y_train, never x_test, y_test.
  • Swapping the arguments of classification_report. The ground truth comes first, the predictions second; swapping them exchanges precision and recall.
  • Expecting predict to return probabilities. It returns labels; use predict_proba for probabilities.
  • Forgetting random_state in the model. Without it, a Random Forest can give slightly different results on each run.

Summary

  • scikit-learn workflow: create the model, fit on training data, predict on test features.
  • RandomForestClassifier(random_state=100) gives a reproducible forest.
  • y_pred holds one predicted label (0 or 1) for each of the 154 test rows.
  • Evaluation compares y_test (truth) with y_pred (predictions).
  • classification_report(y_test, y_pred, output_dict=True) returns the metrics as a dictionary for display.

Flashcards · 3.P5 Training a Random Forest and Predicting

8 words
WordMeaningExample
  • RandomForestClassifier

    scikit-learn's Random Forest for classification problems.

    model = RandomForestClassifier(random_state=100)

  • fit()

    Trains a model on features and their true labels.

    model.fit(x_train, y_train)

  • predict()

    Returns one predicted label for each row of the input features.

    y_pred = model.predict(x_test)

  • Ground truth

    The true labels that predictions are compared against; also called label or target.

    y_test

  • y_pred

    The array of labels the model predicts for the test set.

    154 values of 0 or 1.

  • classification_report()

    Builds precision, recall, F1 and support per class from true and predicted labels.

    classification_report(y_test, y_pred)

  • output_dict=True

    Makes classification_report return a dictionary instead of text, so it can be shown as a table.

    pd.DataFrame(report).transpose()

  • Create-fit-predict pattern

    The standard sequence for every scikit-learn model.

    model = ...; model.fit(...); model.predict(...)