<jupyter_start><jupyter_text>Exploratory Data Analysis on Customer Attrition<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from scipy.stats import skew
from scipy.stats import kurtosis
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
sns.set()
df=pd.read_csv('dataset/clean_data.csv')
df.head()
# data conatins 14 columns and 10,000 datapoints
df.shape
df.Gender.unique()
df.IsActiveMember.unique()
#checking the missing values across columns

df.isnull().sum()
#checking how the data have been distributed (symmetrical or asymmetrical)
df.skew()
#CreditScore have 3 missing values within it and from below graph, it looks symmetrical with a skewness of -0.075

sns.distplot(df['CreditScore'])
sns.distplot(df['Balance'])
fig,axs = plt.subplots(2, 2, figsize=(15, 8))
sns.boxplot(data=df, x="CreditScore", color="skyblue", ax=axs[0, 0])
sns.boxplot(data=df, x="Tenure", color="olive", ax=axs[0, 1])
sns.boxplot(data=df, x="Balance", color="gold", ax=axs[1, 0])
sns.boxplot(data=df, x="EstimatedSalary", color="teal", ax=axs[1, 1])
#Analysis distribution of credit score across genders

f = plt.figure(figsize=(20, 10))
sns.boxplot(x='CreditScore', y='Gender', data=df, orient="h", width=0.6)
#Checking the collinearity between features

plt.figure(figsize = (20, 10))
corr = df.corr()
sns.heatmap(corr, cmap="YlGnBu", annot = True,linewidths = 0.5,)
plt.show()<jupyter_output><empty_output><jupyter_text>From the statistical of overview of data, we found out that the data is almost symmetrical across columns except couple of features like age, HasChcng where skewness is not in range of [-0.5,0.5]  Missing value imputation for CreditScore feature Since the data spread for CreditScore is symmetrical with a skewness of -0.075, we will impute the missing values with it's mean<jupyter_code>#First let's analyse the data with missing Credit Score

df_missing = df[df['CreditScore'].isna()]
df_missing<jupyter_output><empty_output><jupyter_text>Imputing missing values with mean<jupyter_code>#Using mean to impute the missing values
missing_col = ['CreditScore']
for i in missing_col:
    df.loc[df.loc[:,i].isnull(),i]=df.loc[:,i].mean()
#checking the missing values after imputation
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Till this step, we have handled the missing values and did a basic stat check for collinearity and spread of data across dataset.  Analysis statistics on clients attrition rate<jupyter_code># count 
plt.figure(figsize = (5, 5))
sns.countplot(x="Exited",data=df, hue="Exited")
plt.figure(figsize = (20, 10))
corr = df.corr()
sns.heatmap(corr, cmap="YlGnBu", annot = True,linewidths = 0.5,)
plt.show()
#Plotting the different bar charts to analyze data

fig, ax = plt.subplots(3, 2, figsize = (20, 20))
sns.barplot(x="Exited", y="Balance",hue="Tenure", data=df,ax = ax[0][0])
sns.barplot(x="Exited", y="EstimatedSalary",hue="Tenure", data=df,ax = ax[0][1])
sns.barplot(x="Exited", y="Tenure",hue="Gender", data=df,ax = ax[1][0])
sns.barplot(x="Exited", y="Tenure",hue="Gender", data=df,ax = ax[1][1])
sns.barplot(x="Exited", y="HasChckng",hue="Tenure", data=df,ax = ax[2][0])
sns.barplot(x="Exited", y="IsActiveMember",hue="Tenure", data=df,ax = ax[2][1])
# Analysing categorical variables to see the trend of customers got churned

fig, ax = plt.subplots(3, 2, figsize = (18, 15))

sns.countplot('Geography', hue = 'Exited', data = df, ax = ax[0][0],palette="Set2")
sns.countplot('Gender', hue = 'Exited', data = df, ax = ax[0][1],palette="Set2")
sns.countplot('Tenure', hue = 'Exited', data = df, ax = ax[1][0],palette="Set2")
sns.countplot('NumOfProducts', hue = 'Exited', data = df, ax = ax[1][1],palette="Set2")
sns.countplot('HasChckng', hue = 'Exited', data = df, ax = ax[2][0],palette="Set2")
sns.countplot('IsActiveMember', hue = 'Exited', data = df, ax = ax[2][1],palette="Set2")

plt.tight_layout()
plt.show()
plt.figure(figsize = (30, 8))
sns.countplot(x="Age",data=df, hue="Exited")
plt.show()<jupyter_output><empty_output><jupyter_text>Bonus Contents wrt to Advance analytics<jupyter_code># lets remove the columns from dataframe which are just identifiers (non-essential columns)
df1=df.drop(['CustomerId','Surname'],axis=1)
df1.head()<jupyter_output><empty_output><jupyter_text>Using One Hot Encoding For Geography and Gender feature<jupyter_code># importing one hot encoder from sklearn
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

# creating instance of one-hot-encoder
enc = OneHotEncoder(handle_unknown='ignore')

enc_df = pd.DataFrame(enc.fit_transform(df1[['Geography','Gender']]).toarray())

df2 = df1.join(enc_df)
df2.head()
dummies1 = pd.get_dummies(df.Geography)
dummies1.head(5)
dummies2 = pd.get_dummies(df.Gender)
dummies2.head(5)
#Merging dummies dataframes with main dataframe to get categorical features for Gender and Geography

df_input = pd.concat([df1,dummies1,dummies2],axis='columns')
df_input.head()
# Removing the dependent variable from dataframe/ creating the independent dataframes which will be input to model

X=df_input.drop('Exited',axis='columns')
X.head(5)
X.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14801375095492741, 'fraction_numerical': 0.021963330786860198, 'mean_word_length': 6.983231707317073, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '132585', 'license': 'MIT', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1871, 'n_words': 485, 'path': 'Exploratory Data Analysis.ipynb', 'repo_name': 'saboye/Customer-Churn-Analysis-using-Python', 'size': '5236'}