<jupyter_start><jupyter_text>Model: Simple Classification<jupyter_code>y = data['y']
x1 = data['duration']
x = sm.add_constant(x1)
reg_log = sm.Logit(y,x)
results_log = reg_log.fit()
results_log.summary()
plt.scatter(x1,y,color = 'C0')
plt.xlabel('Duration', fontsize = 20)
plt.ylabel('Subscription', fontsize = 20)
plt.show()<jupyter_output><empty_output><jupyter_text>Expanded model: Multivariate Classification<jupyter_code>estimators=['interest_rate','credit','march','previous','duration']
X1_all = data[estimators]
y = data['y']
X_all = sm.add_constant(X1_all)
reg_logit = sm.Logit(y,X_all)
results_logit = reg_logit.fit()
results_logit.summary2()<jupyter_output>Optimization terminated successfully.
         Current function value: 0.336664
         Iterations 7<jupyter_text>Confusion Matrix<jupyter_code>def confusion_matrix(data,actual_values,model):
        
        # Confusion matrix 
        
        # Parameters
        # ----------
        # data: data frame or array
            # data is a data frame formatted in the same way as my input data (without the actual values)
            # e.g. const, var1, var2, etc. Order is very important!
        # actual_values: data frame or array
            # These are the actual values from the test_data
            # In the case of a logistic regression, it should be a single column with 0s and 1s
            
        # model: a LogitResults object
            # this is the variable where I have the fitted model 
        # ----------
        
        #Predict the values using the Logit model
        pred_values = model.predict(data)
        # Specify the bins 
        bins=np.array([0,0.5,1])
        # A histogram where values between 0 and 0.5 are considered 0
        # If they are between 0.5 and 1, they will be considered 1
        cm = np.histogram2d(actual_values, pred_values, bins=bins)[0]
        # Calculate the accuracy
        accuracy = (cm[0,0]+cm[1,1])/cm.sum()
        return cm, accuracy
confusion_matrix(X_all,y,results_logit)<jupyter_output><empty_output><jupyter_text>Testing the accuracy of the model<jupyter_code>raw_data2 = pd.read_csv('Bank-data-testing.csv')
data_test = raw_data2.copy()
data_test = data_test.drop(['Unnamed: 0'], axis = 1)
data_test['y'] = data_test['y'].map({'yes':1, 'no':0})
data_test
y_test = data_test['y']
X1_test = data_test[estimators]
X_test = sm.add_constant(X1_test)
confusion_matrix(X_test, y_test, results_logit)
confusion_matrix(X_all,y, results_logit)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11668657905217045, 'fraction_numerical': 0.019514137793707687, 'mean_word_length': 3.5342960288808665, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '651052', 'license': 'Apache-2.0', 'n_tokens_mistral': 873, 'n_tokens_neox': 830, 'n_words': 202, 'path': 'Testing the Model - Exercise.ipynb', 'repo_name': 'jovemmanuelre/Simple-and-Multivariate-Classification', 'size': '2511'}