192 KiB
192 KiB
In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('Telco-Customer-Churn.csv')
print(df.shape)
df.head()
df.info()
df['Churn'].value_counts(normalize=True)Out [1]:
(7043, 21) <class 'pandas.DataFrame'> RangeIndex: 7043 entries, 0 to 7042 Data columns (total 21 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 customerID 7043 non-null str 1 gender 7043 non-null str 2 SeniorCitizen 7043 non-null int64 3 Partner 7043 non-null str 4 Dependents 7043 non-null str 5 tenure 7043 non-null int64 6 PhoneService 7043 non-null str 7 MultipleLines 7043 non-null str 8 InternetService 7043 non-null str 9 OnlineSecurity 7043 non-null str 10 OnlineBackup 7043 non-null str 11 DeviceProtection 7043 non-null str 12 TechSupport 7043 non-null str 13 StreamingTV 7043 non-null str 14 StreamingMovies 7043 non-null str 15 Contract 7043 non-null str 16 PaperlessBilling 7043 non-null str 17 PaymentMethod 7043 non-null str 18 MonthlyCharges 7043 non-null float64 19 TotalCharges 7043 non-null str 20 Churn 7043 non-null str dtypes: float64(1), int64(2), str(18) memory usage: 1.1 MB
Churn No 0.73463 Yes 0.26537 Name: proportion, dtype: float64
In [62]:
df['SeniorCitizen'].value_counts(normalize=True)Out [62]:
SeniorCitizen 0 0.837853 1 0.162147 Name: proportion, dtype: float64
In [2]:
sns.countplot(data=df, x="Churn")
sns.countplot(data=df, x="Contract", hue='Churn')
sns.boxplot(data=df, x='Churn', y='tenure')
sns.heatmap(df.select_dtypes('number').corr(), annot=True, cmap='coolwarm')Out [2]:
<Axes: >
In [58]:
df = pd.read_csv('Telco-Customer-Churn.csv')
df["TotalCharges"] = pd.to_numeric(df["TotalCharges"], errors='coerce')
df["MonthlyCharges"] = pd.to_numeric(df["MonthlyCharges"], errors='coerce')
df["TechSupport_num"] = df["TechSupport"].map({'No':0, 'Yes':1})
df["Churn_num"] = df["Churn"].map({'No':0, 'Yes':1})
corr = df[["TotalCharges", "MonthlyCharges", "TechSupport_num", "Churn_num", 'tenure']].corr(method='spearman')
sns.heatmap(corr, annot=True, cmap='coolwarm')Out [58]:
<Axes: >
In [44]:
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
df = pd.read_csv('Telco-Customer-Churn.csv')
df["Churn"] = df["Churn"].map({'No':0, 'Yes':1})
df["TotalCharges"] = pd.to_numeric(df["TotalCharges"], errors='coerce')
df = df.drop(columns=['customerID'])
X = df.drop(columns=['Churn'])
y = df['Churn']
num_cols = X.select_dtypes('number').columns.to_list()
cat_cols = X.select_dtypes('object').columns.to_list()
preprocessor = ColumnTransformer([
('num', Pipeline([
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
]), num_cols),
('cat', Pipeline([
('imputer', SimpleImputer(strategy="most_frequent")),
('ohe', OneHotEncoder(handle_unknown='ignore'))
]), cat_cols)
])
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
/tmp/ipykernel_9201/298299848.py:18: Pandas4Warning: For backward compatibility, 'str' dtypes are included by select_dtypes when 'object' dtype is specified. This behavior is deprecated and will be removed in a future version. Explicitly pass 'str' to `include` to select them, or to `exclude` to remove them and silence this warning.
See https://pandas.pydata.org/docs/user_guide/migration-3-strings.html#string-migration-select-dtypes for details on how to write code that works with pandas 2 and 3.
cat_cols = X.select_dtypes('object').columns.to_list()
In [46]:
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import (classification_report, roc_auc_score, confusion_matrix, RocCurveDisplay)
models = {
'LogReg': LogisticRegression(max_iter=100, class_weight='balanced'),
'RandomForest' : RandomForestClassifier(n_estimators=300, class_weight='balanced', random_state=42),
'GradientBoosting' : GradientBoostingClassifier(random_state=42)
}
results = {}
for name, clf in models.items():
pipe = Pipeline([('prep', preprocessor), ('clf', clf)])
pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_val)
y_proba = pipe.predict_proba(X_val)[:, 1]
results[name] = {
'roc_auc': roc_auc_score(y_val, y_proba),
'report': classification_report(y_val, y_pred)
}
print(f"=== {name} ===")
print(results[name]['report'])
print(f"ROC-AUC : {results[name]['roc_auc']:.3f}\n")=== LogReg ===
precision recall f1-score support
0 0.90 0.72 0.80 1035
1 0.50 0.78 0.61 374
accuracy 0.74 1409
macro avg 0.70 0.75 0.71 1409
weighted avg 0.80 0.74 0.75 1409
ROC-AUC : 0.841
=== RandomForest ===
precision recall f1-score support
0 0.86 0.82 0.84 1035
1 0.55 0.62 0.59 374
accuracy 0.77 1409
macro avg 0.70 0.72 0.71 1409
weighted avg 0.78 0.77 0.77 1409
ROC-AUC : 0.822
=== GradientBoosting ===
precision recall f1-score support
0 0.84 0.91 0.87 1035
1 0.67 0.52 0.59 374
accuracy 0.81 1409
macro avg 0.76 0.72 0.73 1409
weighted avg 0.80 0.81 0.80 1409
ROC-AUC : 0.843
In [48]:
from sklearn.model_selection import GridSearchCV
param_grid = {
'clf__n_estimators': [200, 400],
'clf__max_depth': [5, 10, None],
'clf__min_samples_leaf': [1, 5]
}
grid = GridSearchCV(
Pipeline([('prep', preprocessor), ('clf', RandomForestClassifier(random_state=42, class_weight='balanced'))]),
param_grid, cv=5, scoring='roc_auc', n_jobs=-1
)
grid.fit(X_train, y_train)
print(grid.best_params_, grid.best_score_){'clf__max_depth': 10, 'clf__min_samples_leaf': 5, 'clf__n_estimators': 400} 0.8477837278014084
In [50]:
from sklearn.metrics import ConfusionMatrixDisplay, PrecisionRecallDisplay
best = grid.best_estimator_
y_proba = best.predict_proba(X_val)[:,1]
y_pred = (y_proba >= 0.5).astype(int)
ConfusionMatrixDisplay.from_predictions(y_val, y_pred)
RocCurveDisplay.from_predictions(y_val, y_proba)
PrecisionRecallDisplay.from_predictions(y_val, y_proba)
ohe_cols = best.named_steps['prep'].named_transformers_['cat'].named_steps['ohe'].get_feature_names_out(cat_cols)
feature_names = num_cols + list(ohe_cols)
importances = pd.Series(best.named_steps['clf'].feature_importances_, index=feature_names)
importances.sort_values(ascending=False).head(15).plot(kind='barh')Out [50]:
<Axes: ylabel='Precision (Positive label: 1)'>