- Mastering Predictive Analytics with scikit:learn and TensorFlow
- Alan Fontaine
- 526字
- 2021-07-23 16:42:26
Predicting a credit card dataset
Let's take an example of a credit card dataset. This dataset comes from a financial institution in Taiwan and can be found here: https://www.kaggle.com/uciml/default-of-credit-card-clients-dataset. Take a look at the following screenshot, which shows you the dataset's information and its features:
Here, we have the following detailed information about each customer:
- It contains the limit balance, that is, the credit limit provided to the customer that is using the credit card
- Then, we have a few features regarding personal information about each customer, such as gender, education, marital status, and age
- We also have a history of past payments
- We also have the bill statement's amount
- We have the history of the bill's amount and previous payment amounts from the previous month up to six months prior, which was done by the customer
With this information, we are going to predict next month's payment status of the customer. We will first do a little transformation on these features to make them easier to interpret.
In this case, the positive class will be the default, so the number 1 represents the customers that fall under the default status category and the number 0 represents the customers who have paid their credit card dues.
Now, before we start, we need to import the required libraries by running a few commands, as shown in the following code snippet:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
The following screenshot shows the line of code that was used to prepare the credit card dataset:
Let's produce the dummy feature for education in grad _school, university, and high_school. Instead of using the word sex, use the male dummy feature, and instead of using marriage, let's use the married feature. This feature is given value of 1 when the person is married, and 0 otherwise. For the pay_1 feature, we will do a little simplification process. If we see a positive number here, it means that the customer was late in his/her payments for i months. This means that this customer with an ID of 1 delayed the payment for the first two months. We can see that, 3 months ago, he/she was not delayed on his/her payments. This is what the dataset looks like:
Before fitting our models, the last thing we will do is rescale all the features because, as we can see here, we have features that are in very different scales. For example, limit_bal is in a very different scale than age.
This is why we will be using the RobustScaler method from scikit-learn—to try and transform all the features to a similar scale:
As we can see in the preceding screenshot in the last line of code, we are partitioning our dataset into a training set and a testing set and below that, the CMatrix function is used to print the confusion matrix for each model. This function is explained in the following code snippet:
def CMatrix(CM, labels=['pay', 'default']):
df = pd.DataFrame(data=CM, index=labels, columns=labels)
df.index.name='TRUE'
df.columns.name='PREDICTION'
df.loc['Total'] = df.sum()
df['Total'] = df.sum(axis=1)
return df
- Word 2000、Excel 2000、PowerPoint 2000上機指導與練習
- 火格局的時空變異及其在電網防火中的應用
- Dreamweaver CS3網頁制作融會貫通
- Learning Apache Spark 2
- 程序設計缺陷分析與實踐
- INSTANT Varnish Cache How-to
- 數據庫原理與應用技術
- 大數據平臺異常檢測分析系統的若干關鍵技術研究
- 大數據技術與應用
- 3D Printing for Architects with MakerBot
- Docker High Performance(Second Edition)
- Visual Basic.NET程序設計
- 網站入侵與腳本攻防修煉
- 三菱FX/Q系列PLC工程實例詳解
- 大數據導論