<jupyter_start><jupyter_text>Load the dataset<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
pd.set_option('display.max_columns',None)

from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import AdaBoostClassifier
from xgboost import XGBClassifier 
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.metrics import roc_auc_score,accuracy_score,classification_report,roc_curve,confusion_matrix
from sklearn.model_selection import train_test_split

import warnings
warnings.filterwarnings('ignore')
#pd.set_option('max_rows', 6000)<jupyter_output><empty_output><jupyter_text>Problem StatementUsing the method of Boosting, classify whether or not the customer will churn```Customer churn, also known as customer attrition, customer turnover, or customer defection, is the loss of clients or customers.Telephone service companies, Internet service providers, pay-TV companies, insurance firms, and alarm monitoring services, often use customer attrition analysis and customer attrition rates as one of their key business metrics because the cost of retaining an existing customer is far less than acquiring a new one.Predictive analytics use churn prediction models that predict customer churn by assessing their propensity of risk to churn.Since these models generate a small prioritized list of potential defectors, they are effective at focusing customer retention marketing programs on the subset of the customer base who are most vulnerable to churn.For this project, we will be exploring the dataset of a telecom company and try to predict the customer churn```<jupyter_code>#Feature Description
feat_desc=pd.read_excel('/content/drive/MyDrive/Telecom Churn Prediction with Boosting/Telecom Churn Prediction_Data_Dictionary.xlsx')
feat_desc<jupyter_output><empty_output><jupyter_text>Load the Dataset<jupyter_code># read the dataset and extract the test  and train data separately
df_train=pd.read_csv('/content/drive/MyDrive/Telecom Churn Prediction with Boosting/train.csv')
df_test=pd.read_csv('/content/drive/MyDrive/Telecom Churn Prediction with Boosting/test.csv')

#Dropping df_train Id column : train_id
train_id = df_train['Id']
df_train.drop(['Id'], axis=1, inplace=True)
#First 5 rows
df_train.head()
# Data shape and columns

print(df_train.shape)
print(df_train.columns)
#Features Info

df_train.info()
# Describe data

df_train.describe()
#Describing data - 'object'

df_train.describe(include='object')<jupyter_output><empty_output><jupyter_text>Visualize the data and preprocess<jupyter_code>#Replacing empty value in TotalCharges
df_train['TotalCharges'].replace(' ',np.NaN, inplace=True)
df_test['TotalCharges'].replace(' ',np.NaN, inplace=True)
#Total Charges to float
df_train['TotalCharges']=df_train['TotalCharges'].astype(float)
df_test['TotalCharges']=df_test['TotalCharges'].astype(float)

# 9 Null values in TotalCharges
df_train.isnull().sum()
#Filling missing values in TotalCharges
df_train['TotalCharges'].fillna(df_train['TotalCharges'].mean(),inplace=True)
df_test['TotalCharges'].fillna(df_test['TotalCharges'].mean(), inplace=True)

# TotalCharges
sns.distplot(df_train['TotalCharges'])
#Target - Churn

sns.countplot(x='Churn', data=df_train)
#Obs: Imbalanced Classes
# Encoding categorical variables
encoder=LabelEncoder()

#Encoding Training data
df_train['Churn'] = encoder.fit_transform(df_train['Churn'])

col=['gender','Partner','Dependents','PhoneService','MultipleLines','InternetService','OnlineSecurity','OnlineBackup','DeviceProtection',
     'TechSupport','StreamingTV','StreamingMovies','Contract','PaperlessBilling','PaymentMethod']

for i in col:
  df_train[i] = encoder.fit_transform(df_train[i])
  df_test[i] = encoder.fit_transform(df_test[i])<jupyter_output><empty_output><jupyter_text>Model building<jupyter_code>#Splitting training and validation set

X=df_train.drop(['Churn','customerID'],1)

y=df_train['Churn']

print(X.head())
print(y.head())


#we split 75% of the data to training set while 25% of the data to validation 
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.25, random_state=0)

#X_train, X_valid shape
print(X_train.shape)
print(X_valid.shape)<jupyter_output>gender  SeniorCitizen  Partner  Dependents  tenure  PhoneService  \
0       0              0        0           0      27             1   
1       1              0        1           1       1             1   
2       0              0        0           0      17             1   
3       0              1        0           0      42             1   
4       1              0        1           0      23             1   

   MultipleLines  InternetService  OnlineSecurity  OnlineBackup  \
0              0                2               1             1   
1              0                2               1             1   
2              2                1               0             0   
3              2                1               0             0   
4              0                0               2             0   

   DeviceProtection  TechSupport  StreamingTV  StreamingMovies  Contract  \
0                 1            1            1                1         2   
1                 [...]<jupyter_text>**Using AdaBoost Classifier**<jupyter_code># Initialising AdaBoostClassifier
ada = AdaBoostClassifier()
ada.fit(X_train,y_train)
# Predicting the values of validation data
y_ada_pred = ada.predict(X_valid)
print("Classification report - \n", classification_report(y_valid,y_ada_pred))
#Accuracy Score
accuracy_score(y_valid,y_ada_pred)<jupyter_output><empty_output><jupyter_text>**Using XGBoost Classifier**<jupyter_code># Initialising AdaBoostClassifier
XGB = XGBClassifier()
XGB.fit(X_train,y_train)
# Predicting the values of validation data
y_XGB_pred = XGB.predict(X_valid)
print("Classification report - \n", classification_report(y_valid,y_XGB_pred))
#Accuracy Score
accuracy_score(y_valid,y_XGB_pred)<jupyter_output><empty_output><jupyter_text>Prediction on the test data<jupyter_code>#the Id column in a separate variable : test_id
test_id = df_test['Id']
df_test.drop(['Id'], axis=1, inplace=True) 

#Test data shape and columns names
print(df_test.shape)
print(df_test.columns)
#Features selected from test data

X_test=df_test.drop(['customerID'],1)

#make prediction : Churn - AdaBoostClassifier
y_test_pred = ada.predict(X_test)

#Making df for submission
subm=pd.DataFrame({"Id": test_id, "Churn": y_test_pred})
subm['Churn']=subm['Churn'].replace({0:'No', 1:'Yes'})
print(subm.head())
# To CSV for submission
#subm.to_csv('Telecom_Churn_submission.csv',index=False)

#from google.colab import files
#files.download('Telecom_Churn_submission.csv')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup