You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
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.
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)
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.By the end of this lesson you can:
RandomForestClassifier and train it with fitpredict and read themfit, test features for predict)classification_report by comparing y_test with y_predKey idea: Every scikit-learn model follows the same three steps: create the object,
fitit on the training data, thenpredicton new data; evaluation compares those predictions with the true test labels.
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.
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.
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.
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.
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:
len(y_pred) prints 154: one prediction per test patient.y_pred[:10] prints ten 0/1 labels (the exact values depend on the data and split).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.
fit must receive x_train, y_train, never x_test, y_test.classification_report. The ground truth comes first, the predictions second; swapping them exchanges precision and recall.predict to return probabilities. It returns labels; use predict_proba for probabilities.random_state in the model. Without it, a Random Forest can give slightly different results on each run.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.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 wordsscikit-learn's Random Forest for classification problems.
Trains a model on features and their true labels.
Returns one predicted label for each row of the input features.
The true labels that predictions are compared against; also called label or target.
The array of labels the model predicts for the test set.
Builds precision, recall, F1 and support per class from true and predicted labels.
Makes classification_report return a dictionary instead of text, so it can be shown as a table.
The standard sequence for every scikit-learn model.