How to do it...

The model fitting process is done by following these steps (Please refer to Credit default prediction.ipynb file in GitHub while implementing the code):

history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=1024, verbose=1,class_weight = {0:1,1:100})

Note that, in the preceding code snippet, we created a dictionary with the weights that correspond to the distinct classes in output that is then passed as an input to the class_weight parameter.

The preceding step ensures that we assign a weightage of 100 to calculating the loss value when the actual outcome is 1 and a weightage of 1 when calculating the loss value when the actual outcome is 0.

The variation of accuracy and loss values over increasing epochs is as follows:

Note that the accuracy values are much lower in this iteration, as we are predicting more number of data points to have a value 1 than in the scenario of equal weightage to both classes.

Once the model is fitted, let's proceed and check for the number of actual defaulters that are captured in the top 10% of predictions, as follows:

pred = model.predict(X_test)
test_data = pd.DataFrame([y_test[:,1]]).T
test_data['pred']=pred[:,1]
test_data = test_data.reset_index(drop='index')
test_data = test_data.sort_values(by='pred',ascending=False)
test_data.columns = ['Defaultin2yrs','pred']
print(test_data[:4500]['Defaultin2yrs'].sum())

You notice that compared to the previous scenario of 1,580 customers being captured in the the top 10%, we have 1,640 customers captured in the top 10% in this scenario, and thus a better outcome for the objective we have set where we have captured 36% of all defaulters in top 10% of high probable customers in this scenario, when compared to 35% in the previous scenario.

It is not always necessary that accuracy improves as we increase class weights. Assigning class weights is a mechanism to give higher weightage to the prediction of our interest.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset