<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
df=pd.read_csv('/content/ChurnBank.csv')
df.head()
df.shape
df.dtypes
# First of all delete RowNumber and CustomerId as it is of no use.
df.drop(['RowNumber','CustomerId','Surname'], axis = 1, inplace = True)
df['Geography'].nunique()
df.Geography
df.isnull().sum()
df[df['Exited']== 0]
existed_yes = df[df['Exited']== 1].Tenure 
existed_no = df[df['Exited']== 0].Tenure 
plt.xlabel('Tenure')
plt.ylabel('no of customer')
plt.title('Churn prediction')
plt.hist([existed_no,existed_yes], color = ['yellow', 'green'], label =(['Existed = no','Existed = yes']))
plt.legend()
salary_no_existed = df[df.Exited== 0].EstimatedSalary
salary_yes_existed = df[df.Exited== 1].EstimatedSalary
plt.xlabel('Estimad_salary')
plt.ylabel('no of customer')
plt.title('Churn prediction')
plt.hist([salary_no_existed,salary_yes_existed], color = ['blue', 'pink'], label =(['Existed = no','Existed = yes']))
plt.legend()
def unique_val(df):
    for col in df:
        if df[col].dtypes == "object":
            print(f'{col}:{df[col].unique}')
unique_val(df)
for col in df :
    print(f'{col}:{df[col].unique()}')
# convert gender into 0,1
df['Gender'].replace({'Female' : 1,'Male':0}, inplace = True)
df.Gender.unique()
df1 = pd.get_dummies(data = df, columns = ['Geography'])
df1.head()
# Redefining data in the of 0 to 1

scaled_col = ['CreditScore','Age','Balance','EstimatedSalary']
from sklearn.preprocessing import MinMaxScaler
scalar = MinMaxScaler()
df1[scaled_col] = scalar.fit_transform(df1[scaled_col])
df1.head()
X = df1.drop('Exited',axis = 'columns')
y = df1['Exited']
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
y_train.shape
X_train[:10]
len(X_test.columns)
import tensorflow as tf 
from tensorflow import keras
model=  keras.Sequential([
    keras.layers.Dense(300, input_shape=(12,), activation = 'relu'),     #300 neurons, ReLU activation function
    keras.layers.Dense(1, activation = 'sigmoid')      #output layer
])
model.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics=['accuracy'])
model.fit(X_train,y_train, epochs = 50)
model.evaluate(X_test,y_test)
y_pred = model.predict(X_test)
y_pred
y_test[:5]
yp = []
for element in y_pred:
    if element > 0.5:
        yp.append(1)
    else:
        yp.append(0)
yp[:5]
y_test[:5]
import seaborn as sns 
cm = tf.math.confusion_matrix(labels=y_test, predictions=yp)
plt.figure(figsize=(8,5))
sns.heatmap(cm , annot=True,fmt = 'd')
plt.show()
y_test.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16611172770995192, 'fraction_numerical': 0.016278209396966333, 'mean_word_length': 5.693069306930693, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '446079', 'license': 'MIT', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1012, 'n_words': 218, 'path': '1c Implementation_of_Customer_churn_prediction_using_dataset_for_Bankipynb.ipynb', 'repo_name': 'JSDesai/NNDL_lab_2022', 'size': '2703'}