Credit Card Fraud Detection using Gradient Boosting Trees
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 is imbalanced.
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. We choose over sampling because we saw in the previous project that under sampling was not good.
Model
Baseline
We calculate the baseline accuracy score for our model.
Iterate
We are going to create a model named clf, it is going to contain Gradient Boosting Classifier
Now we create a dictionary with the range of hyperparameters that we are going to evaluate for our classifier.
We create a GridSearchCV to which we called model. This is going to include clf and hyperparameter grid.
We fit model to the over-sampled training data.
Now we extract the cross-validation results from the model
Now we extract the best hyperparameters from model.
Evaluate
We are going to evaluate the model.
Calculate the training and test accuracy scores for model.
Now we make the confusion Matrix. First we count how many observation in y_test belong to positive and negative class.
Now we plot hte confusion matrix.
We can see that there are more true positives than when we used the Random Forest Classifier. In this case of projects, we need to understand what we need. If we want to reduce the false positives or the false negatives. Of this depends on which algorithm we are going to use.
Now we are going to see the precision and the recall of this model, this are going to be more important to evaluate it than the accuracy score.
Now we print the classification report for the model, using the test set.
We can see that the recall is higher than the precision for the positive class. This is because in this model the False Negatives are lower, and the True Positives are higher, for this reason recall is higher. In other hand, the False Negatives are higher, that is the reason why the precision is lower.
Finally, we are going to make a function to make predictions in which we can change the threshold. This is a parameter that affects the recall and precision.
If the threshold is high, the recall is low and the precision high. If the threshold is low, the recall is high and the precision is low.
The value we choose of threshold depends on which one we want higher, recall or precision. This is a decision we are going to take depending on for what we are going to use the results we obtain.
Communicate
We obtain the features and importances of our model and plot them.
We can see that the most importan feature is V14.