-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchurn_prediction.py
More file actions
224 lines (180 loc) · 8.31 KB
/
Copy pathchurn_prediction.py
File metadata and controls
224 lines (180 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# ==============================================================================
# Section 1: Import Necessary Libraries
# ==============================================================================
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
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.metrics import classification_report, confusion_matrix, roc_auc_score
import xgboost as xgb
print("Libraries imported successfully.")
# ==============================================================================
# Section 2: Load and Initially Inspect the Data
# ==============================================================================
# Load the dataset
try:
df = pd.read_csv('WA_Fn-UseC_-Telco-Customer-Churn.csv')
print("Dataset loaded successfully.")
print("\nFirst 5 rows of the dataset:")
print(df.head())
except FileNotFoundError:
print("Error: 'WA_Fn-UseC_-Telco-Customer-Churn.csv' not found.")
print("Please download the dataset from Kaggle and place it in the same directory as this script.")
exit()
# Initial data inspection
print("\nDataset Info:")
df.info()
# The 'TotalCharges' column is object type, needs to be converted to numeric.
# Some values are empty strings ' ' which will cause an error. We handle this.
df['TotalCharges'] = pd.to_numeric(df['TotalCharges'], errors='coerce')
# Check for missing values after coercion
print("\nMissing values in each column:")
print(df.isnull().sum())
# We will handle the missing 'TotalCharges' by imputing with the median.
df['TotalCharges'].fillna(df['TotalCharges'].median(), inplace=True)
print("\nMissing 'TotalCharges' filled with median.")
# Drop the customerID column as it's not a useful feature for prediction
df.drop('customerID', axis=1, inplace=True)
print("\n'customerID' column dropped.")
# Convert target variable 'Churn' to binary (0/1)
df['Churn'] = df['Churn'].apply(lambda x: 1 if x == 'Yes' else 0)
print("\nTarget variable 'Churn' converted to 0/1.")
# ==============================================================================
# Section 3: Exploratory Data Analysis (EDA)
# ==============================================================================
print("\nStarting Exploratory Data Analysis... Plots will be saved as PNG files.")
# Set plot style
sns.set_style("whitegrid")
# 1. Churn Distribution
plt.figure(figsize=(6, 4))
sns.countplot(x='Churn', data=df)
plt.title('Churn Distribution (0 = No Churn, 1 = Churn)')
plt.savefig('eda_churn_distribution.png')
plt.close()
print("Saved 'eda_churn_distribution.png'")
# 2. Churn by Contract Type
plt.figure(figsize=(10, 6))
sns.countplot(x='Contract', hue='Churn', data=df)
plt.title('Churn by Contract Type')
plt.savefig('eda_churn_by_contract.png')
plt.close()
print("Saved 'eda_churn_by_contract.png'")
# 3. Churn by Internet Service
plt.figure(figsize=(10, 6))
sns.countplot(x='InternetService', hue='Churn', data=df)
plt.title('Churn by Internet Service Type')
plt.savefig('eda_churn_by_internet_service.png')
plt.close()
print("Saved 'eda_churn_by_internet_service.png'")
# 4. Tenure distribution for Churn vs. No Churn
plt.figure(figsize=(12, 6))
sns.histplot(data=df, x='tenure', hue='Churn', multiple='stack', bins=30, kde=True)
plt.title('Tenure Distribution by Churn Status')
plt.savefig('eda_tenure_distribution.png')
plt.close()
print("Saved 'eda_tenure_distribution.png'")
print("\nEDA finished.")
# ==============================================================================
# Section 4: Data Preprocessing
# ==============================================================================
print("\nStarting data preprocessing...")
# Define features (X) and target (y)
X = df.drop('Churn', axis=1)
y = df['Churn']
# Identify numerical and categorical features
numerical_features = X.select_dtypes(include=np.number).columns.tolist()
categorical_features = X.select_dtypes(exclude=np.number).columns.tolist()
print(f"\nNumerical features: {numerical_features}")
print(f"Categorical features: {categorical_features}")
# Create preprocessing pipelines for numerical and categorical data
# Numerical features will be scaled.
# Categorical features will be one-hot encoded.
preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), numerical_features),
('cat', OneHotEncoder(handle_unknown='ignore'), categorical_features)
])
# ==============================================================================
# Section 5: Model Training
# ==============================================================================
print("\nSplitting data and training the model...")
# Split the data into training and testing sets (80/20 split)
# We use stratify=y to ensure the proportion of churn is the same in train and test sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Calculate scale_pos_weight for handling class imbalance in XGBoost
# It's the ratio of negative class to positive class
scale_pos_weight = y_train.value_counts()[0] / y_train.value_counts()[1]
print(f"\nCalculated 'scale_pos_weight' for imbalance: {scale_pos_weight:.2f}")
# Create the final machine learning pipeline with preprocessing and the model
# The model is an XGBoost Classifier, which is powerful for this type of problem
model = Pipeline(steps=[
('preprocessor', preprocessor),
('classifier', xgb.XGBClassifier(
objective='binary:logistic',
eval_metric='logloss',
use_label_encoder=False,
scale_pos_weight=scale_pos_weight, # Handle class imbalance
random_state=42
))
])
# Train the model
model.fit(X_train, y_train)
print("\nModel training complete.")
# ==============================================================================
# Section 6: Model Evaluation
# ==============================================================================
print("\nEvaluating the model on the test set...")
# Make predictions on the test set
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]
# Print Classification Report (Precision, Recall, F1-Score)
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
# Print Confusion Matrix
print("\nConfusion Matrix:")
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.savefig('evaluation_confusion_matrix.png')
plt.close()
print("Saved 'evaluation_confusion_matrix.png'")
# Print AUC-ROC Score
auc_score = roc_auc_score(y_test, y_pred_proba)
print(f"\nAUC-ROC Score: {auc_score:.4f}")
# ==============================================================================
# Section 7: Feature Importance
# ==============================================================================
print("\nGenerating feature importance plot...")
# Get feature names after one-hot encoding
feature_names = model.named_steps['preprocessor'].named_transformers_['cat'].get_feature_names_out(categorical_features)
all_feature_names = np.concatenate([numerical_features, feature_names])
# Get feature importances from the trained XGBoost model
importances = model.named_steps['classifier'].feature_importances_
# Create a DataFrame for visualization
importance_df = pd.DataFrame({
'feature': all_feature_names,
'importance': importances
}).sort_values('importance', ascending=False).head(15) # Top 15 features
# Plot feature importances
plt.figure(figsize=(12, 8))
sns.barplot(x='importance', y='feature', data=importance_df)
plt.title('Top 15 Most Important Features for Churn Prediction')
plt.tight_layout()
plt.savefig('feature_importance.png')
plt.close()
print("Saved 'feature_importance.png'")
print("\n--- SCRIPT FINISHED ---")
print("Check the folder for saved plots: 'eda_*.png', 'evaluation_*.png', and 'feature_importance.png'")
import pickle
# Save the trained model pipeline to a file
with open('churn_model.pkl', 'wb') as f:
pickle.dump(model, f)
print("\n✅ Model saved successfully as churn_model.pkl")