<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<jupyter_output><empty_output><jupyter_text>Read the CSV into DataFrame<jupyter_code># Load the data
file_path = Path('Resources/lending_data.csv')
df = pd.read_csv(file_path)
df.head()
# # The homeowner column needs to be encoded to facilitate the analysis
# # The LabelEncoder from sklearn will be used to ferform integer encoding of the homeowner column

# # Import LabelEncoder from sklearn
# from sklearn.preprocessing import LabelEncoder

# # Create the LabelEncoder instance
# le = LabelEncoder()
# # Fitting and encoding the homeowner column with the LabelEncoder
# le.fit(df["homeowner"])
# df["homeowner"] = le.transform(df["homeowner"])

# df.head()

# # The dummies created for homeowner are: "1" for own, "2" for rent and "0" for mortgage
# The homeowner column needs to be encoded to facilitate the analysis
# The get_dummies() function from Pandas will be used to perform integer encoding of the homeowner column

df = pd.get_dummies(df, columns=["homeowner"])
df.head()
# The loan_status column needs to be encoded to facilitate the analysis
# The LabelEncoder from sklearn will be used to ferform integer encoding of the loan_status column

# Import LabelEncoder from sklearn
from sklearn.preprocessing import LabelEncoder

# Create the LabelEncoder instance
le = LabelEncoder()
# Fitting and encoding the loan_status column with the LabelEncoder
le.fit(df["loan_status"])
df["loan_status"] = le.transform(df["loan_status"])

df.head()

# The dummies created for homeowner are: "1" for own, "2" for rent and "0" for mortgage<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Testing<jupyter_code># # Create our features
# X = # YOUR CODE HERE

# # Create our target
# y = # YOUR CODE HERE
# FV
x_cols = [i for i in df.columns if i not in ("loan_status")]

# Create our features
X = df[x_cols]

# Create our target
y = df["loan_status"]


# X.describe()
# FV

X.describe()
# # Check the balance of our target values
# y['loan_status'].value_counts()
# FV
# Check the balance of our target values
Counter(y)
# FV
# Check the balance of our target values
y.value_counts()
# FV
# Check the balance of our target values
y.describe()
# Create X_train, X_test, y_train, y_test
# YOUR CODE HERE
# FV
# Create X_train, X_test, y_train, y_test

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)


Counter(y_train)   # This indicates that low_risk is "1" and high_risk is "0"
Counter(y_test)   # This indicates that low_risk is "1" and high_risk is "0"<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
from sklearn.preprocessing import StandardScaler
# YOUR CODE HERE
# FV
# Create the StandardScaler instance
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
# Fit the Standard Scaler with the training data
# When fitting scaling functions, only train on the training dataset
# YOUR CODE HERE
# FV
# Fit the Standard Scaler with the training data
# When fitting scaling functions, only train on the training dataset

X_scaler = scaler.fit(X_train)
# Scale the training and testing data
# YOUR CODE HERE
# FV
# Scale the training and testing data
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Simple Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
model = LogisticRegression(solver='lbfgs', random_state=1)
model.fit(X_train, y_train)
# FV
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(solver='lbfgs', random_state=1)
model.fit(X_train, y_train)
# Calculated the balanced accuracy score
from sklearn.metrics import balanced_accuracy_score
y_pred = model.predict(X_test)
balanced_accuracy_score(y_test, y_pred)
# FV 
# Calculate the balanced accuracy score
from sklearn.metrics import balanced_accuracy_score
y_pred = model.predict(X_test)
balanced_acc_score = balanced_accuracy_score(y_test, y_pred)
# Display the balanced accuracy score
print(f"Balanced Accuracy Score : {balanced_acc_score}")
# Display the confusion matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)
# FV
# Display the confusion matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)
# FV
# Confusion matrix
from sklearn.metrics import confusion_matrix

# Calculating 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"]
)

# Displaying results
print("Confusion Matrix")
display(cm_df)
# Based on the information from the counter: Low risk is "1" and high risk is "0".
# Displaying results
print("Confusion Matrix")
display(cm_df)
# Based on the information from the counter: Low risk is "0" and high risk is "1".
# Print the imbalanced classification report
from imblearn.metrics import classification_report_imbalanced
print(classification_report_imbalanced(y_test, y_pred))
Counter(y_test)
Counter(y_train)
pd.DataFrame({"Prediction": y_pred, "Actual": y_test})<jupyter_output><empty_output><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. Print 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
# YOUR CODE HERE

# View the count of target classes with Counter
# YOUR CODE HERE
# Train the Logistic Regression model using the resampled data
# YOUR CODE HERE
# Calculated the balanced accuracy score
# YOUR CODE HERE
# Display the confusion matrix
# YOUR CODE HERE
# Print the imbalanced classification report
# YOUR CODE HERE<jupyter_output><empty_output><jupyter_text>SMOTE Oversampling<jupyter_code># Resample the training data with SMOTE
# YOUR CODE HERE

# View the count of target classes with Counter
# YOUR CODE HERE
# Train the Logistic Regression model using the resampled data
# YOUR CODE HERE
# Calculated the balanced accuracy score
# YOUR CODE HERE
# Display the confusion matrix
# YOUR CODE HERE
# Print the imbalanced classification report
# YOUR CODE HERE<jupyter_output><empty_output><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
# YOUR CODE HERE

# View the count of target classes with Counter
# YOUR CODE HERE
# Train the Logistic Regression model using the resampled data
# YOUR CODE HERE
# Calculate the balanced accuracy score
# YOUR CODE HERE
# Display the confusion matrix
# YOUR CODE HERE
# Print the imbalanced classification report
# YOUR CODE HERE<jupyter_output><empty_output><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
# YOUR CODE HERE

# View the count of target classes with Counter
# YOUR CODE HERE
# Train the Logistic Regression model using the resampled data
# YOUR CODE HERE
# Calculate the balanced accuracy score
# YOUR CODE HERE
# Display the confusion matrix
# YOUR CODE HERE
# Print the imbalanced classification report
# YOUR CODE HERE<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06301824212271974, 'fraction_numerical': 0.00404228855721393, 'mean_word_length': 5.8142655367231635, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '296216', 'license': 'ADSL', 'n_tokens_mistral': 2817, 'n_tokens_neox': 2667, 'n_words': 1137, 'path': 'Starter_Code/.ipynb_checkpoints/Dummy_risk_credit_risk_resampling-checkpoint.ipynb', 'repo_name': 'FranklinFV/Machine_learning_project', 'size': '9648'}