You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
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.
By the end of this lesson you can:
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.
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.
| Step | Diabetes lab | What you change |
|---|---|---|
| 3.P1 Load and split x, y | pd.read_csv("diabetes.csv"), target = "Outcome" | the file name and the target column |
| 3.P2 Train/test split | test_size=0.2, random_state=1009 | usually nothing; consider stratify=y if classes are imbalanced |
| 3.P3 Reproducibility | fixed random_state | keep it fixed for every run |
| 3.P4 Standardise | fit_transform on train, transform on test | nothing; all features must be numeric first |
| 3.P5 Train and predict | RandomForestClassifier(random_state=100) | optionally another classifier |
| 3.P6 Evaluate | classification_report, confusion_matrix | your interpretation of the errors |
data.shape, data.head(), data.isna().sum(). Missing values or text columns must be handled before StandardScaler, which needs numbers.x.shape, y.value_counts() (class balance).train_test_split: the train and test row counts add up to the total.len(y_pred) == len(y_test).Write a short paragraph answering the same questions as in 3.P6:
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:
x.shape is (569, 30); the class counts are 212 for label 0 and 357 for label 1.test_size=0.2, the test set has 114 rows (569 x 0.2 = 113.8, rounded up) and the training set 455.data.drop("Outcome", axis=1) fails on a dataset without that column.Flashcards · Capstone Practice: Re-run the Entire Workflow on Your Own Data
7 wordsThe reusable sequence load, split x/y, train/test split, standardise, train, predict, evaluate.
The column you want to predict; it must be changed when you switch datasets.
Counts missing values in each column; check it before scaling.
StandardScaler and most models need numeric inputs, so text columns must be encoded first.
What each class value represents; needed to choose the positive class and interpret recall.
Loads a built-in scikit-learn dataset as a pandas DataFrame for practice.
Quick checks of shapes, counts and lengths after each step to catch mistakes early.