# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernel_info:
#     name: python3-azureml
#   kernelspec:
#     display_name: Python 3.6 - AzureML
#     language: python
#     name: python3-azureml
# ---

# + [markdown] nteract={"transient": {"deleting": false}}
# # Assignment 4
# ## Kaggle: Telco Customer Churn.

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251078932}
#imports
import pandas as pd
# #!pip install plotly
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import seaborn as sns

# + [markdown] nteract={"transient": {"deleting": false}}
# ## Loading the dataset

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251079462}
data = pd.read_csv("WA_Fn-UseC_-Telco-Customer-Churn.csv")
data.head(10)

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251079866}
#number of rows and columns
data.shape

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251080275}
data.info()

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251080662}
data.describe()

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251080984}
#dataset columns
data.columns

# + [markdown] nteract={"transient": {"deleting": false}}
# ## 1.Demographic

# + [markdown] nteract={"transient": {"deleting": false}}
# ### a. Which 2 demographics have proven to be the most popular with the telco brand?
# ### b.If the marketing team wanted to increase customer retention, to whom should they targettheir advertisements to?

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251081402}
#demographics can be represented by gender, SeniorCitizen, Partner,  and Dependents columns
demographics = data[['customerID','gender', 'SeniorCitizen', 'Partner', 'Dependents','Churn']]
demographics

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251081935}
#Gender
axs=sns.catplot(x="gender", hue="Churn", data=demographics, kind='count',height=8.27, aspect=11.7/8.27)
plt.title ('Churn by Gender')


# + [markdown] nteract={"transient": {"deleting": false}}
# For both Male and Female there seems to be no major difference in the Churn 

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251082296}
#Senior Citizen
ax1=sns.catplot(x="SeniorCitizen", hue="Churn", data=demographics, kind='count',height=8.27, aspect=11.7/8.27)
plt.title ('Churn by Senior Citizen')

# + [markdown] nteract={"transient": {"deleting": false}}
# The number of none senior citizens is higher than senior citizens. The churn rate in older customers ,i.e. senior citizens, is higher than in younger customers.
#

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251082894}
#Partner
ax1=sns.catplot(x="Partner", hue="Churn", data=demographics, kind='count',height=8.27, aspect=11.7/8.27)
plt.title ('Churn by Partner')

# + [markdown] nteract={"transient": {"deleting": false}}
# Churn is slightly higher in customers without partners

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618251083518}
#Dependents
ax1=sns.catplot(x="Dependents", hue="Churn", data=demographics, kind='count',height=8.27, aspect=11.7/8.27)
plt.title ('Churn by Dependents')

# + [markdown] nteract={"transient": {"deleting": false}}
# Majority of the customer base have no dependents. the percentage of customers who churn is more in those with dependents than in those without.

# + [markdown] nteract={"transient": {"deleting": false}}
# #### a. The most popular demographics are dependents and Senior Citizen which is an indication of age as they have a significant impact on age.
# #### b. The marketing team should target their advertisements on senior citizens and on customers without dependents to increase customer retention. 

# + [markdown] nteract={"transient": {"deleting": false}}
# ## Services

# + [markdown] nteract={"transient": {"deleting": false}}
# ### a. Which 3 services  are  contributing  to  a  higher  monthly  charge  and  resulting  to customers churning?(Use visualizationsin explaining your analysis

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618253368775}
df_services = data[['PhoneService', 'MultipleLines', 'InternetService',
       'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport',
       'StreamingTV', 'StreamingMovies', 'MonthlyCharges']]
df_services

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}} gather={"logged": 1618253475435}
#changing service variables to be categorical
services=['PhoneService', 'MultipleLines', 'InternetService',
       'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport',
       'StreamingTV', 'StreamingMovies']
df_services[services]=df_services[services].astype('category')
df_services.dtypes


# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}}
#encoding service variables
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
df_services[services]= bridge_df['Bridge_Types'].cat.codes
encoded_df_services 

# + jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}}


StarcoderdataJupyterScriptsDedupFiltered