Machine Learning
Preparing data
Prepare and scale data for SVM.
Project: churn prediction
In this project we'll predict which customers of a subscription service will leave. The data is synthetic but carries three difficulties of real projects: 8 features, 5% missing values and imbalanced classes (about 20% of customers leave).
X, y = make_classification(n_samples=600, n_features=8, n_informative=4,
weights=[0.8, 0.2], random_state=7)
X[rng.random(X.shape) < 0.05] = np.nan # 5% missing
The goal: catch as many of the leaving customers (class 1) as possible. That's why the main metric will be F1, not accuracy.
Getting to know the data
The first inspection: class distribution, missing count per column and a stratified split. We'll fill the missing values inside the pipeline as a preprocessing step:
print(np.bincount(y)) # class distribution
print(np.isnan(X).sum(axis=0)) # missing per column
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y)
stratify=y makes the minority class appear in the test set in the same proportion.
Exercise Count classes and gaps
Print the class distribution with np.bincount and the total number of missing values with np.isnan(X).sum(). Then split with test_size=0.25, random_state=0, stratify=y and print the class distribution in the test set.
Show example solution
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=600, n_features=8, n_informative=4, weights=[0.8, 0.2], random_state=7)
rng = np.random.default_rng(7)
X[rng.random(X.shape) < 0.05] = np.nan
print(np.bincount(y))
print(np.isnan(X).sum())
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
print(np.bincount(y_test))
Test yourself
-
Why isn't accuracy the main metric in this project?
- Even a model saying "stays" for everyone gets 80%
- It can't be computed
- Because of missing values
- Because it is regression
Show answer
Even a model saying "stays" for everyone gets 80% — With imbalanced classes accuracy rewards the majority class.
-
Count the missing values per column.
print(np.isnan(X).sum(____))
Show answer
print(np.isnan(X).sum(axis=0))
-
Match the difficulties with the fixes.
- Missing value
- SimpleImputer
- Imbalanced class
- stratify and F1
- Different scales
- StandardScaler
- Setting selection
- GridSearchCV
Next lesson: Training model