import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
%matplotlib inline
df = pd.read_csv("customer_churn.csv")
[Link](5)
[Link]('customerID',axis='columns',inplace=True)
[Link]
[Link]
pd.to_numeric([Link])
pd.to_numeric([Link],errors='coerce').isnull()
df[pd.to_numeric([Link],errors='coerce').isnull()
[Link]
[Link][488].TotalCharges
df[[Link]!=' '].shape
df1 = df[[Link]!=' ']
[Link]
[Link]
[Link] = pd.to_numeric([Link])
[Link]
df1[[Link]=='No']
tenure_churn_no = df1[[Link]=='No'].tenure
tenure_churn_yes = df1[[Link]=='Yes'].tenure
[Link]("tenure")
[Link]("Number Of Customers")
[Link]("Customer Churn Prediction Visualiztion")
blood_sugar_men = [113, 85, 90, 150, 149, 88, 93, 115, 135, 80, 77, 82, 129]
blood_sugar_women = [67, 98, 89, 120, 133, 150, 84, 69, 89, 79, 120, 112, 100]
[Link]([tenure_churn_yes, tenure_churn_no], rwidth=0.95,
color=['green','red'],label=['Churn=Yes','Churn=No'])
[Link]()
mc_churn_no = df1[[Link]=='No'].MonthlyCharges
mc_churn_yes = df1[[Link]=='Yes'].MonthlyCharges
[Link]("Monthly Charges")
[Link]("Number Of Customers")
[Link]("Customer Churn Prediction Visualiztion")
blood_sugar_men = [113, 85, 90, 150, 149, 88, 93, 115, 135, 80, 77, 82, 129]
blood_sugar_women = [67, 98, 89, 120, 133, 150, 84, 69, 89, 79, 120, 112, 100]
[Link]([mc_churn_yes, mc_churn_no], rwidth=0.95,
color=['green','red'],label=['Churn=Yes','Churn=No'])
[Link]()
def print_unique_col_values(df):
for column in df:
if df[column].dtypes=='object':
print(f'{column}: {df[column].unique()}')
print_unique_col_values(df1)
[Link]('No internet service','No',inplace=True)
[Link]('No phone service','No',inplace=True)
print_unique_col_values(df1)
yes_no_columns =
['Partner','Dependents','PhoneService','MultipleLines','OnlineSecurity','OnlineBackup',
'DeviceProtection','TechSupport','StreamingTV','StreamingMovies','PaperlessBilling','Churn']
for col in yes_no_columns:
df1[col].replace({'Yes': 1,'No': 0},inplace=True)
for col in df1:
print(f'{col}: {df1[col].unique()}')
df1['gender'].replace({'Female':1,'Male':0},inplace=True)
[Link]()
df2 = pd.get_dummies(data=df1, columns=['InternetService','Contract','PaymentMethod'])
[Link]
[Link](5)
[Link]
cols_to_scale = ['tenure','MonthlyCharges','TotalCharges']
from [Link] import MinMaxScaler
scaler = MinMaxScaler()
df2[cols_to_scale] = scaler.fit_transform(df2[cols_to_scale])
for col in df2:
print(f'{col}: {df2[col].unique()}')
X = [Link]('Churn',axis='columns')
y = df2['Churn']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=5)
X_train.shape
X_test.shape
X_train[:10]
len(X_train.columns)
import tensorflow as tf
from tensorflow import keras
model = [Link]([
[Link](26, input_shape=(26,), activation='relu'),
[Link](15, activation='relu'),
[Link](1, activation='sigmoid')
])
# opt = [Link](learning_rate=0.01)
[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=100)
[Link](X_test, y_test)
yp = [Link](X_test)
yp[:5]
y_pred = []
for element in yp:
if element > 0.5:
y_pred.append(1)
else:
y_pred.append(0)
y_pred[:10]
y_test[:10]
from [Link] import confusion_matrix , classification_report
print(classification_report(y_test,y_pred))
import seaborn as sn
cm = [Link].confusion_matrix(labels=y_test,predictions=y_pred)
[Link](figsize = (10,7))
[Link](cm, annot=True, fmt='d')
[Link]('Predicted')
[Link]('Truth')
y_test.shape
round((862+229)/(862+229+137+179),2)
round(862/(862+179),2)
round(229/(229+137),2)
round(229/(229+179),2)