<jupyter_start><jupyter_text>Credit Risk ClassificationCredit risk poses a classification problem that’s inherently imbalanced. This is because healthy loans easily outnumber risky loans. In this analysis, two machine learning techniques will be compared on a historical data set for lending activity from a peer-to-peer lending services company with the goal of finding which model is better for identifying creditworthiness of borrowers.The two machine learning methods compared will both use logistical regression on randomly split versions of the original data, with training sets and testing sets. The training set will be used to train the machine learning model, and then the model will be applied to the testing set to see how well it performs in terms of percent of correctly identified high risk loans, and percent of incorrectly identified high risk loans. Since the amount of high risk loans is a small portion of the dataset, a second model will be run using an imbalanced learning approach that oversamples the amount of high risk loans, and the results will be compared. Credit Risk Analysis ReportA report with analysis of the results is included in the README.md file, which is also on the front page of the github site for this project.[https://github.com/phodsman/P2P-Lending-Credit-Risk-Classifier-with-Machine-Learning](https://github.com/phodsman/P2P-Lending-Credit-Risk-Classifier-with-Machine-Learning)<jupyter_code># Import 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 imblearn.metrics import classification_report_imbalanced
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from imblearn.over_sampling import RandomOverSampler

import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>---  Split the Data into Training and Testing Sets<jupyter_code># 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
display(lending_df)
# Separate the data into labels and features

# Separate the y variable, the labels
y = lending_df["loan_status"]

# Separate the X variable, the features
X = lending_df.drop(columns=["loan_status"])
# Review the y variable Series
y.head()
# Review the X variable DataFrame
X.head()
# Check the balance of our target values
y.value_counts()
# Split the data using train_test_split
# Assign a random_state of 1 to the function
X_train, X_test, y_train, y_test = train_test_split(X,y, random_state=1)<jupyter_output><empty_output><jupyter_text>---  Logistic Regression Model with the Original Data<jupyter_code># Instantiate the Logistic Regression model
# Assign a random_state parameter of 1 to the model
logistic_regression_model = LogisticRegression(random_state=1)
# Fit the model using training data
lr_original_model = logistic_regression_model.fit(X_train, y_train)
# Make a prediction using the testing data
y_original_pred = lr_original_model.predict(X_test)<jupyter_output><empty_output><jupyter_text>Evaluate the model’s performance:* Calculate the accuracy score of the model.* Generate a confusion matrix.* Print the classification report.<jupyter_code># Print the balanced_accuracy score of the model
print(balanced_accuracy_score(y_test, y_original_pred))
# Generate a confusion matrix for the model
print(confusion_matrix(y_test, y_original_pred))
# Print the classification report for the model
print(classification_report_imbalanced(y_test, y_original_pred))<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?**Analysis:** The recall amount is 91%, which means about 91% of the actual high risk loans were identified using the model. The precision is 85%, which means that there is around a 15% rate of false positives in identifying high risk loans with this model. ---  Logistic Regression Model with Resampled Training Data<jupyter_code># Instantiate the random oversampler model
# # Assign a random_state parameter of 1 to the model
random_oversampler = RandomOverSampler(random_state=1)

# Fit the original training data to the random_oversampler model
X_resampled, y_resampled = random_oversampler.fit_resample(X_train, y_train)
# Count the distinct values of the resampled labels data
y_resampled.value_counts()
# Instantiate the Logistic Regression model
# Assign a random_state parameter of 1 to the model
model = LogisticRegression(random_state=1
                          )
# Fit the model using the resampled training data
lr_resampled_model = model.fit(X_resampled, y_resampled)

# Make a prediction using the testing data
y_resampled_pred = lr_resampled_model.predict(X_test)<jupyter_output><empty_output><jupyter_text>Evaluate the model’s performance:* Calculate the accuracy score of the model.* Generate a confusion matrix.* Print the classification report.<jupyter_code># Print the balanced_accuracy score of the model 
print(balanced_accuracy_score(y_test, y_resampled_pred))
# Generate a confusion matrix for the model
print(confusion_matrix(y_test, y_resampled_pred))
# Print the classification report for the model
print(classification_report_imbalanced(y_test, y_resampled_pred))<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.05674110835401158, 'fraction_numerical': 0.026468155500413565, 'mean_word_length': 4.10210970464135, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '511322', 'license': 'MIT', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1668, 'n_words': 665, 'path': 'credit_risk_resampling.ipynb', 'repo_name': 'phodsman/P2P-Lending-Credit-Risk-Classifier-with-Machine-Learning', 'size': '6045'}