Credit Card Fraud Detection using Decision Tree Classifier
Import libraries
Read the data
The data was obtained from Kaggle.
This dataset presents transactions that occurred in two days. The column Class take the values 0 an 1, 1 if there is a fraud in the transaction.
Explore
We are going to explore the dataset. First we are going to see the head of it.
We can see that there is not null values.
Now we are going to plot the Class Balance.
We have an imbalanced dataset. Our majority class is far bigger than our minority class.
Split
We are going to split the data frame in X and y and then we are going to use train test plit to obtain X train, X test, y train and y test.
Resample
We see that the data set es imbalanced.
We are going to resample the training data. We are going to cretae a new feature matrix X_train_under and target vector y_train_under by performing random under-sampling on our training data.
Now we are going to cretae a new feature matrix X_train_over and target vector y_train_over by performing random over-sampling on our training data.
Model
Baseline
We calculate the baseline accuracy score for our model.
Iterate
We build the model and fit three models. The first, model_reg, it is fit in X_train and y_train. The second, model_under, it is fit in X_train_under and y_train_under. Finally, the third, model_over, it is fit in X_train_over and y_train_over.
Evaluate
We are going to evaluate the three models.
The training and test accuracy for model_under is not good. It does not perform well. The other two models are good. These beat the baseline.
Now, we are going to plot a confusion matrix that shows how the model_over performs on our test set.
Communicate
We obtain the features and importances of our model and plot them.
We can see that the feature V14 is the one with the highest Gini Importance.