Đạ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

Capstone Practice: Re-run the Entire Workflow on Your Own Data

Print / Save as PDF

Capstone Practice: Re-run the Entire Workflow on Your Own Data

This lesson repeats exactly the workflow you just watched — load the data, separate x and y, split into train/test, standardize, train, predict, evaluate — but on a different dataset that you choose. Do it after completing units 3.P1 to 3.P6.

The task is in the Assignment section below. This is direct preparation for the Group Project.

Study notes

By the end of this lesson you can:

  • Repeat the six practical steps (3.P1 to 3.P6) on a dataset you choose
  • Adapt the code to a new file, a new target column and a different number of rows
  • Check each step with shapes, class counts and fixed random seeds
  • Interpret the final report and confusion matrix in the context of your own problem

Key idea: The diabetes notebook is a reusable template: load, split x and y, split train/test, standardise, train, predict and evaluate; only the file, the target column and your interpretation change.

What this practice asks you to do

This lesson repeats the workflow from the diabetes lab, but on a different dataset that you choose. The task is in the Assignment section, and it is direct preparation for the Group Project. Choose a dataset with a clear target column; a binary classification problem (two classes) is the easiest to compare with the lab.

The six steps and what to change

StepDiabetes labWhat you change
3.P1 Load and split x, ypd.read_csv("diabetes.csv"), target = "Outcome"the file name and the target column
3.P2 Train/test splittest_size=0.2, random_state=1009usually nothing; consider stratify=y if classes are imbalanced
3.P3 Reproducibilityfixed random_statekeep it fixed for every run
3.P4 Standardisefit_transform on train, transform on testnothing; all features must be numeric first
3.P5 Train and predictRandomForestClassifier(random_state=100)optionally another classifier
3.P6 Evaluateclassification_report, confusion_matrixyour interpretation of the errors

Checks to run at every step

  • After loading: data.shape, data.head(), data.isna().sum(). Missing values or text columns must be handled before StandardScaler, which needs numbers.
  • After splitting x and y: x.shape, y.value_counts() (class balance).
  • After train_test_split: the train and test row counts add up to the total.
  • After predicting: len(y_pred) == len(y_test).
  • In the report: support per class, per-class recall and precision, macro and weighted averages.

Interpreting your results

Write a short paragraph answering the same questions as in 3.P6:

  1. Which class is the positive class in your problem, and which error (false positive or false negative) is more costly?
  2. Does the overall accuracy hide a weak class? Compare per-class recall.
  3. Is the model good enough for its real use? If not, what would you try next (another algorithm, tuning, more data)?

Worked example

A complete run of the template on the breast cancer dataset that ships with scikit-learn, so it works without downloading a file. Replace the first two lines with pd.read_csv("your_file.csv") and your own target name for the assignment.

import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix

data = load_breast_cancer(as_frame=True).frame       # 569 rows, 30 features + target
target = "target"
x = data.drop(target, axis=1)
y = data[target]
print(x.shape, y.value_counts().to_dict())

x_train, x_test, y_train, y_test = train_test_split(
    x, y, test_size=0.2, random_state=1009)
print(x_train.shape, x_test.shape)

scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)

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

print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))

What to expect, step by step:

  1. x.shape is (569, 30); the class counts are 212 for label 0 and 357 for label 1.
  2. With test_size=0.2, the test set has 114 rows (569 x 0.2 = 113.8, rounded up) and the training set 455.
  3. The report and the 2x2 confusion matrix appear; exact values depend on the split.
  4. Interpretation: in this dataset label 0 means malignant and label 1 benign. Read the class 0 row: a missed malignant tumour (a true 0 predicted as 1) is the costly error, so class 0 recall is the number to discuss, not just accuracy.

Common mistakes

  • Copying the code without changing the target name. data.drop("Outcome", axis=1) fails on a dataset without that column.
  • Feeding text columns to StandardScaler. Encode categorical columns (and fill missing values) first.
  • Not checking what each label means. Always confirm which value is the positive class before interpreting recall.
  • Reporting only accuracy. Discuss per-class recall and the confusion matrix, as in 3.P6.

Summary

  • Reuse the six-step lab workflow on a new dataset: load, x/y, split, standardise, train and predict, evaluate.
  • Change the file, the target column and, if needed, add stratification and preprocessing for text or missing values.
  • Check shapes, class counts and fixed seeds at every step.
  • Interpret results by asking which error is costly and whether accuracy hides a weak class.
  • This exercise prepares the workflow you will use in the Group Project.

Flashcards · Capstone Practice: Re-run the Entire Workflow on Your Own Data

7 words
WordMeaningExample
  • Workflow template

    The reusable sequence load, split x/y, train/test split, standardise, train, predict, evaluate.

    The diabetes notebook applied to a new CSV.

  • Target column

    The column you want to predict; it must be changed when you switch datasets.

    target = 'Outcome' -> target = 'target'

  • data.isna().sum()

    Counts missing values in each column; check it before scaling.

    Glucose 0, Insulin 5 ...

  • Numeric features requirement

    StandardScaler and most models need numeric inputs, so text columns must be encoded first.

    pd.get_dummies(data, columns=['city'])

  • Label meaning

    What each class value represents; needed to choose the positive class and interpret recall.

    Breast cancer data: 0 = malignant, 1 = benign.

  • load_breast_cancer(as_frame=True)

    Loads a built-in scikit-learn dataset as a pandas DataFrame for practice.

    data = load_breast_cancer(as_frame=True).frame

  • Sanity checks

    Quick checks of shapes, counts and lengths after each step to catch mistakes early.

    len(y_pred) == len(y_test)