<jupyter_start><jupyter_text>Peer-to-Peer Credit Risk AnalysisCredit risk poses a classification problem that’s inherently imbalanced. This is because healthy loans easily outnumber risky loans. This program uses various techniques to train and evaluate models with imbalanced classes. It uses a dataset of historical lending activity from a peer-to-peer lending services company to build a model that can identify the creditworthiness of borrowers.<jupyter_code># Imported the modules
import numpy as np
import pandas as pd
from pathlib import Path
from sklearn.metrics import balanced_accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from imblearn.over_sampling import RandomOverSampler
from imblearn.metrics import classification_report_imbalanced

import warnings
warnings.filterwarnings('ignore')
# Read the CSV file from the Resources folder into a Pandas DataFrame
lending_df = pd.read_csv(Path('Resources/lending_data.csv'))

# Review the DataFrame
lending_df.head()
# Separated the data into labels:(y) and features:(X)
y = lending_df['loan_status']

X = lending_df.drop(columns=['loan_status'])
# Review the y variable Series
y[:5]
# Review the X variable DataFrame
X[:5]
# Checked the balance of our target values
y.value_counts()
# Split the data using train_test_split
# Assigned a random_state of 1 to the function
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
# Instantiated the Logistic Regression model
# Assigned a random_state parameter of 1 to the model
logistic_regression_model = LogisticRegression(random_state=1)

# Fitted the model using training data
lr_model = logistic_regression_model.fit(X_train, y_train)
# Made a prediction using the testing data
model_pred = lr_model.predict(X_test)
# Printed the balanced_accuracy score of the model
baso = balanced_accuracy_score(y_test, model_pred)
print(baso)
# Generated a confusion matrix for the model
test_matrix = confusion_matrix(y_test, model_pred)
print(test_matrix)
# Printed the classification report for the model
test_report = classification_report_imbalanced(y_test, model_pred)
print(test_report)<jupyter_output>pre       rec       spe        f1       geo       iba       sup

          0       1.00      0.99      0.91      1.00      0.95      0.91     18765
          1       0.85      0.91      0.99      0.88      0.95      0.90       619

avg / total       0.99      0.99      0.91      0.99      0.95      0.91     19384<jupyter_text>**Question:** How well does the logistic regression model predict both the `0` (healthy loan) and `1` (high-risk loan) labels?**Answer:**     This Logistic Regression model has a balanced accuracy score of about 95%. The recall amount for `1`(high-risk loan) is 91%, which means that 91% of the high risk loans were identified using the model. The precision of `1`(high-risk loan) is 85%, which means that in this model there is a 15% rate of false positives when identifying high risk loans.    This model is able to predict `0`(healthy loan) extreamly well with the recall at 99% which means that about 99% of the healthy loans were identified and had a percision of 100%, which means that in this model there is a 0% rate of false positives when identifying healthy loans.<jupyter_code># Instantiated the RandomOverSampler model
# # Assigned a random_state parameter of 1 to the model
random_oversampler = RandomOverSampler(random_state=1)

# Fitted the original training data to the random_oversampler model
X_resampled, y_resampled = random_oversampler.fit_resample(X_train, y_train)
# Counted the distinct values of the resampled labels data
y_resampled.value_counts()
# Instantiated the Logistic Regression model
# Assigned a random_state parameter of 1 to the model
model = LogisticRegression(random_state=1)

# Fitted the model using the resampled training data
lr_resampled_model = model.fit(X_resampled, y_resampled)

# Made a prediction using the testing data
y_resampled_pred = lr_resampled_model.predict(X_test)
# Printed the balanced_accuracy score of the model 
basr = balanced_accuracy_score(y_test, y_resampled_pred)
print(basr)
# Generated a confusion matrix for the model
resampled_matrix = confusion_matrix(y_test, y_resampled_pred)
print(resampled_matrix)
# Printed the classification report for the model
resampled_report = classification_report_imbalanced(y_test, y_resampled_pred)
print(resampled_report)<jupyter_output>pre       rec       spe        f1       geo       iba       sup

          0       1.00      0.99      0.99      1.00      0.99      0.99     18765
          1       0.84      0.99      0.99      0.91      0.99      0.99       619

avg / total       0.99      0.99      0.99      0.99      0.99      0.99     19384
{'fraction_non_alphanumeric': 0.05716652858326429, 'fraction_numerical': 0.03583264291632146, 'mean_word_length': 3.833833833833834, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '331600', 'license': 'MIT', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1406, 'n_words': 534, 'path': 'credit_risk_resampling.ipynb', 'repo_name': 'Surdano/Credit_Risk_Resampling', 'size': '4828'}