<jupyter_start><jupyter_text>Credit Risk Ensemble Techniques<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<jupyter_output><empty_output><jupyter_text>Read the CSV and Perform Basic Data Cleaning<jupyter_code># Load the data
file_path = Path('02-Homework_11-Machine-Learning_Instructions_Starter_Code_Resources_lending_data.csv')
df = pd.read_csv(file_path)

df.head()<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Testing<jupyter_code># Create our features

X = pd.get_dummies(df.drop('loan_status', axis=1))

# Create our target

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

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

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test= train_test_split(X, 
                                                   y, 
                                                   random_state=1, 
                                                   stratify=y)
X_train.shape<jupyter_output><empty_output><jupyter_text>Ensemble Learners  Balanced Random Forest Classifier<jupyter_code># Resample the training data with the BalancedRandomForestClassifier
# YOUR CODE HERE

from imblearn.ensemble import BalancedRandomForestClassifier

rf=BalancedRandomForestClassifier(random_state=1,class_weight='balanced',sampling_strategy='not minority')
rf.fit(X_train, y_train)
# Calculated the balanced accuracy score
# YOUR CODE HERE

from sklearn.metrics import balanced_accuracy_score

predictions = rf.predict(X_test)
balanced_accuracy_score(y_test, predictions)
# Display the confusion matrix
# YOUR CODE HERE

from sklearn.metrics import confusion_matrix

predictions = rf.predict(X_test)
results = pd.DataFrame({"Prediction": predictions,
                        "Actual": y_test}).reset_index(drop=True)
results.head()

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

y_pred = rf.predict(X_test)
print(classification_report_imbalanced(y_test, y_pred))
#print(classification_report_imbalanced(y_test, y_pred))
# List the features sorted in descending order by feature importance
# YOUR CODE HERE

import pandas as pd
feature_importances = pd.DataFrame(rf.feature_importances_,
                                   index = X_train.columns,
                                    columns=['importance']).sort_values('importance', ascending=False)
print(feature_importances)<jupyter_output>importance
interest_rate         0.197783
debt_to_income        0.185944
borrower_income       0.185060
total_debt            0.179877
loan_size             0.148687
num_of_accounts       0.070993
derogatory_marks      0.020741
homeowner_own         0.004657
homeowner_mortgage    0.003764
homeowner_rent        0.002493<jupyter_text>Easy Ensemble Classifier<jupyter_code># Train the EasyEnsembleClassifier
# YOUR CODE HERE

from sklearn.ensemble import AdaBoostClassifier
from sklearn import datasets

adaboost = AdaBoostClassifier(n_estimators=100,
                         learning_rate=1,random_state=1)

model = adaboost.fit(X_train, y_train)

y_pred = model.predict(X_test)
# Calculated the balanced accuracy score
# YOUR CODE HERE

from sklearn.metrics import balanced_accuracy_score

balanced_accuracy_score(y_test, y_pred)
# Display the confusion matrix
# YOUR CODE HERE

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

from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

   high_risk       0.86      1.00      0.92       625
    low_risk       1.00      0.99      1.00     18759

    accuracy                           0.99     19384
   macro avg       0.93      0.99      0.96     19384
weighted avg       1.00      0.99      0.99     19384
{'fraction_non_alphanumeric': 0.06610978520286397, 'fraction_numerical': 0.034606205250596656, 'mean_word_length': 3.6105610561056105, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '250673', 'license': 'ADSL', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1259, 'n_words': 308, 'path': 'Notebooks/credit_risk_ensemble.ipynb', 'repo_name': 'RimiSharma/Machine_Learning', 'size': '4190'}