This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | CliCancelados = (df.customerID.loc[(df.tenure <= 24) & (df.Churn =='Yes')].index.value_counts().sum()) | |
| NCancelados = (df.customerID.loc[(df.tenure <= 24) & (df.Churn =='No')].index.value_counts().sum()) | |
| CliDetectados = (df.customerID.loc[(df.tenure <= 24) & (df.Churn =='Yes')].index.value_counts().sum())*0.91 | |
| CliFalsoPositivo = (df.customerID.loc[(df.tenure <= 24) & (df.Churn =='No')].index.value_counts().sum())*0.43 | |
| Recuperado = df.MonthlyCharges.loc[(df.tenure <= 24) & (df.Churn =='Yes')].mean()*CliDetectados | |
| print("Simulação da aplicação do modelo diretamente no Dataset (utilizando as porcentagens de acerto dos dados de teste)") | |
| print("=================================================================================================================") | |
| print("\nClientes que cancelaram contrato: {:.0f}".format(CliCancelados)) | |
| print("Clientes que seriam detectados entre os que cancelaram contrato: {:.0f}".format(CliDetectados)) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Criando o plot | |
| shap.summary_plot(shap_values, | |
| plot_type="dot", | |
| color_bar_label="Valor das variáveis", | |
| feature_names=X.columns, | |
| show=False) | |
| #Capturando a figure e os axes do objeto criado | |
| fig, ax = plt.gcf(), plt.gca() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Criando o plot | |
| shap.summary_plot(shap_values, | |
| max_display=20, | |
| plot_type="bar", | |
| feature_names=X.columns, | |
| show=False) | |
| #Capturando a figure e os axes do objeto criado | |
| fig, ax = plt.gcf(), plt.gca() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Criando um objeto para calcular os shap values | |
| explainer = shap.Explainer(lsvc_final.predict, X_test, random_state=seed) | |
| shap_values = explainer(X_test) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Instanciando o modelo com os melhores hiperparâmetros encontrados | |
| lsvc_final = LinearSVC(tol=1, | |
| class_weight={0:4,1:8}, | |
| fit_intercept=False, | |
| random_state=seed) | |
| #Normalizar as variáveis numéricas | |
| X_test = MinMax.fit_transform(X_test) | |
| #Balancear os dados | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Dicionário dos parâmetros que serão utilizados no grid search | |
| grid_params = { | |
| 'loss':['hinge', 'squared_hinge'], | |
| 'dual':[True, False], | |
| 'multi_class':['ovr', 'crammer_singer'], | |
| 'fit_intercept':[True,False], | |
| 'intercept_scaling':range(0,5,1) | |
| } | |
| #Instanciando o modelo | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Instanciando o modelo sem hiperparâmetros | |
| lsvc_random = LinearSVC(random_state=seed) | |
| #Dicionário dos parâmetros que serão utilizados na Randomized Search | |
| random_params = { | |
| 'penalty': ['l1','l2'], | |
| 'tol':[0.0001,0.001,0.01,0.1,1], | |
| 'C':range(1,20,5), | |
| 'class_weight':['balanced', {0:2,1:7}, {0:2,1:8}, {0:3,1:7}, {0:3,1:8}, {0:4,1:8}, {0:4,1:9}], | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Instanciando o modelo sem hiperparâmetros | |
| lsvc_random = LinearSVC(random_state=seed) | |
| #Dicionário dos parâmetros que serão utilizados na Randomized Search | |
| random_params = { | |
| 'penalty': ['l1','l2'], | |
| 'tol':[0.0001,0.001,0.01,0.1,1], | |
| 'C':range(1,20,5), | |
| 'class_weight':['balanced', {0:2,1:7}, {0:2,1:8}, {0:3,1:7}, {0:3,1:8}, {0:4,1:8}, {0:4,1:9}], | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Instanciando o modelo com os hiperparâmetros encontrados | |
| lsvc_grid = LinearSVC(C=1, | |
| tol=0.001, | |
| class_weight={0: 4, 1: 8}, | |
| penalty='l2', | |
| random_state=seed) | |
| #Treinando o modelo utilizando o cross_val_predict | |
| y_pred = cross_val_predict(lsvc_grid, X_train, y_train) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #Dicionário dos parâmetros que serão utilizados no grid search | |
| grid_params = { | |
| 'class_weight':['balanced', {0:2,1:7}, {0:2,1:8}, {0:3,1:7}, {0:3,1:8}, {0:4,1:8}, {0:4,1:9}], | |
| 'penalty':['l1', 'l2'], | |
| } | |
| #Instanciando o modelo | |
| lsvc_grid = LinearSVC(C=1, | |
| tol=0.001, | 
NewerOlder