<jupyter_start><jupyter_text>* @author: <EMAIL>  * @site: e-smartdata.org  Churn Modelling<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

url = 'https://ml-repository-krakers.s3-eu-west-1.amazonaws.com/kaggle+/churn_modelling/Telco-Customer-Churn.csv'
df = pd.read_csv(url)
df.head()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 7043 entries, 0 to 7042
Data columns (total 21 columns):
customerID          7043 non-null object
gender              7043 non-null object
SeniorCitizen       7043 non-null int64
Partner             7043 non-null object
Dependents          7043 non-null object
tenure              7043 non-null int64
PhoneService        7043 non-null object
MultipleLines       7043 non-null object
InternetService     7043 non-null object
OnlineSecurity      7043 non-null object
OnlineBackup        7043 non-null object
DeviceProtection    7043 non-null object
TechSupport         7043 non-null object
StreamingTV         7043 non-null object
StreamingMovies     7043 non-null object
Contract            7043 non-null object
PaperlessBilling    7043 non-null object
PaymentMethod       7043 non-null object
MonthlyCharges      7043 non-null float64
TotalCharges        7043 non-null object
Churn               7043 non-null object
dtypes: float64(1), int64(2), obj[...]<jupyter_text>Wstępne przetworzenie danych<jupyter_code>TotalChargesMedian = df[df.TotalCharges != ' ']['TotalCharges'].astype('float32').median()
df['TotalCharges'][df['TotalCharges'] == ' '] = TotalChargesMedian

df['TotalCharges'] = df['TotalCharges'].astype('float32')
df.info()
if not df.isnull().sum().sum():
    print('Nie ma braków.')
categorical = ['gender', 'SeniorCitizen', 'Partner', 'Dependents', 'PhoneService', 'MultipleLines', 
               'InternetService', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport',
               'StreamingTV', 'Contract', 'StreamingMovies', 'PaperlessBilling', 'PaymentMethod', 'Churn']
numerical = ['tenure', 'MonthlyCharges', 'TotalCharges']

for col in categorical: 
    df[col] = pd.Categorical(df[col])
    
for col in numerical: 
    df[col] = df[col].astype('float32')

df.info()
df = df.drop(columns=['customerID'])
df.info()
df.describe()
df.describe(include=['category'])<jupyter_output><empty_output><jupyter_text>Rozkład Churn<jupyter_code>plt.figure(figsize=(8, 6))
sns.set()
df.Churn.value_counts().plot(kind='pie',
                            fontsize=16,
                            colors=['#3DDC97', '#FF495C'],
                            explode=[0.1, 0],
                            shadow=True,
                            autopct='%1.1f%%')
plt.legend()
plt.ylabel('')
plt.title('Rozkład Churn')
print(df.Churn.value_counts())
plt.figure(figsize=(8, 6))
sns.set()
df.gender.value_counts().plot(kind='pie',
                            fontsize=16,
                            labels=['', ''],
                            colors=['#3DDC97', '#FF495C'],
                            explode=[0.1, 0],
                            shadow=True,
                            autopct='%1.1f%%')
plt.legend(['Male', 'Female'])
plt.ylabel('')
plt.title('Rozkład Płci')
print(df.Churn.value_counts())
plt.figure(figsize=(8, 6))
df.SeniorCitizen.value_counts().plot(kind='pie',
                            fontsize=16,
                            colors=['#3DDC97', '#FF495C'],
                            explode=[0.1, 0],
                            shadow=True,
                            autopct='%1.1f%%')
plt.legend()
plt.ylabel('')
plt.title('Rozkład zmiennej SeniorCitizen')
print(df.Churn.value_counts())<jupyter_output>No     5174
Yes    1869
Name: Churn, dtype: int64<jupyter_text>Rozkłady<jupyter_code>sns.distplot(df.MonthlyCharges, bins=30)
sns.distplot(df.tenure, bins=30)
fig, ax = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(12, 6))
sns.distplot(df.MonthlyCharges, bins=30, ax=ax[0])
sns.distplot(df.tenure, bins=30, ax=ax[1])

fig.suptitle('Rozkłady zmiennych')
ax[0].title.set_text('Rozkład: Monthly Charges')
ax[1].title.set_text('Rozkład: tenure')
sns.pairplot(data=df, vars=['tenure', 'MonthlyCharges', 'TotalCharges'],
            hue='Churn', palette=['#3DDC97', '#FF495C'])
sns.catplot(data=df, x='Churn', y='MonthlyCharges',
           kind='violin', palette=['#3DDC97', '#FF495C'])<jupyter_output><empty_output><jupyter_text><NAME><jupyter_code>df.corr()
sns.heatmap(df.corr())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14708500222518914, 'fraction_numerical': 0.04428126390743213, 'mean_word_length': 3.053201082055906, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '538313', 'license': 'MIT', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1574, 'n_words': 277, 'path': 'seaborn-tut/02_case_study.ipynb', 'repo_name': 'krakowiakpawel9/plotly-course', 'size': '4523'}