text_plain_output_1.png
from sklearn.metrics import roc_auc_score

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder

from sklearn.preprocessing import MinMaxScaler

from tensorflow.keras.callbacks import Callback

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

import tensorflow as tf

class Callback(tf.keras.callbacks.Callback):
    def __init__(self, x_train, y_train, x_val, y_val):
        self.x = x_train
        self.y = y_train
        self.x_val = x_val
        self.y_val = y_val
    def on_epoch_end(self, epoch, logs={}):
        y_pred = self.model.predict(self.x)
        roc_train = roc_auc_score(self.y, y_pred)
        y_pred_val = self.model.predict(self.x_val)
        roc_val = roc_auc_score(self.y_val, y_pred_val)
        return
encoder = LabelEncoder()
scaler = MinMaxScaler()
x = pd.read_csv('../input/loan-prediction-based-on-customer-behavior/Training Data.csv')
x = pd.concat([x.loc[x['Risk_Flag'] == 0][:30996], x.loc[x['Risk_Flag'] == 1]])
y = x.pop('Risk_Flag')
str_x = x.select_dtypes(include=[object])
for i in range(0, len(str_x.columns)):
    x.pop(str_x.columns[i])
x = scaler.fit_transform(x)
x = pd.DataFrame(x)
str_x = str_x.apply(encoder.fit_transform)
str_x = pd.DataFrame(str_x)
str_x.index = x.index
x = pd.concat([x, str_x], axis=1)
x_train, x_val, y_train, y_val = train_test_split(x, y, train_size=0.8, shuffle=True)

dense_1 = tf.keras.layers.Dense(192, activation='relu', input_dim=12)
dense_2 = tf.keras.layers.Dense(128, activation='relu')
dense_3 = tf.keras.layers.Dense(64, activation='relu')
dense_4 = tf.keras.layers.Dense(32, activation='relu')
output = tf.keras.layers.Dense(1, activation='sigmoid')
model = tf.keras.models.Sequential([dense_1, tf.keras.layers.Dropout(0.4), dense_2, tf.keras.layers.Dropout(0.2), dense_3, dense_4, output])
callback = Callback(x_train=x_train, y_train=y_train, x_val=x_val, y_val=y_val)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=75, batch_size=512, callbacks=[callback])