TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun · 77.8K subscribers
Post #3957 4.67K
✅ Data Science Project Series Part-2: Customer Churn Prediction

Project goal
Predict which customers will leave. Act before revenue drops.

Business value
• Retention costs less than acquisition
• Clear actions for sales and support
• High interview relevance

Dataset
Telco customer churn style dataset.
Target: Churn (Yes left, No stayed)

Key features
• tenure
• MonthlyCharges
• TotalCharges
• Contract
• PaymentMethod
• InternetService

Tech stack
• Python
• Pandas
• NumPy
• Matplotlib
• Seaborn
• Scikit-learn

Step 1. Import 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 LabelEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score


Step 2. Load data
df = pd.read_csv("customer_churn.csv")
df.head()


Step 3. Basic checks
df.shape
df.info()
df.isnull().sum()


Step 4. Data cleaning
Convert TotalCharges to numeric.
df['TotalCharges'] = pd.to_numeric(df['TotalCharges'], errors='coerce')
df['TotalCharges'].fillna(df['TotalCharges'].median(), inplace=True)

Drop customer ID.
df.drop('customerID', axis=1, inplace=True)


Step 5. Exploratory Data Analysis
Churn distribution.
sns.countplot(x='Churn', data=df)
plt.show()

Tenure vs churn.
sns.boxplot(x='Churn', y='tenure', data=df)
plt.show()

Common insights:
• Month-to-month contracts churn more
• Low tenure users churn early
• High monthly charges increase churn

Step 6. Encode categorical variables
le = LabelEncoder()
for col in df.select_dtypes(include='object').columns:
df[col] = le.fit_transform(df[col])


Step 7. Feature scaling
scaler = StandardScaler()
num_cols = ['tenure', 'MonthlyCharges', 'TotalCharges']
df[num_cols] = scaler.fit_transform(df[num_cols])


Step 8. Split data
X = df.drop('Churn', axis=1)
y = df['Churn']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)


Step 9. Build model
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)


Step 10. Predictions
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:,1]


Step 11. Evaluation
confusion_matrix(y_test, y_pred)
print(classification_report(y_test, y_pred))
roc_auc_score(y_test, y_prob)

Typical results:
• Accuracy around 78 to 83 percent
• ROC AUC around 0.84
• Recall for churn is key metric

Step 12. Business actions from model
• Target high-risk users
• Offer discounts to month-to-month users
• Push yearly contracts
• Improve onboarding for first 90 days

Resume bullet example:
• Built churn prediction model using Logistic Regression
• Identified contract type and tenure as top churn drivers
• Improved churn recall using class-aware split

Interview explanation flow:
• Revenue loss problem
• Why recall matters more than accuracy
• How features map to actions

Mini task for you:
• Train Random Forest
• Compare ROC AUC
• Tune threshold for higher recall

Double Tap ♥️ For Part-3
  • ❤ 17
More from @datasciencefun
  1. Oct 17, 2026𝗜𝗕𝗠 𝗙𝗥𝗘𝗘 𝗧𝗘𝗖𝗛 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝟮𝟬𝟮𝟲! 🎓 Explore IBM Cognitive Class and build…
  2. Oct 16, 2026𝗗𝗮𝘁𝗮 𝘀𝗰𝗶𝗲𝗻𝗰𝗲 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗪𝗶𝘁𝗵 𝗔𝗜😍 Future Ready Data Scien…
  3. Oct 15, 2026Post #4665
  4. Oct 15, 2026Post #4664
  5. Oct 15, 2026Post #4663
  6. Oct 15, 2026Post #4662
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →