<jupyter_start><jupyter_text>Ensemble Learning Initial Imports<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.metrics import confusion_matrix
from imblearn.metrics import classification_report_imbalanced
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from imblearn.ensemble import BalancedRandomForestClassifier
from imblearn.ensemble import EasyEnsembleClassifier<jupyter_output><empty_output><jupyter_text>Read the CSV and Perform Basic Data Cleaning<jupyter_code># Load the data

file_path = Path('Resources/LoanStats_2019Q1.csv')
df = pd.read_csv(file_path)

# Preview the data
df.head()<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Testing<jupyter_code># encoding categorical data

columns = ['home_ownership', 'verification_status', 'issue_d', 'loan_status', 'pymnt_plan', 
           'initial_list_status', 'next_pymnt_d', 'application_type',  'hardship_flag', 'debt_settlement_flag']
for column in columns:
    df[column] = df[column].astype("category")
    df[column] = df[column].cat.codes
    
# Create our features
X = df.drop('loan_status', 1)

# Create our target
y = df[['loan_status']]
X
X.describe()
# Check the balance of our target values

y['loan_status'].value_counts()
# Split the X and y into X_train, X_test, y_train, y_test
# Create X_train, X_test, y_train, y_test

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)<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

scaler = StandardScaler()
# Fit the Standard Scaler with the training data

scaler = scaler.fit(X_train)
# Scale the training and testing data

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
X_train.shape<jupyter_output><empty_output><jupyter_text>Ensemble LearnersIn this section, you will compare two ensemble algorithms to determine which algorithm results in the best performance. You will train a Balanced Random Forest Classifier and an Easy Ensemble classifier . For each algorithm, be sure to complete the folliowing steps:1. Train the model using the training data. 2. Calculate the balanced accuracy score from sklearn.metrics.3. Display the confusion matrix from sklearn.metrics.4. Generate a classication report using the `imbalanced_classification_report` from imbalanced-learn.5. For the Balanced Random Forest Classifier only, print the feature importance sorted in descending order (most important feature to least important) along with the feature scoreNote: Use a random state of 1 for each algorithm to ensure consistency between tests  Balanced Random Forest Classifier<jupyter_code># Resample the training data with the BalancedRandomForestClassifier

brf = BalancedRandomForestClassifier(random_state=1)
brf.fit(X_train, y_train)
# Calculated the balanced accuracy score

y_pred = brf.predict(X_test)
balanced_accuracy_score(y_test, y_pred)
# Display the confusion matrix

confusion_matrix(y_test, y_pred)
# Print the imbalanced classification report

print(classification_report_imbalanced(y_test, y_pred))
# List the features sorted in descending order by feature importance

X.columns[np.argsort(brf.feature_importances_)[::-1]]<jupyter_output><empty_output><jupyter_text>Easy Ensemble Classifier<jupyter_code># Train the Classifier

eec = EasyEnsembleClassifier(random_state=1)
eec.fit(X_train, y_train)
# Calculated the balanced accuracy score

y_pred = eec.predict(X_test)
balanced_accuracy_score(y_test, y_pred)
# Display the confusion matrix

confusion_matrix(y_test, y_pred)
# Print the imbalanced classification report

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

          0       0.10      0.88      0.95      0.18      0.91      0.82        88
          1       1.00      0.95      0.88      0.97      0.91      0.84     13676

avg / total       0.99      0.95      0.88      0.97      0.91      0.84     13764
StarcoderdataJupyterStructuredCleanDedup