Preparing data
- Machine Learning course
- Intermediate
- Lesson 31/112
- 7 min
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% missingThe 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.
Practice 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.
Starter code
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
# buraya yazSample 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))Expected output
[476 124] 232 [119 31]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why isn't accuracy the main metric in this project?
- Even a model saying "stays" for everyone gets 80% Correct answer
- It can't be computed
- Because of missing values
- Because it is regression
Explanation: With imbalanced classes accuracy rewards the majority class.
2 Count the missing values per column.
print(np.isnan(X).sum(____))
print(np.isnan(X).sum(axis=0))
Explanation: axis=0 sums along the rows: one number per column.
3 Match the difficulties with the fixes.
| Term | Meaning |
|---|---|
Missing value |
SimpleImputer |
Imbalanced class |
stratify and F1 |
Different scales |
StandardScaler |
Setting selection |
GridSearchCV |