Skip to content

The telecommunications (telecom) sector in India is rapidly changing, with more and more telecom businesses being created and many customers deciding to switch between providers. "Churn" refers to the process where customers or subscribers stop using a company's services or products. Understanding the factors that influence keeping a customer as a client in predicting churn is crucial for telecom companies to enhance their service quality and customer satisfaction. As the data scientist on this project, you aim to explore the intricate dynamics of customer behavior and demographics in the Indian telecom sector in predicting customer churn, utilizing two comprehensive datasets from four major telecom partners: Airtel, Reliance Jio, Vodafone, and BSNL:

  • telecom_demographics.csv contains information related to Indian customer demographics:
VariableDescription
customer_id Unique identifier for each customer.
telecom_partner The telecom partner associated with the customer.
gender The gender of the customer.
age The age of the customer.
stateThe Indian state in which the customer is located.
cityThe city in which the customer is located.
pincodeThe pincode of the customer's location.
registration_eventWhen the customer registered with the telecom partner.
num_dependentsThe number of dependents (e.g., children) the customer has.
estimated_salaryThe customer's estimated salary.
  • telecom_usage contains information about the usage patterns of Indian customers:
VariableDescription
customer_idUnique identifier for each customer.
calls_madeThe number of calls made by the customer.
sms_sentThe number of SMS messages sent by the customer.
data_usedThe amount of data used by the customer.
churnBinary variable indicating whether the customer has churned or not (1 = churned, 0 = not churned).
# Import libraries and methods/functions
import pandas as pd
from sklearn.preprocessing import StandardScaler, OneHotEncoder 
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix

# Start your code here!               
# importing datasets 
telecoms_df = pd.read_csv('telecom_demographics.csv') 

print(telecoms_df.head()) 
print(telecoms_df.shape) 
print(telecoms_df.columns)
# Checking shape of data 

tele_usage_df = pd.read_csv('telecom_usage.csv') 

print(tele_usage_df.head())

print(tele_usage_df.shape)
print(tele_usage_df.columns)
# churn dataset 

churn_df = telecoms_df.merge(tele_usage_df, on = 'customer_id') 
print(churn_df.head())
print(churn_df.shape)
print(churn_df.info())
churn_df.head()
# Churn rate 

print(churn_df['churn'].value_counts(normalize=True))

churn_rate = churn_df['churn'].value_counts() / len(churn_df)
print(churn_rate)
# Converting categorical features

churn_df = pd.get_dummies(churn_df, columns=['telecom_partner', 'gender', 'state', 'city', 'registration_event'])

print(churn_df.columns)
# Splitting data

scaler = StandardScaler() 

features = churn_df.drop(['customer_id', 'churn'], axis=1)
features.head()

# Scaling 
features_scaled = scaler.fit_transform(features)
# scaling 

target = churn_df['churn'] 
target.head() 
# Splitting data 

X_train, X_test, y_train, y_test = train_test_split(features_scaled, target, test_size=0.2, random_state=42) 
# fitting logistic regression model 
logreg = LogisticRegression(random_state=42) 

logreg.fit(X_train, y_train)
logreg_pred = logreg.predict(X_test) 

# confusion matrix
print(confusion_matrix(y_test, logreg_pred)) 

# classification_report 
print(classification_report(y_test, logreg_pred))
# fitting random forest 

rf = RandomForestClassifier(random_state=42) 

rf.fit(X_train, y_train)
rf_pred = rf.predict(X_test)

print(confusion_matrix(y_test, rf_pred))
print(classification_report(y_test, rf_pred))
# higher accuracy 

higher_accuracy = 'RandomForest'