# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# # Desafio 05 | SONDA
# ## Objetivo
# O desafio consiste de implementar um algoritmo de Machine Learning para classificação binária, capaz de identificar se um cliente será perdido ou não.

from PIL import Image
image = Image.open("Ranking11.png")
image

# ## Import das bibliotecas

# + colab={"base_uri": "https://localhost:8080/"} id="BjwKH552p4x9" outputId="e776a6fa-fc72-4a86-ba28-1401b88062a5"
# !pip install dython

# + id="4sA5TwtEm9nD"
from dython import nominal
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from imblearn.over_sampling import SMOTE, SMOTENC
from imblearn.pipeline import Pipeline as imbpipeline
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.compose import ColumnTransformer, make_column_selector as selector
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, f1_score, confusion_matrix
from sklearn.model_selection import GridSearchCV, train_test_split, cross_validate, cross_val_score, StratifiedKFold, KFold
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler, MinMaxScaler, LabelEncoder
# -

# ## Leitura e análise do dataset

# + id="KuZJIqA3nAx3"
df = pd.read_csv('dataset.csv')

# + colab={"base_uri": "https://localhost:8080/"} id="Rpd3dmYvVAHk" outputId="1b671029-3a60-4cde-b50a-88c5d28a0f3f"
df['CHURN'].unique()

# + id="dOo2mSeFnkOL"
dfNa = df.dropna()

# + id="sEEu-gn-tN-e"
dfteste = df.fillna(df.mode().iloc[0])

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="O-InXLl0c8Xs" outputId="80d6d047-4075-4b59-ebd8-a5def28f17f5"
nominal.associations(dfNa, figsize=(20,10), mark_columns=True)

# + colab={"base_uri": "https://localhost:8080/", "height": 719} id="9m2dpUqtn7ob" outputId="c502a63c-b610-4509-bf78-9327ed118f66"
nominal.associations(dfteste, figsize=(20,10), mark_columns=True);


# + [markdown] id="jdC3cNRp9nBZ"
# Analisei a matriz e removi as que possuiam menos relações no total e com a coluna CHURN

# + id="XgXOKzw2QZYN"
class DropColumns(BaseEstimator, TransformerMixin):
    def __init__(self, columns):
        self.columns = columns

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        data = X.copy()
        return data.drop(labels=self.columns, axis='columns')


# + id="SZdw7xIumDb2"
class DummyEstimator(BaseEstimator):
        def fit(self): pass
        def score(self): pass


# -

# ## Distribuição das variáveis de treino e teste

# + id="ABv9378cQlmv"
X = df.drop('CHURN', axis=1, inplace=False)
y = df.CHURN

# + id="l5UuODhfQtvn"
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state = 1)

# + id="cNsOGEz9dWeT"
unwanted_columns = ['ID', 'GENDER', 'SENIORCITIZEN', 'PARTNER', 
                    'DEPENDENTS', 'PHONESERVICE', 'MULTIPLELINES']
Num_features = ['TENURE', 'MONTHLYCHARGES']
Cat_features = list((set(X.columns) - set(unwanted_columns) - set(Num_features)))

numeric_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent', missing_values=np.nan)),
    ('scaler', StandardScaler())])

categorical_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent', missing_values=np.nan)),
    ('onehot', OneHotEncoder(drop='first', sparse=False, handle_unknown='ignore'))])

preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_transformer, Num_features),
        ('cat', categorical_transformer, Cat_features)
    ]
)

dropCol = DropColumns(unwanted_columns)
# -

# ## Testando e validando o modelo

# + colab={"base_uri": "https://localhost:8080/"} id="r5ySvWnR_eQF" outputId="bcec7898-951a-43b3-a0b8-30fe52fdfcdc"
pip_LR = imbpipeline(steps=[
    ('drop_columns', dropCol),
    ('pre', preprocessor),
    ('smote', SMOTE(random_state=42)),
    ('clf', LogisticRegression(max_iter=30000))
])

pip_LR.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="lcpmWYngFumU" outputId="11c78cf2-0261-447d-c5ac-bd4dff4a0a68"
y_pred_test = pip_LR.predict(X_test)
print(classification_report(y_test, y_pred_test))

# + id="gXt4G5I4GKoS"
stratified_kfold = StratifiedKFold(n_splits=5)
kf = KFold(n_splits=5, random_state=1, shuffle=True)
param_grid = {'clf__C': [0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000]}
gridsearchLR = GridSearchCV(estimator=pip_LR, param_grid=param_grid, scoring='f1_micro',
                            cv=kf, n_jobs=-1)
best_modelLR = gridsearchLR.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="FbQ3ATodNbgb" outputId="dedbb05c-6230-49c9-a1a4-84fa85a0ee21"
y_pred_testGRID = best_modelLR.best_estimator_.predict(X_test)
print(classification_report(y_test, y_pred_testGRID)) #COMO QUE FICOU PIOR????

# + colab={"base_uri": "https://localhost:8080/"} id="rq7Tu-DgkBG9" outputId="55d46cc3-3495-41a2-9ea2-beb9c4e880a9"
confusion_matrix(y_test, y_pred_test)

# + colab={"base_uri": "https://localhost:8080/"} id="IdW-wgCwF3Ul" outputId="7354ec70-89da-4d83-bf64-1e5a7ae5e11a"
pip_LR.score(X_train, y_train)
# -

# ## Calculando a resposta

Alvo = pd.read_csv('ANSWERS.csv')

XAns = Alvo.drop('CHURN', axis=1, inplace=False)

y_pred_Ans = pip_LR.predict(XAns) # o pipeline cuida desse warning

Alvo['CHURN'] = y_pred_Ans
Alvo.to_csv('ANSWERS.csv', index=False)

StarcoderdataJupyterScriptsDedupFiltered