text_plain_output_1.png image_output_1.png
from imblearn.combine import SMOTETomek

from imblearn.ensemble import BalancedRandomForestClassifier

from imblearn.over_sampling import ADASYN

from imblearn.under_sampling import TomekLinks

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import f1_score, accuracy_score, roc_auc_score, plot_roc_curve, plot_confusion_matrix

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

import seaborn as sns

data = pd.read_csv('/kaggle/input/loan-prediction-based-on-customer-behavior/Training Data.csv')

import matplotlib.pyplot as plt
total = list(data.Risk_Flag.value_counts())
Flag0 = total[0]
Flag1 = total[1]

import seaborn as sns
g=sns.catplot(x='STATE', data=data, height=12, aspect=1.5, kind='count', palette='deep')
g.set_xticklabels(rotation=60)

import seaborn as sns
import matplotlib.pyplot as plt
plt.xticks(rotation=60)

from imblearn.ensemble import BalancedRandomForestClassifier
from sklearn.metrics import f1_score, accuracy_score, roc_auc_score, plot_roc_curve, plot_confusion_matrix

brf=BalancedRandomForestClassifier().fit(X_train, y_train)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16,6))
plt.title('asfafasf')   
ax1.set_title('Confusion matrix (Balanced RF)')
ax2.set_title('ROC curve (Balanced RF)')
ax2.plot([0,1], [0,1], 'g--', alpha=0.25)
    
plot_confusion_matrix(brf, X_test, y_test, cmap=plt.cm.Blues, normalize='true', ax=ax1)
plot_roc_curve(brf, X_test, y_test, ax=ax2)

y_pred = brf.predict(X_test)

acc_brf=accuracy_score(y_test, y_pred)
f1_brf=f1_score(y_test, y_pred)
roc_brf=roc_auc_score(y_test, y_pred)
print('Roc_Auc score: %.3f' %roc_brf)    

from imblearn.over_sampling import ADASYN
ada = ADASYN(random_state=42)
X_ada, y_ada = ada.fit_resample(X_train, y_train)

from sklearn.ensemble import RandomForestClassifier

rf_ada=RandomForestClassifier().fit(X_ada, y_ada)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16,6))  
ax1.set_title('Confusion matrix (RF and ADASYN)')
ax2.set_title('ROC curve (RF and ADASYN)')
ax2.plot([0,1], [0,1], 'g--', alpha=0.25)
    
plot_confusion_matrix(rf_ada,X_test, y_test, cmap=plt.cm.Blues, normalize='true', ax=ax1)
plot_roc_curve(rf_ada, X_test, y_test, ax=ax2)

y_pred = rf_ada.predict(X_test)

acc_ada=accuracy_score(y_test, y_pred)
f1_ada=f1_score(y_test, y_pred)
roc_ada=roc_auc_score(y_test, y_pred)
print('Roc_Auc score: %.3f' %roc_ada)    

from imblearn.combine import SMOTETomek
from imblearn.under_sampling import TomekLinks
smt = SMOTETomek(tomek=TomekLinks(sampling_strategy='majority'))
X_smt, y_smt = smt.fit_resample(X_train, y_train)

rf_smt = RandomForestClassifier().fit(X_smt, y_smt)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))
ax1.set_title('Confusion matrix (RF and SMOTETomek)')
ax2.set_title('ROC curve (RF and SMOTETomek)')
ax2.plot([0, 1], [0, 1], 'g--', alpha=0.25)
plot_confusion_matrix(rf_smt, X_test, y_test, cmap=plt.cm.Blues, normalize='true', ax=ax1)
plot_roc_curve(rf_smt, X_test, y_test, ax=ax2)
y_pred = rf_smt.predict(X_test)
acc_smt = accuracy_score(y_test, y_pred)
f1_smt = f1_score(y_test, y_pred)
roc_smt = roc_auc_score(y_test, y_pred)
print('Roc_Auc score: %.3f' % roc_smt)