Все разделы
Jupyter

Jupyter

JupyterLab в учебном маршруте

churn_features.ipynb
Python 3.11 (lesson)

Lesson 4 · Churn features

Проверьте признаки перед обучением модели. Датасет уже лежит в рабочей папке урока.

In [1]
import pandas as pd
import matplotlib.pyplot as plt

customers = pd.read_csv('customers.csv')
customers[['tenure_months', 'tickets_30d', 'nps', 'churned']].head()
   tenure_months  tickets_30d   nps  churned
0              4            3   6.0        1
1             18            0   9.0        0
2              9            2   7.0        0
3             31            1   8.0        0
4              2            5   3.0        1
5 rows × 4 columns
In [2]
features = customers.assign(
    ticket_rate=customers['tickets_30d']
    / customers['tenure_months'].clip(lower=1)
)
features[['tenure_months', 'ticket_rate', 'nps', 'churned']].corr().round(3)
               tenure_m  ticket_rate      nps   churned
tenure_m         1.000       -0.412   0.186    -0.341
ticket_rate     -0.412        1.000  -0.274     0.503
nps              0.186       -0.274   1.000    -0.298
churned         -0.341        0.503  -0.298     1.000
4 × 4
In [3]
tenure_bin = pd.cut(
    customers['tenure_months'],
    bins=[0, 6, 12, 24, 120],
    labels=['0–6m', '6–12m', '12–24m', '24m+'],
)
rate = customers.groupby(tenure_bin, observed=True)['churned'].mean() * 100
rate.plot(kind='bar', color='#0ea5e9', rot=0)
plt.ylabel('churn rate, %')
plt.title('Churn rate by tenure')
plt.tight_layout()
28.4
0–6m
19.1
6–12m
11.6
12–24m
6.2
24m+
<Figure size 640x360 with 1 Axes>
Mode: CommandPython 3.11 | IdleMock · no remote kernel
Notebook execution complete