Класифікація за допомогою sklearn
Розглянемо, як розв’язувати задачі класифікації з використанням бібліотеки sklearn. На двох практичних прикладах - текстові дані та класи з сильним дисбалансом - розберемо повний підхід до побудови, навчання й коректної оцінки моделей.
Code and results
Published version after Merge. Results were saved by the author; code does not run on this page.
HTML, SVG, віджети та інтерактивні результати не переносяться. Збережено безпечний текст або зображення.
Python · 1
# В даному ноуті виконав пару завдань - два різних датасети
# У першому датасеті дизбаланс даних несуттєвий тому після нормалізації данних
# та застосуванні моделі GradientBoostingClassifier метрики виглядають добре
# на мій недосвідчений погляд :)
import kagglehub
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
classification_report,
f1_score,
precision_score,
recall_score,
roc_auc_score,
precision_recall_curve
)
# Перший датасет - передбачення хвороби серця - дизбаланс несуттєвий
path = kagglehub.dataset_download("fedesoriano/heart-failure-prediction")
df = pd.read_csv(os.path.join(path, "heart.csv"), sep=",")
Python · 2
df.head(3)Results saved by the author in Colab
Age Sex ChestPainType RestingBP Cholesterol FastingBS RestingECG MaxHR \ 0 40 M ATA 140 289 0 Normal 172 1 49 F NAP 160 180 0 Normal 156 2 37 M ATA 130 283 0 ST 98 ExerciseAngina Oldpeak ST_Slope HeartDisease 0 N 0.0 Up 0 1 N 1.0 Flat 1 2 N 0.0 Up 0
Python · 3
df["HeartDisease"].value_counts()Results saved by the author in Colab
HeartDisease 1 508 0 410 Name: count, dtype: int64
Python · 4
TARGET_NAME="HeartDisease"
X = df.drop(columns=[TARGET_NAME])
y=df[TARGET_NAME]
X_train, X_valid, y_train, y_valid = train_test_split(
X, y,
test_size=0.33,
random_state=42
)
Python · 5
# normalization of data
float_cols = X_train.select_dtypes(include="float").columns.tolist()
int_cols = X_train.select_dtypes(include="int").columns.tolist()
cat_cols = X_train.select_dtypes(include="object").columns.tolist()Python · 6
X_train[cat_cols]Results saved by the author in Colab
Sex ChestPainType RestingECG ExerciseAngina ST_Slope 585 M ATA ST N Flat 169 M NAP Normal N Up 602 M NAP Normal Y Flat 140 M ASY Normal Y Flat 173 M NAP Normal N Up .. .. ... ... ... ... 106 F ASY ST N Up 270 M ASY Normal N Up 860 M ASY Normal Y Up 435 M ASY ST Y Up 102 F ASY Normal N Flat [615 rows x 5 columns]
Python · 7
ohe = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
X_train_ohe = ohe.fit_transform(X_train[cat_cols])
X_valid_ohe = ohe.transform(X_valid[cat_cols])
X_train_ohe_df = pd.DataFrame(X_train_ohe, columns=ohe.get_feature_names_out(cat_cols), index=X_train.index)
X_valid_ohe_df = pd.DataFrame(X_valid_ohe, columns=ohe.get_feature_names_out(cat_cols), index=X_valid.index)Python · 8
X_train_ohe_df.head(3)Results saved by the author in Colab
Sex_F Sex_M ChestPainType_ASY ChestPainType_ATA ChestPainType_NAP \
585 0.0 1.0 0.0 1.0 0.0
169 0.0 1.0 0.0 0.0 1.0
602 0.0 1.0 0.0 0.0 1.0
ChestPainType_TA RestingECG_LVH RestingECG_Normal RestingECG_ST \
585 0.0 0.0 0.0 1.0
169 0.0 0.0 1.0 0.0
602 0.0 0.0 1.0 0.0
ExerciseAngina_N ExerciseAngina_Y ST_Slope_Down ST_Slope_Flat \
585 1.0 0.0 0.0 1.0
169 1.0 0.0 0.0 0.0
602 0.0 1.0 0.0 1.0
ST_Slope_Up
585 0.0
169 1.0
602 0.0 Python · 9
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train[float_cols])
X_valid_scaled = scaler.transform(X_valid[float_cols])
X_train_scaled_df = pd.DataFrame(X_train_scaled, columns=float_cols, index=X_train.index)
X_valid_scaled_df = pd.DataFrame(X_valid_scaled, columns=float_cols, index=X_valid.index)
X_train_scaled_df.head(3)Results saved by the author in Colab
Oldpeak 585 -0.106065 169 -0.826835 602 -0.826835
Python · 10
df.describe()Results saved by the author in Colab
Age RestingBP Cholesterol FastingBS MaxHR \
count 918.000000 918.000000 918.000000 918.000000 918.000000
mean 53.510893 132.396514 198.799564 0.233115 136.809368
std 9.432617 18.514154 109.384145 0.423046 25.460334
min 28.000000 0.000000 0.000000 0.000000 60.000000
25% 47.000000 120.000000 173.250000 0.000000 120.000000
50% 54.000000 130.000000 223.000000 0.000000 138.000000
75% 60.000000 140.000000 267.000000 0.000000 156.000000
max 77.000000 200.000000 603.000000 1.000000 202.000000
Oldpeak HeartDisease
count 918.000000 918.000000
mean 0.887364 0.553377
std 1.066570 0.497414
min -2.600000 0.000000
25% 0.000000 0.000000
50% 0.600000 1.000000
75% 1.500000 1.000000
max 6.200000 1.000000 Python · 11
X_train_df_final = pd.concat([X_train_ohe_df, X_train_scaled_df, X_train[int_cols]], axis=1)
X_valid_df_final = pd.concat([X_valid_ohe_df, X_valid_scaled_df, X_valid[int_cols]], axis=1)Python · 12
X_train_df_final.head(2)Results saved by the author in Colab
Sex_F Sex_M ChestPainType_ASY ChestPainType_ATA ChestPainType_NAP \
585 0.0 1.0 0.0 1.0 0.0
169 0.0 1.0 0.0 0.0 1.0
ChestPainType_TA RestingECG_LVH RestingECG_Normal RestingECG_ST \
585 0.0 0.0 0.0 1.0
169 0.0 0.0 1.0 0.0
ExerciseAngina_N ExerciseAngina_Y ST_Slope_Down ST_Slope_Flat \
585 1.0 0.0 0.0 1.0
169 1.0 0.0 0.0 0.0
ST_Slope_Up Oldpeak Age RestingBP Cholesterol FastingBS MaxHR
585 0.0 -0.106065 57 180 285 1 120
169 1.0 -0.826835 58 140 179 0 160 Python · 13
print(X_train.shape[0] == X_train_df_final.shape[0])Results saved by the author in Colab
True
Python · 14
print(X_train_df_final.isna().sum())Results saved by the author in Colab
Sex_F 0 Sex_M 0 ChestPainType_ASY 0 ChestPainType_ATA 0 ChestPainType_NAP 0 ChestPainType_TA 0 RestingECG_LVH 0 RestingECG_Normal 0 RestingECG_ST 0 ExerciseAngina_N 0 ExerciseAngina_Y 0 ST_Slope_Down 0 ST_Slope_Flat 0 ST_Slope_Up 0 Oldpeak 0 Age 0 RestingBP 0 Cholesterol 0 FastingBS 0 MaxHR 0 dtype: int64
Python · 15
gb_model = GradientBoostingClassifier(
n_estimators=1000
)
gb_model.fit(X_train_df_final, y_train)
y_valid_pred = gb_model.predict(X_valid_df_final)Python · 16
print(classification_report(y_valid, y_valid_pred))Results saved by the author in Colab
precision recall f1-score support
0 0.78 0.89 0.83 123
1 0.92 0.83 0.87 180
accuracy 0.85 303
macro avg 0.85 0.86 0.85 303
weighted avg 0.86 0.85 0.86 303
Python · 17
# Другий датасет - передбачення виживання пасажирів Титаніку. Дані більш дизбалансовані ніж в попередній задачі.
# Було використано підходи з уроку - нормалізація даних - застосування кількох моделей та файнтюнинг моделі
# з підбором таргетованого реколу
import kagglehub
path = kagglehub.dataset_download("heptapod/titanic")
df = pd.read_csv(os.path.join(path, "train_and_test2.csv"), sep=",")Results saved by the author in Colab
Using Colab cache for faster access to the 'titanic' dataset.
Python · 18
df.head(3)Results saved by the author in Colab
Passengerid Age Fare Sex sibsp zero zero.1 zero.2 zero.3 \ 0 1 22.0 7.2500 0 1 0 0 0 0 1 2 38.0 71.2833 1 1 0 0 0 0 2 3 26.0 7.9250 1 0 0 0 0 0 zero.4 ... zero.12 zero.13 zero.14 Pclass zero.15 zero.16 Embarked \ 0 0 ... 0 0 0 3 0 0 2.0 1 0 ... 0 0 0 1 0 0 0.0 2 0 ... 0 0 0 3 0 0 2.0 zero.17 zero.18 2urvived 0 0 0 0 1 0 0 1 2 0 0 1 [3 rows x 28 columns]
Python · 19
df.isna().sum()Results saved by the author in Colab
Passengerid 0 Age 0 Fare 0 Sex 0 sibsp 0 zero 0 zero.1 0 zero.2 0 zero.3 0 zero.4 0 zero.5 0 zero.6 0 Parch 0 zero.7 0 zero.8 0 zero.9 0 zero.10 0 zero.11 0 zero.12 0 zero.13 0 zero.14 0 Pclass 0 zero.15 0 zero.16 0 Embarked 2 zero.17 0 zero.18 0 2urvived 0 dtype: int64
Python · 20
df.dropna(subset="Embarked", inplace=True)
Python · 21
plt.bar(counts.index[0].astype(str), counts.values[0], label="Клас 0")
plt.bar(counts.index[1].astype(str), counts.values[1], label="Class 1")
plt.show()
Results saved by the author in Colab
Python · 22
df.describe()Results saved by the author in Colab
Passengerid Age Fare Sex sibsp \
count 1307.000000 1307.000000 1307.000000 1307.000000 1307.000000
mean 655.319816 29.471821 33.209595 0.355011 0.499617
std 377.922205 12.881592 51.748768 0.478700 1.042273
min 1.000000 0.170000 0.000000 0.000000 0.000000
25% 328.500000 22.000000 7.895800 0.000000 0.000000
50% 655.000000 28.000000 14.454200 0.000000 0.000000
75% 982.500000 35.000000 31.275000 1.000000 1.000000
max 1309.000000 80.000000 512.329200 1.000000 8.000000
zero zero.1 zero.2 zero.3 zero.4 ... zero.12 zero.13 zero.14 \
count 1307.0 1307.0 1307.0 1307.0 1307.0 ... 1307.0 1307.0 1307.0
mean 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0
std 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0
min 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0
25% 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0
50% 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0
75% 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0
max 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0
Pclass zero.15 zero.16 Embarked zero.17 zero.18 \
count 1307.000000 1307.0 1307.0 1307.000000 1307.0 1307.0
mean 2.296863 0.0 0.0 1.492731 0.0 0.0
std 0.836942 0.0 0.0 0.814626 0.0 0.0
min 1.000000 0.0 0.0 0.000000 0.0 0.0
25% 2.000000 0.0 0.0 1.000000 0.0 0.0
50% 3.000000 0.0 0.0 2.000000 0.0 0.0
75% 3.000000 0.0 0.0 2.000000 0.0 0.0
max 3.000000 0.0 0.0 2.000000 0.0 0.0
2urvived
count 1307.000000
mean 0.260138
std 0.438877
min 0.000000
25% 0.000000
50% 0.000000
75% 1.000000
max 1.000000
[8 rows x 28 columns]Python · 23
df.drop(list(df.filter(regex='zero')), axis=1, inplace=True)
df.drop(columns=["Passengerid"], inplace=True)
df.rename(columns={"2urvived": "Survived"}, inplace=True)Python · 24
df.describe()Results saved by the author in Colab
Age Fare Sex sibsp Parch \
count 1309.000000 1309.000000 1309.000000 1309.000000 1309.000000
mean 29.503186 33.281086 0.355997 0.498854 0.385027
std 12.905241 51.741500 0.478997 1.041658 0.865560
min 0.170000 0.000000 0.000000 0.000000 0.000000
25% 22.000000 7.895800 0.000000 0.000000 0.000000
50% 28.000000 14.454200 0.000000 0.000000 0.000000
75% 35.000000 31.275000 1.000000 1.000000 0.000000
max 80.000000 512.329200 1.000000 8.000000 9.000000
Pclass Embarked Survived
count 1309.000000 1307.000000 1309.000000
mean 2.294882 1.492731 0.261268
std 0.837836 0.814626 0.439494
min 1.000000 0.000000 0.000000
25% 2.000000 1.000000 0.000000
50% 3.000000 2.000000 0.000000
75% 3.000000 2.000000 1.000000
max 3.000000 2.000000 1.000000 Python · 25
TARGET_NAME="Survived"
X = df.drop(columns=[TARGET_NAME])
y = df[TARGET_NAME]
X_train, X_valid, y_train, y_valid = train_test_split(
X, y,
test_size=0.33,
shuffle=True,
random_state=42
)Python · 26
float_cols = X_train.select_dtypes(include="float").columns.tolist()
int_cols = X_train.select_dtypes(include="int").columns.tolist()
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train[float_cols])
X_valid_scaled = scaler.transform(X_valid[float_cols])
X_train_scaled_df = pd.DataFrame(X_train_scaled, columns=float_cols, index=X_train.index)
X_valid_scaled_df = pd.DataFrame(X_valid_scaled, columns=float_cols, index=X_valid.index)Python · 27
int_colsResults saved by the author in Colab
['Sex', 'sibsp', 'Parch', 'Pclass']
Python · 28
X_train_scaled_df.head(3)Results saved by the author in Colab
Age Fare Embarked 377 -0.228783 3.285404 -1.881209 1169 0.009351 -0.243911 0.612818 495 -0.149405 -0.365106 -1.881209
Python · 29
X_train_df_final = pd.concat([X_train[int_cols], X_train_scaled_df], axis=1)
X_valid_df_final = pd.concat([X_valid[int_cols], X_valid_scaled_df], axis=1)Python · 30
X_train_df_final.head(3)
X_train_df_final.isna().sum()Results saved by the author in Colab
Sex 0 sibsp 0 Parch 0 Pclass 0 Age 0 Fare 0 Embarked 0 dtype: int64
Python · 31
log_model = LogisticRegression()
log_model.fit(X_train_df_final, y_train)
y_valid_pred = log_model.predict(X_valid_df_final)
print(classification_report(y_valid, y_valid_pred))Results saved by the author in Colab
precision recall f1-score support
0 0.79 0.94 0.86 308
1 0.72 0.37 0.49 124
accuracy 0.78 432
macro avg 0.75 0.66 0.67 432
weighted avg 0.77 0.78 0.75 432
Python · 32
gb_model = GradientBoostingClassifier(
n_estimators=1000
)
gb_model.fit(X_train_df_final, y_train)
y_valid_pred = gb_model.predict(X_valid_df_final)
print(classification_report(y_valid, y_valid_pred))Results saved by the author in Colab
precision recall f1-score support
0 0.80 0.86 0.83 308
1 0.57 0.46 0.51 124
accuracy 0.75 432
macro avg 0.68 0.66 0.67 432
weighted avg 0.73 0.75 0.74 432
Python · 33
from imblearn.over_sampling import SMOTE
smote = SMOTE(random_state=42)
X_train_smote, y_train_smote = smote.fit_resample(
X_train_df_final, y_train
)
y_train_smote.value_counts()Results saved by the author in Colab
Survived 0 659 1 659 Name: count, dtype: int64
Python · 34
import lightgbm as lgbm
lgbm_model = lgbm.LGBMClassifier(num_leaves = 3,
n_estimators = 500,
min_child_samples = 20,
max_depth = 15,
learning_rate = 0.1,
random_state=42,
verbose=-1
)
lgbm_model.fit(X_train_smote, y_train_smote)
y_valid_pred = lgbm_model.predict(X_valid_df_final)
print(classification_report(y_valid, y_valid_pred))Results saved by the author in Colab
precision recall f1-score support
0 0.85 0.81 0.83 308
1 0.57 0.65 0.61 124
accuracy 0.76 432
macro avg 0.71 0.73 0.72 432
weighted avg 0.77 0.76 0.76 432
Python · 35
y_valid_pred_proba = lgbm_model.predict_proba(X_valid_df_final)[:, 1]
precision, recall, threshold = precision_recall_curve(y_valid, y_valid_pred_proba)
f1_score = (2 * precision * recall) / (precision + recall)
idx = np.argmax(f1_score)
idxResults saved by the author in Colab
np.int64(182)
Python · 36
y_valid_pred = [1 if el >= threshold[idx] else 0 for el in y_valid_pred_proba]
print(classification_report(y_valid, y_valid_pred))
Results saved by the author in Colab
precision recall f1-score support
0 0.91 0.67 0.77 308
1 0.50 0.84 0.63 124
accuracy 0.72 432
macro avg 0.71 0.75 0.70 432
weighted avg 0.79 0.72 0.73 432
Python · 37
target_recall = 0.9
idxs = np.where(recall[:-1] >= target_recall)[0]
if len(idxs) == 0:
print(f"Recall {target_recall} недосяжний для даної моделі/даних")
else:
i = idxs[-1] # самий високий поріг при recall>=target
thr = threshold[i]
y_pred = (y_valid_pred_proba >= thr).astype(int)
print("thr:", thr)
print(classification_report(y_valid, y_pred))Results saved by the author in Colab
thr: 0.27045589755621346
precision recall f1-score support
0 0.94 0.57 0.71 308
1 0.46 0.90 0.61 124
accuracy 0.66 432
macro avg 0.70 0.74 0.66 432
weighted avg 0.80 0.66 0.68 432
Community reviews
Відмінна робота. Використання оверсемплінгу окремо заслуговує відзнаки. Можна мерджати
