<jupyter_start><jupyter_text>Geodemographic Segmentation ModelDataset: https://www.superdatascience.com/training/Author: <NAME> (<EMAIL>)<jupyter_code>%matplotlib inline 
import warnings
warnings.filterwarnings("ignore")
from __future__ import division
import pandas
import csv
import numpy as np
import statsmodels.api as sm
import statsmodels.discrete.discrete_model as smdis
import statsmodels.stats.outliers_influence as outliers
import matplotlib.pyplot as plt
df = pandas.DataFrame.from_csv('Churn-Modelling.csv', index_col=None)
df[:5]<jupyter_output><empty_output><jupyter_text>Variables- Exited: dependent variable (y) -->  is binary- Gender, Geography: categorical independent variable (x)- All the remaining variables: numeric independent variable (x)<jupyter_code>df_y = df['Exited']
df_x = df.drop(['Exited'], axis = 1)<jupyter_output><empty_output><jupyter_text>Create dummy variables for the categorical variables:<jupyter_code>dummy = pandas.get_dummies(df_x['Gender'])
df_x = dummy.join(df_x)
dummy = pandas.get_dummies(df_x['Geography'])
df_x = dummy.join(df_x)
df_x[:5]<jupyter_output><empty_output><jupyter_text>Only one dummy variable should be used to avoid the "dummy variable trap":<jupyter_code>df_x = df_x.drop(['Gender', 'Male', 'France', 'Geography'], axis =1)
df_x[:5]<jupyter_output><empty_output><jupyter_text>Add a constant \begin{align} b_0 \end{align} to the model:<jupyter_code>df_x = sm.add_constant(df_x)
df_x[:2]<jupyter_output><empty_output><jupyter_text>Exclude the variables that should not affect the model:<jupyter_code>df_x = df_x.drop(['RowNumber', 'CustomerId', 'Surname'], axis = 1)
df_x[:2]<jupyter_output><empty_output><jupyter_text>Create a model with all the remaining variables:<jupyter_code>model1 = smdis.Logit(df_y, df_x).fit()
print model1.summary()
print "\n_____P-values____"
p = model1.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model1.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.428068
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9988
Method:                           MLE   Df Model:                           11
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1532
Time:                        13:04:11   Log-Likelihood:                -4280.7
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
===================================================================================
                      coef    std err          z      P>|z|      [95.0% Conf. Int.]
-------------------[...]<jupyter_text>Note: __"Pseudo R-squ."__ -  pseudo R-squared. Logistic regression does not have an equivalent to the R-squared that is found in OLS regression; however, many people have tried to come up with one.  There are a wide variety of pseudo-R-square statistics.  Because this statistic does not mean what R-square means in OLS regression (the proportion of variance explained by the predictors), we suggest interpreting this statistic with great caution.        (source: http://www.ats.ucla.edu/stat/stata/output/stata_logistic.htm)  __Backward Elimination__ New model without the variable with the highest p-value: "Spain"<jupyter_code>df_x1 = df_x.drop(['Spain'], axis = 1)
df_x1[:2]
model2 = smdis.Logit(df_y, df_x1).fit()
print model2.summary()
print "\n_____P-values____"
p = model2.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model2.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.428080
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9989
Method:                           MLE   Df Model:                           10
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1531
Time:                        13:04:12   Log-Likelihood:                -4280.8
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
===================================================================================
                      coef    std err          z      P>|z|      [95.0% Conf. Int.]
-------------------[...]<jupyter_text>New model without the variable with the highest p-value: "HasCrCard"<jupyter_code>df_x2 = df_x1.drop(['HasCrCard'], axis = 1)
df_x2[:2]
model3 = smdis.Logit(df_y, df_x2).fit()
print model3.summary()
print "\n_____P-values____"
p = model3.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model3.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.428109
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9990
Method:                           MLE   Df Model:                            9
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1531
Time:                        13:04:12   Log-Likelihood:                -4281.1
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
===================================================================================
                      coef    std err          z      P>|z|      [95.0% Conf. Int.]
-------------------[...]<jupyter_text>New model without the variable with the highest p-value: "EstimatedSalary"<jupyter_code>df_x3 = df_x2.drop(['EstimatedSalary'], axis = 1)
df_x3[:2]
model4 = smdis.Logit(df_y, df_x3).fit()
print model4.summary()
print "\n_____P-values____"
p = model4.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model4.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.428161
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9991
Method:                           MLE   Df Model:                            8
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1530
Time:                        13:04:12   Log-Likelihood:                -4281.6
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
==================================================================================
                     coef    std err          z      P>|z|      [95.0% Conf. Int.]
---------------------[...]<jupyter_text>New model without the variable with the highest p-value: "Tenure"<jupyter_code>df_x4 = df_x3.drop(['Tenure'], axis = 1)
df_x4[:2]
model5 = smdis.Logit(df_y, df_x4).fit()
print model5.summary()
print "\n_____P-values____"
p = model5.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model5.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.428307
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9992
Method:                           MLE   Df Model:                            7
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1527
Time:                        13:04:13   Log-Likelihood:                -4283.1
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
==================================================================================
                     coef    std err          z      P>|z|      [95.0% Conf. Int.]
---------------------[...]<jupyter_text>Since the Accuracy decreases, the variable "Tenure" should be used in the model. Now, the variable with the highest p-value is "NumOfProducts" with 0.032, which is above of the defined threshold - 0.05.<jupyter_code>df_x3[:5]<jupyter_output><empty_output><jupyter_text>Transforming Independent Variables Most common transformations:1. $$\sqrt{x}$$2. $$x^2$$3. $$ln(x)$$ Transform "Balance" variable with ln(x):Original value: Balance (in 1000$):        Bal2 = Bal1 + 1unit -->  Bal2 = Bal1 + 1000$                Scenario1: Bal1 = 1000$            --> Bal2 = 1000$ + 1000$ = 2000$        Scenario2: Bal1 = 10000$            --> Bal2 = 10000$ + 1000$ = 11000$          Log10(Balance + 1):        log10(Bal2) = log10(Bal1) + 1unit --> Bal2 = Bal1*10                Scenario1: Bal1 = 1000$            --> Bal2 = 1000*10 = 10000    Scenario2: Bal1 = 10000$            ---> Bal2 = 10000*10 = 100000            Using the original value, means that if someone that starts with a balance of 1000$ and has an increase of 1000$, and someone that starts with a balance of 10000$ and has the same amount of increasing, are two completly different things.Using the ln transformation, it has the same affect on any person regardless of their starting point. It always a 10 times increase so a unit increase has a consistent increase in the balance variable which is 10 times.So regardless of who we're segmenting we can say that the effect of a one unit increase in the new transformed variable is consistent throughout our population and that is much more powerful because that does not restrict the logistic regression.<jupyter_code>df_x4 = df_x3
df_x4['Balance_log'] = df_x4['Balance'].map(lambda x: np.log10(x+1))
df_x4[:2]
model6 = smdis.Logit(df_y, df_x4).fit()
print model6.summary()
print "\n_____P-values____"
p = model6.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model6.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.428134
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9990
Method:                           MLE   Df Model:                            9
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1530
Time:                        13:04:14   Log-Likelihood:                -4281.3
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
==================================================================================
                     coef    std err          z      P>|z|      [95.0% Conf. Int.]
---------------------[...]<jupyter_text>The accuracy is improved!  Derived VariablesSometimes it is useful too account for some effects, for example in our case, the balance and age might be correlated, because the older a person is the more whealth he/she can accumulate. So a new variable can be created:$$WealthAccumlation = \frac{Balance}{Age}$$<jupyter_code>df_x5 = df_x4
df_x5['WealthAccumulation'] = df_x5['Balance']/ df_x5['Age']
df_x5[:2]
model7 = smdis.Logit(df_y, df_x5).fit()
print model7.summary()
print "\n_____P-values____"
p = model7.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model7.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.427323
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9989
Method:                           MLE   Df Model:                           10
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1546
Time:                        13:04:15   Log-Likelihood:                -4273.2
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
======================================================================================
                         coef    std err          z      P>|z|      [95.0% Conf. Int.]
-------------[...]<jupyter_text>This new variable had a negative effect in the model accuracy.However, one should have in consideration that might be some collinearity effects because wealth accumulation includes the variables balance and age that are already in the model. So basically this means that this new variable might be somehow correlated with the other two and including all of them in the model might cause some damage, so on of them should be excluded.  MulticollinearityMulticollinearity is a phenomenon in which two or more predictor variables in a multiple regression model are highly correlated, meaning that one can be linearly predicted from the others with a substantial degree of accuracy.The "Variance Inflation Factors - VIF" can be used to measure the degree of multicollinearity.Minimum possible value = 1.0Values > 5 may indicate a collinearity problem.$$VIF(j) = \frac{1}{( 1 - R(j)^2)}$$where R(j) is the multiple correlarion coefficient between variable j and the other independent variables<jupyter_code>i = 0
for column in df_x5.columns:
    
    print column + " %s" % outliers.variance_inflation_factor(df_x5.values, i)
    i += 1<jupyter_output>const 91.4773722803
Germany 1.27101224421
Female 1.00321582613
CreditScore 1.00104450767
Age 2.10487620512
Tenure 1.00161073156
Balance 20.8895673667
NumOfProducts 1.15312466504
IsActiveMember 1.01384972933
Balance_log 8.72881202924
WealthAccumulation 14.5598595824<jupyter_text>Rerun without the "Balance_log" variable:<jupyter_code>df_x6 = df_x5.drop('Balance_log', axis = 1)
model8 = smdis.Logit(df_y, df_x6).fit()
print model8.summary()
print "\n_____P-values____"
p = model8.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model8.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))
i = 0

for column in df_x6.columns:
    
    print column + " %s" % outliers.variance_inflation_factor(df_x6.values, i)
    i += 1<jupyter_output>const 90.6320775047
Germany 1.2161676569
Female 1.00302785174
CreditScore 1.00097995156
Age 2.10477505278
Tenure 1.00153406128
Balance 14.0245653407
NumOfProducts 1.12330226029
IsActiveMember 1.01353234553
WealthAccumulation 14.5598562679<jupyter_text>Create new variable log for wealth accumulation:<jupyter_code>df_x7 = df_x5
df_x7['WealthAccumulation_log'] = df_x7['WealthAccumulation'].map(lambda x: np.log10(x + 1))
df_x7[:2]
df_x7 = df_x7.drop(['Balance', 'WealthAccumulation'], axis = 1)
model9 = smdis.Logit(df_y, df_x7).fit()
print model9.summary()
print "\n_____P-values____"
p = model9.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model9.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))
i = 0

for column in df_x7.columns:
    
    print column + " %s" % outliers.variance_inflation_factor(df_x7.values, i)
    i += 1<jupyter_output>const 93.5608074531
Germany 1.26986359735
Female 1.00282038022
CreditScore 1.00102945704
Age 2.26479838945
Tenure 1.00165459162
NumOfProducts 1.15243000098
IsActiveMember 1.01168357579
Balance_log 705.940971113
WealthAccumulation_log 704.739971273<jupyter_text>"Balance_log" and "WealthAccumulation_log" have too large values which means that these two variables are basically the same thing. So one of these variables must be excluded!<jupyter_code>df_x8 = df_x7.drop(['Balance_log'], axis = 1)
model10 = smdis.Logit(df_y, df_x8).fit()
print model10.summary()
print "\n_____P-values____"
p = model10.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model10.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))<jupyter_output>Optimization terminated successfully.
         Current function value: 0.428322
         Iterations 6
                           Logit Regression Results                           
==============================================================================
Dep. Variable:                 Exited   No. Observations:                10000
Model:                          Logit   Df Residuals:                     9991
Method:                           MLE   Df Model:                            8
Date:                Wed, 28 Dec 2016   Pseudo R-squ.:                  0.1527
Time:                        13:04:18   Log-Likelihood:                -4283.2
converged:                       True   LL-Null:                       -5054.9
                                        LLR p-value:                     0.000
==========================================================================================
                             coef    std err          z      P>|z|      [95.0% Conf. Int.]
-----[...]<jupyter_text>Correlation between variables:<jupyter_code>df_x5[['Age', 'Balance_log', 'WealthAccumulation_log', 'WealthAccumulation']].corr()<jupyter_output><empty_output><jupyter_text>As one can observe, the "WealthAccumulation_log" and "Balance_log" variables are highly correlated - they are basically the same which is very bad for the model. Thumb rule: anything 0.9 is very high correlation. Correlations above 0.5 should be addressed.<jupyter_code>df_final = df_x5.drop(['Balance', 'WealthAccumulation', 'WealthAccumulation_log'], axis = 1)
model11 = smdis.Logit(df_y, df_final).fit()
print model11.summary()
print "\n_____P-values____"
p = model11.pvalues
print p
print "\n_____Highest p-value____"
print "\n %s \n" % p[p ==max(p)]
print "\n____Confusion Matrix___"
cm = model11.pred_table()
print cm
print "\nNumber of cases correctly predicted: %s (%s %%)" % (cm[0][0] + cm[1][1], (cm[0][0] + cm[1][1])*100/np.sum(cm))
i = 0

for column in df_final.columns:
    
    print column + " %s" % outliers.variance_inflation_factor(df_final.values, i)
    i += 1<jupyter_output>const 76.3936634172
Germany 1.26934389528
Female 1.00281465894
CreditScore 1.00102204693
Age 1.01171547993
Tenure 1.0014367308
NumOfProducts 1.15145597018
IsActiveMember 1.01028026511
Balance_log 1.42007014313<jupyter_text>Now, none of the variables presents multicollinearity.  CAP - Cumulative Accuracy Profile Curve<jupyter_code>df_cap = df[['RowNumber', 'Exited']]
df_cap['phat'] = model11.predict()
df_cap.sort('phat', ascending=False)[:5]
Total_Exited = sum(df_cap['Exited'])
print "Total Exited: %s" % Total_Exited
Total_Records = len(df)
print "Total number of clients: %s" % Total_Records
Exit_Ratio = Total_Exited/Total_Records
print "Ratio of exits: %s " % Exit_Ratio<jupyter_output>Total Exited: 2037
Total number of clients: 10000
Ratio of exits: 0.2037<jupyter_text>First, I select the clients that have a higher probability.<jupyter_code>df_cap['total_selected'] = range(1,len(df_cap)+1)
df_cap['total_selected_per'] = df_cap['total_selected'].map(lambda x: round(x*100/Total_Records, 1))
df_cap['random_selection'] = df_cap['total_selected'].map(lambda x: x*Exit_Ratio)
df_cap['random_selection_per'] = df_cap['total_selected'].map(lambda x: round(x*100/Total_Records, 1))
df_cap = df_cap.sort('phat', ascending=False)
df_cap['model_select'] = df_cap.Exited.cumsum()
df_cap['model_select_per'] = df_cap['model_select'].map(lambda x: round(x*100/Total_Exited, 1))
df_cap[:5]
df_cap = df_cap.sort('RowNumber')
plt.plot(df_cap['total_selected_per'], df_cap['random_selection_per'], label = 'random model' )
plt.plot(df_cap['total_selected_per'], df_cap['model_select_per'], label = 'Good Model' )
plt.plot([50, 50], [80, 0], 'r--', lw=1, label = 'x = 50%, y = X')
plt.plot([0, 50], [80, 80], 'r--', lw=1)
plt.plot([10, 100], [99, 99], 'y-', lw=3, label = 'perfect model')
plt.plot([0, 10], [0, 99], 'y-', lw=3)
plt.legend(bbox_to_anchor=(1, 1), loc='upper left', ncol=1)
plt.xlabel('Total Contacted')
plt.ylabel('Total Purchased')
plt.show()<jupyter_output><empty_output><jupyter_text>The closer the green line is to the yellow line, the better the model. The closer to the blue line, the worse. __Accuracy Ratio__:    - Take the area under the perfect model (yellow line) to the random model (blue line) - $$a_p$$    - Take the area under the good model (green line) to the random model (blue line) -     $$a_r$$    - So the accuracy ratio:    $$AR = \frac{a_r}{a_p}$$that is between 0 and 1, and the closer the ratio is to 1 the better.__Rule of thumb:__Another way of evaluating the model is to look to the red line, x = 50%, and y = X:If:    90%  Too Good (overfitting?)    80%  Very Good    70%  Good    60%  Poor    X  Rubbish  Test model with test data<jupyter_code>df_test = pandas.DataFrame.from_csv('Churn-Modelling-Test-Data.csv', index_col=None)
df_test[:5]
df_test = sm.add_constant(df_test)
df_test_y = df_test['Exited']
df_test = df_test.drop(['Exited'], axis = 1)
dummy = pandas.get_dummies(df_test['Gender'])
df_test= dummy.join(df_test)
dummy = pandas.get_dummies(df_test['Geography'])
df_test = dummy.join(df_test)
df_test[:5]
df_test['Balance_log'] = df_test['Balance'].map(lambda x: np.log10(x+1))<jupyter_output><empty_output><jupyter_text>Check the parameters included in the final model (model11) and rerun the model with the same parameters but now including the test data:<jupyter_code>model11.params
df_test = df_test[['Germany', 'Female', 'CreditScore', 'Age', 'Tenure', 'NumOfProducts', 'IsActiveMember', 'Balance_log', 'const']]
df_total = df_final.append(df_test)
model_final = smdis.Logit(df_y, df_total[:10000]).fit()
y_pred = model_final.predict(df_total[10000:])
y_pred[:5]<jupyter_output><empty_output><jupyter_text>Let's construct the CAP curve:<jupyter_code>df_pred = pandas.DataFrame()
df_pred['phat'] = y_pred
df_pred['Exited'] = df_test_y
df_pred = df_pred.sort( 'phat', ascending=False)
df_pred[:5]
Total_Exited = sum(df_pred['Exited'])
print "Total Exited: %s" % Total_Exited
Total_Records = len(df_pred)
print "Total number of clients: %s" % Total_Records
Exit_Ratio = Total_Exited/Total_Records
print "Ratio of exits: %s" % Exit_Ratio
df_pred['total_selected'] = range(1,len(df_pred)+1)
df_pred['total_selected_per'] = df_pred['total_selected'].map(lambda x: round(x*100/Total_Records, 1))
df_pred['random_selection'] = df_pred['total_selected'].map(lambda x: x*Exit_Ratio)
df_pred['random_selection_per'] = df_pred['total_selected'].map(lambda x: round(x*100/Total_Records, 1))
df_pred = df_pred.sort('phat', ascending=False)
df_pred['model_select'] = df_pred.Exited.cumsum()
df_pred['model_select_per'] = df_pred['model_select'].map(lambda x: round(x*100/Total_Exited, 1))
df_pred[:5]
plt.plot(df_pred['total_selected_per'], df_pred['random_selection_per'], label = 'random model' )
plt.plot(df_pred['total_selected_per'], df_pred['model_select_per'], label = 'Good Model' )
plt.plot([50, 50], [76, 0], 'r--', lw=1, label = 'x = 50%, y = X')
plt.plot([0, 50], [76, 76], 'r--', lw=1)
plt.plot([10, 100], [99, 99], 'y-', lw=3, label = 'perfect model')
plt.plot([0, 10], [0, 99], 'y-', lw=3)
plt.legend(bbox_to_anchor=(1, 1), loc='upper left', ncol=1)
plt.xlabel('Total Contacted')
plt.ylabel('Total Purchased')
plt.show()<jupyter_output><empty_output><jupyter_text>__Conclusions__:- The model is performing above 80%, so a bit worse than the training data (a drop of about 3%).- The concentration of data is about 10 times less than the training data which explains the ruggedness of the curve.  Parameters Interpretation Odds ratio:$$odds = \frac{p}{1-p}$$Multiple logistic regression:$$ln(\frac{p}{1-p}) = b_0 + b_0x_1 + ... + b_nx_n$$So we can write:$$ln(odds) = b_0 + b_0x_1 + ... + b_nx_n$$$$ odds = e^{b_0 + b_0x_1 + ... + b_nx_n}$$$$ odds = e^{b_0} + e^{b_0x_1} + ... + e^{b_nx_n}$$which means that, if we increase an unit to the $$x_1$$ variable,  for example:$$e^{b_1x_1} -> e^{b_1(x_1 + 1)} = e^{b_1x_1}e^{b_1}$$which means that, increading an independente variable, e.g. $$x_1$$, by 1 unit, will increase the odds by a multiplicative factor of $$e^{b_i}$$So let's transform the coefficients of our model into odds ratios:<jupyter_code>odds_ratio = np.exp(model_final.params)
odds_ratio<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16542132129910483, 'fraction_numerical': 0.055226751498113485, 'mean_word_length': 2.8418360096632087, 'pattern_counts': {'":': 1, '<': 94, '<?xml version=': 0, '>': 111, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '26', 'dir': 'jupyter-structured-clean-dedup', 'id': '605050', 'license': 'BSD-3-Clause-LBNL', 'n_tokens_mistral': 9520, 'n_tokens_neox': 8342, 'n_words': 2304, 'path': 'Docker/multiple-container-setup/localfiles_test/test2/test2.ipynb', 'repo_name': 'krinsman/notebook-clone-extension', 'size': '27070'}