Credit risk poses a classification problem that’s inherently imbalanced. This is because healthy loans easily outnumber risky loans. In this homework, you’ll use various techniques to train and evaluate models with imbalanced classes. You’ll use 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.
Using your knowledge of the imbalanced-learn library, you’ll use a logistic regression model to compare two versions of the dataset. First, you’ll use the original dataset. Second, you’ll resample the data by using the RandomOverSampler
module from the imbalanced-learn library.
For both cases, you’ll get the count of the target classes, train a logistic regression classifier, calculate the balanced accuracy score, generate a confusion matrix, and generate a classification report.
As part of your GitHub repository’s README.md
file, you will create a credit risk analysis report based on the template provided in your Starter_Code
folder.
This homework consists of the following subsections:
-
Split the Data into Training and Testing Sets
-
Create a Logistic Regression Model with the Original Data
-
Predict a Logistic Regression Model with Resampled Training Data
-
Write a Credit Risk Analysis Report --> Credit Risk Analysis Report
Open the starter code notebook and then use it to complete the following steps.
-
Read the
lending_data.csv
data from theResources
folder into a Pandas DataFrame. -
Create the labels set (
y
) from the “loan_status” column, and then create the features (X
) DataFrame from the remaining columns.Note A value of 0 in the “loan_status” column means that the loan is healthy. A value of 1 means that the loan has a high risk of defaulting.
-
Check the balance of the labels variable (
y
) by using thevalue_counts
function. -
Split the data into training and testing datasets by using
train_test_split
.
Employ your knowledge of logistic regression to complete the following steps:
-
Fit a logistic regression model by using the training data (
X_train
andy_train
). -
Save the predictions on the testing data labels by using the testing feature data (
X_test
) and the fitted model. -
Evaluate the model’s performance by doing the following:
-
Calculate the accuracy score of the model.
-
Generate a confusion matrix.
-
Print the classification report.
-
-
Answer the following question: How well does the logistic regression model predict both the
0
(healthy loan) and1
(high-risk loan) labels?
Did you notice the small number of high-risk loan labels? Perhaps, a model that uses resampled data will perform better. You’ll thus resample the training data and then reevaluate the model. Specifically, you’ll use RandomOverSampler
.
To do so, complete the following steps:
-
Use the
RandomOverSampler
module from the imbalanced-learn library to resample the data. Be sure to confirm that the labels have an equal number of data points. -
Use the
LogisticRegression
classifier and the resampled data to fit the model and make predictions. -
Evaluate the model’s performance by doing the following:
-
Calculate the accuracy score of the model.
-
Generate a confusion matrix.
-
Print the classification report.
-
-
Answer the following question: How well does the logistic regression model, fit with oversampled data, predict both the
0
(healthy loan) and1
(high-risk loan) labels?
For this section, you’ll write a brief report that includes a summary and an analysis of the performance of both machine learning models that you used in this homework. You should write this report as the README.md
file included in your GitHub repository.
Structure your report by using the report template that Starter_Code.zip
includes, and make sure that it contains the following:
-
An overview of the analysis: Explain the purpose of this analysis.
-
The results: Using bulleted lists, describe the balanced accuracy scores and the precision and recall scores of both machine learning models.
-
A summary: Summarize the results from the machine learning models. Compare the two versions of the dataset predictions. Include your recommendation, if any, for the model to use the original vs. the resampled data. If you don’t recommend either model, justify your reasoning.
© 2022 Trilogy Education Services, a 2U, Inc. brand. All Rights Reserved.