<jupyter_start><jupyter_text>Credit Risk Resampling Techniques<jupyter_code>import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
from pathlib import Path
from collections import Counter
from sklearn.metrics import balanced_accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from imblearn.ensemble import BalancedRandomForestClassifier
from imblearn.ensemble import EasyEnsembleClassifier
from imblearn.metrics import classification_report_imbalanced
from imblearn.over_sampling import RandomOverSampler
from sklearn.linear_model import LogisticRegression
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import ClusterCentroids
from imblearn.combine import SMOTEENN<jupyter_output><empty_output><jupyter_text>Read the CSV into DataFrame<jupyter_code># Load the data
file_path = Path('C:/Users/Alema/Desktop/FinTechClass/Homework/Homework_11/lending_data.csv')
df = pd.read_csv(file_path)
df.head()<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Testing<jupyter_code># Create our features
X = pd.get_dummies(df.drop('loan_status',axis=1))
# Create our target
y = df[['loan_status']]

X.describe()
# Check the balance of our target values
y['loan_status'].value_counts()
# Create X_train, X_test, y_train, y_test
X_train, X_test, y_train, y_test = train_test_split(X, 
                                                    y, 
                                                    random_state=1, 
                                                    stratify=y)<jupyter_output><empty_output><jupyter_text>Data Pre-ProcessingScale the training and testing data using the `StandardScaler` from `sklearn`. Remember that when scaling the data, you only scale the features data (`X_train` and `X_testing`).<jupyter_code># Create the StandardScaler instance
loan_scaler = StandardScaler()
# Fit the Standard Scaler with the training data
# When fitting scaling functions, only train on the training dataset
X_scaler = loan_scaler.fit(X_train)


# Scale the training and testing data
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)
X_train_scaled.shape<jupyter_output><empty_output><jupyter_text>Simple Logistic Regression<jupyter_code>model = LogisticRegression(solver='lbfgs', random_state=1)
model.fit(X_train, y_train)
# Calculated the balanced accuracy score

y_pred = model.predict(X_test)
balanced_accuracy_score(y_test, y_pred)
# Display the confusion matrix
cm = confusion_matrix(y_test, y_pred)
cm_df = pd.DataFrame(
    cm, index=["Actual 0", "Actual 1"], columns=["Predicted 0", "Predicted 1"]
)
print("Confusion Matrix")
display(cm_df)
# Print the imbalanced classification report

print("Imblanced Classification Report")
print(classification_report_imbalanced(y_test, y_pred))<jupyter_output>Imblanced Classification Report
                   pre       rec       spe        f1       geo       iba       sup

  high_risk       0.86      0.91      1.00      0.89      0.95      0.90       625
   low_risk       1.00      1.00      0.91      1.00      0.95      0.92     18759

avg / total       0.99      0.99      0.92      0.99      0.95      0.92     19384<jupyter_text>OversamplingIn this section, you will compare two oversampling algorithms to determine which algorithm results in the best performance. You will oversample the data using the naive random oversampling algorithm and the SMOTE algorithm. For each algorithm, be sure to complete the folliowing steps:1. View the count of the target classes using `Counter` from the collections library. 3. Use the resampled data to train a logistic regression model.3. Calculate the balanced accuracy score from sklearn.metrics.4. Display the confusion matrix from sklearn.metrics.5. Generate a classication report using the `imbalanced_classification_report` from imbalanced-learn.Note: Use a random state of 1 for each sampling algorithm to ensure consistency between tests  Naive Random Oversampling<jupyter_code># Resample the training data with the RandomOversampler
# View the count of target classes with Counter

ros = RandomOverSampler(random_state=1)
X_resampled, y_resampled = ros.fit_resample(X_train, y_train)
Counter(y_resampled)
# Train the Logistic Regression model using the resampled data
resample_model = LogisticRegression(solver='lbfgs', random_state=1)
resample_model.fit(X_resampled, y_resampled)
# Display the confusion matrix
y_pred_resample = resample_model.predict(X_test)
cm_resample = confusion_matrix(y_test, y_pred_resample)
cm_resample_df = pd.DataFrame(
    cm_resample, index=["Actual 0", "Actual 1"], columns=["Predicted 0", "Predicted 1"]
)
print("Confusion Matrix")
display(cm_resample_df)
# Calculated the balanced accuracy score
balanced_accuracy_score(y_test, y_pred_resample)
# Print the imbalanced classification report
print("Imblanced Classification Report")
print(classification_report_imbalanced(y_test, y_pred_resample))<jupyter_output>Imblanced Classification Report
                   pre       rec       spe        f1       geo       iba       sup

  high_risk       0.85      1.00      0.99      0.92      0.99      0.99       625
   low_risk       1.00      0.99      1.00      1.00      0.99      0.99     18759

avg / total       1.00      0.99      1.00      0.99      0.99      0.99     19384<jupyter_text>SMOTE Oversampling<jupyter_code># Resample the training data with SMOTE
# View the count of target classes with Counter

X_resampled, y_resampled = SMOTE(random_state=1, sampling_strategy=1.0).fit_resample(
    X_train, y_train
)

Counter(y_resampled)
# Train the Logistic Regression model using the resampled data
smote_model = LogisticRegression(solver='lbfgs', random_state=1)
smote_model.fit(X_resampled, y_resampled)
# Calculated the balanced accuracy score
smote_y_pred = smote_model.predict(X_test)
balanced_accuracy_score(y_test, smote_y_pred)
# Display the confusion matrix

cm_smote = confusion_matrix(y_test, smote_y_pred)
cm_smote_df = pd.DataFrame(
    cm_smote, index=["Actual 0", "Actual 1"], columns=["Predicted 0", "Predicted 1"]
)
print("Confusion Matrix")
display(cm_smote_df)
# Print the imbalanced classification report
print("Imblanced Classification Report")
print(classification_report_imbalanced(y_test, smote_y_pred))<jupyter_output>Imblanced Classification Report
                   pre       rec       spe        f1       geo       iba       sup

  high_risk       0.86      1.00      0.99      0.92      0.99      0.99       625
   low_risk       1.00      0.99      1.00      1.00      0.99      0.99     18759

avg / total       1.00      0.99      1.00      0.99      0.99      0.99     19384<jupyter_text>UndersamplingIn this section, you will test an undersampling algorithm to determine which algorithm results in the best performance compared to the oversampling algorithms above. You will undersample the data using the Cluster Centroids algorithm and complete the folliowing steps:1. View the count of the target classes using `Counter` from the collections library. 3. Use the resampled data to train a logistic regression model.3. Calculate the balanced accuracy score from sklearn.metrics.4. Display the confusion matrix from sklearn.metrics.5. Generate a classication report using the `imbalanced_classification_report` from imbalanced-learn.Note: Use a random state of 1 for each sampling algorithm to ensure consistency between tests<jupyter_code># Resample the data using the ClusterCentroids resampler
# View the count of target classes with Counter

cc = ClusterCentroids(random_state=1)
X_resampled, y_resampled = cc.fit_resample(X_train, y_train)

from collections import Counter

Counter(y_resampled)
# Train the Logistic Regression model using the resampled data
cc_model = LogisticRegression(solver='lbfgs', random_state=1)
cc_model.fit(X_resampled, y_resampled)
# Calculate the balanced accuracy score
cc_y_pred = cc_model.predict(X_test)
balanced_accuracy_score(y_test, cc_y_pred)
# Display the confusion matrix
cm_cc = confusion_matrix(y_test, cc_y_pred)
cm_cc_df = pd.DataFrame(
    cm_cc, index=["Actual 0", "Actual 1"], columns=["Predicted 0", "Predicted 1"]
)
print("Confusion Matrix")
display(cm_cc_df)
# Print the imbalanced classification report
print("Imblanced Classification Report")
print(classification_report_imbalanced(y_test, cc_y_pred))<jupyter_output>Imblanced Classification Report
                   pre       rec       spe        f1       geo       iba       sup

  high_risk       0.86      0.97      0.99      0.91      0.98      0.96       625
   low_risk       1.00      0.99      0.97      1.00      0.98      0.97     18759

avg / total       0.99      0.99      0.97      0.99      0.98      0.97     19384<jupyter_text>Combination (Over and Under) SamplingIn this section, you will test a combination over- and under-sampling algorithm to determine if the algorithm results in the best performance compared to the other sampling algorithms above. You will resample the data using the SMOTEENN algorithm and complete the folliowing steps:1. View the count of the target classes using `Counter` from the collections library. 3. Use the resampled data to train a logistic regression model.3. Calculate the balanced accuracy score from sklearn.metrics.4. Display the confusion matrix from sklearn.metrics.5. Generate a classication report using the `imbalanced_classification_report` from imbalanced-learn.Note: Use a random state of 1 for each sampling algorithm to ensure consistency between tests<jupyter_code># Resample the training data with SMOTEENN
# View the count of target classes with Counter

sm = SMOTEENN(random_state=1)
X_resampled, y_resampled = sm.fit_resample(X_train, y_train)
Counter(y_resampled)
# Train the Logistic Regression model using the resampled data
sm_model = LogisticRegression(solver='lbfgs', random_state=1)
sm_model.fit(X_resampled, y_resampled)
# Calculate the balanced accuracy score
sm_y_pred = sm_model.predict(X_test)
balanced_accuracy_score(y_test, sm_y_pred)
# Display the confusion matrix
sm_cm = confusion_matrix(y_test, sm_y_pred)
sm_cm_df = pd.DataFrame(
    sm_cm, index=["Actual 0", "Actual 1"], columns=["Predicted 0", "Predicted 1"]
)
print("Confusion Matrix")
display(sm_cm_df)
# Print the imbalanced classification report
print("Imblanced Classification Report")
print(classification_report_imbalanced(y_test, sm_y_pred))<jupyter_output>Imblanced Classification Report
                   pre       rec       spe        f1       geo       iba       sup

  high_risk       0.85      1.00      0.99      0.92      0.99      0.99       625
   low_risk       1.00      0.99      1.00      1.00      0.99      0.99     18759

avg / total       1.00      0.99      1.00      0.99      0.99      0.99     19384
{'fraction_non_alphanumeric': 0.06543065430654306, 'fraction_numerical': 0.035370353703537034, 'mean_word_length': 3.5006075334143376, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '448774', 'license': 'ADSL', 'n_tokens_mistral': 3651, 'n_tokens_neox': 3385, 'n_words': 1026, 'path': 'credit_risk_resampling.ipynb', 'repo_name': 'AlemanyLucas89/Homework_11', 'size': '11111'}