Credit Card Fraud Detection using Random Forest Classifier
Import libraries
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
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.
0
0.0
-1.3598071336738
1
0.0
1.19185711131486
2
1.0
-1.35835406159823
3
1.0
-0.966271711572087
4
2.0
-1.15823309349523
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.
X shape: (284807, 30)
y shape: (284807,)
X_train shape: (227845, 30)
y_train shape: (227845,)
X_test shape: (56962, 30)
y_test shape: (56962,)
Resample
We see that the data set es 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.
(454902, 30)
0
143352.0
1.95504092199146
1
117173.0
-0.400975238728654
2
149565.0
0.0725090163689562
3
93670.0
-0.535045380949255
4
82655.0
-4.02693795043132
Model
Baseline
We calculate the baseline accuracy score for our model.
Baseline Accuracy: 0.9983
Iterate
We are going to create a model named clf, it is going to contain Random Forest Classifier.
Pipeline(steps=[('randomforestclassifier',
RandomForestClassifier(random_state=42))])
We are going to perform cross-validation with the classifier, using the over-sampled training data.
We are going to have five folds, we set cv to 5. We want to speed up training, to set n_jobs to -1.
[0.99994504 0.99997802 0.99996703 0.99997802 0.99993405]
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 our classifier and hyperparameter grid.
We fit model to the over-sampled training data.
Fitting 5 folds for each of 12 candidates, totalling 60 fits
Now we extract the cross-validation results from the model
0
31.964545917510986
0.6569906131326068
1
64.08779926300049
0.46953656754064976
2
97.67254586219788
0.6039641121443456
3
36.319709587097165
0.7415397726699184
4
74.09811482429504
1.9280468393887165
5
111.70251822471619
1.2552862396342395
6
37.065311431884766
1.0900864845846472
7
73.0264997959137
0.9123585105225449
8
111.50762825012207
2.691989688064549
9
37.71368489265442
0.7405538741603671
10
74.7997929096222
0.9125979690728477
11
107.25660214424133
7.5852559624587155
We are going to analyze what happen with model with max depth equals 10.
We plot param_randomforestclassifier__n_estimators" on the x-axis and "mean_fit_time" on the y-axis.
We can see that when number of estimators is higher the mean fit time is higher too.
Now we are going to look at max_depth. We are going to analyze the n_estimators equals 25.
We plot "param_randomforestclassifier__max_depth" on the x-axis and "mean_fit_time" on the y-axis.
We can see that when max depth is higher, mean fit time is higher too.
Now we extract the best hyperparameters from model.
Now we are going to predict with the best model.
Evaluate
We are going to evaluate the model.
Calculate the training and test accuracy scores for model.
Training Accuracy: 1.0
Test Accuracy: 0.9996
We beat the baseline.
Now using confucion matrix we are going to see how the model performs.
First we count how many observation in y_test belong to positive and negative class.
Now we plot hte confusion matrix.
This model decrease the number of False Positives.
Communicate
We obtain the features and importances of our model and plot them.
We can see that the most importan feature is V14.