Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

Python Code

Customer Churn Prediction - Python Script

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, LabelEncoder from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

Load dataset

df = pd.read_csv('data/raw_customer_data.csv')

Data Preprocessing

Handling missing values

df.fillna(method='ffill', inplace=True)

Convert categorical features using Label Encoding

label_encoder = LabelEncoder() df['Gender'] = label_encoder.fit_transform(df['Gender']) df['Contract'] = label_encoder.fit_transform(df['Contract']) df['PaymentMethod'] = label_encoder.fit_transform(df['PaymentMethod']) df['Churn'] = df['Churn'].apply(lambda x: 1 if x == 'Yes' else 0)

Splitting data into features and target variable

X = df.drop(columns=['Churn', 'CustomerID']) y = df['Churn'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Feature Scaling

scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test)

Model Training

model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train_scaled, y_train)

Predictions

y_pred = model.predict(X_test_scaled)

Model Evaluation

print("Accuracy:", accuracy_score(y_test, y_pred)) print("Classification Report:\n", classification_report(y_test, y_pred))

Confusion Matrix

plt.figure(figsize=(6,4)) sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues') plt.xlabel('Predicted') plt.ylabel('Actual') plt.title('Confusion Matrix') plt.show()

Save model and scaler

import joblib joblib.dump(model, 'models/churn_model.pkl') joblib.dump(scaler, 'models/scaler.pkl')

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors