Giter Club home page Giter Club logo

credit-fraud-detection's Introduction

Credit Card Transaction Fraud Detection

Trained and compared 10 models to detect whether a credit card transaction is fraudulent or not.

Dataset contains 284,807 examples each with 31 features resulting in over 8,829,017 instances parsed.

** Note ** The dataset is too large to upload to github so it is left out of the repo. It can be found at: https://www.kaggle.com/mlg-ulb/creditcardfraud

Some initial data visualization

Transaction amount and time distribution

image

Box plot to visualize outliers in each class based on amount.

image

In this kernel, I train and analyze a variety of models with different pre-processing techiques. The first technique for the unbalanced dataset is Random Undersampling

Class distributions before and after random undersampling.

Before

image

After

image

Analyzing the features using a correlation matrix to see which ones are likely to be important.

image

From this correlation matrix, we can see that features V2, V4, V11, and V19 are correlated positvely and that features V10, V12, V14, and V16 are correlated negatively.

Check out the boxplots of these features.

image

We can see that these features have a great number of outliers which can inhibit our models accuracy. Some of these outliers will be removed. After calculating the interquartile range (statistical dispersion) by subtracting the 25th lower percentiles from the 75th upper percentiles (quartile75 - quartile25) I add an outlier cutoff value of 1.5 to the range. If any point is lower than the (lower quartile * cutoff), it will be removed. Similarly, if any point is greater than the (upper quartile * 1.5) it will also be removed.

Feature V2 contained the highest number of outliers at 46.

image

It is a good idea to use some clustering algorithms to indicate whether future predictive models will be accurate.

Here are 3 clustering algorithms fit onto the data

image

We see that the T-distributed stochastic neighbor embedding performs the best.

These are the learning curves of the models after optimizing their hyperparameters.

image

image

image

** Note ** how badly the Random Forest and K Nearest Neighbors classifiers overfit the data.

Displaying the ROC AUC Curves after cross validation

image

Here we see that Logistic Regression is performing best on the test data.

I will now implement the second technique - SMOTE Oversampling

After training the best logistic regression model from the previous section on the oversampled data, I obtained these results.

image

I now used TensorFlow as a backend to implement two neural networks, each with one hidden layer. The neural nets will be used to see what dataset provides better accuracy (SMOTE Oversampled vs. Random Undersampled)

Here is the accuracy on the last few epochs:

Random Undersampling

image

SMOTE Oversampling

image

We see that the SMOTE Oversampled neural network has a greater accuracy BUT also takes longer to train.

The results of the neural networks in a confusion matrix form. From the results we see that the SMOTE oversampling technique has much better results than random undersampling. The oversampled model misclassified 40 cases whereas the random undersampling misclassified 2823 cases.

image

As a final technique, I implemented a Voting Classifier along with Bagging and Pasting Ensemble Classifiers.

Here are the results, respectively:

image

image

image

Please Note that these models were originally created by myself but then used Janio Martinez's work as reference for many of the visualization techniques and process.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.