Credit Card Fraud Detection using Machine Learning from Kaggle

Krish Naik · Intermediate ·📐 ML Fundamentals ·7y ago
Skills: ML Pipelines80%

Key Takeaways

Builds a credit card fraud detection model using machine learning from a Kaggle dataset

Full Transcript

hello all today we will be doing a project which is called as credit-card anomaly detection this is a wonderful data set that is actually present in the Kaggle website so I have taken up this data set and I have actually implemented the you know the Hannah Molly detection basically to detect whether there is any fraudulent transaction credit card so the context of this particular problem is all about credit card fraud detection now over here let us just read the context it is important that credit card companies are able to recognize fraudulent credit card transactions so that the customer are not charged five items they did not purchase so this data said that you have will be you know this is the data set and you can't read the details from this content let me just go tell you or some of the information about this particular data set it contains only numerical inputs which are the result of pca transformation see they have done the pca transverse transformation of the already of the already done transaction data and the reason they have done the pca transformation is that because this data is very important and cannot be shared just like that you can see we unfortunately due to confidentiality issues we cannot provide the original features and more background information about the data so what they have done is that they just transformed that variables created new features and new features like v1 v2 will be 28 they all other principal component values with the help of ECA BC and there is also some features that are basically dependent cost sensing learnings future classing the class is basically the response variable which is basically the output and it is having one in the case of fraud otherwise if it is not one it is basically it is not fraud apart from this there are some more information from where the data set is actually collected and these are the basically the website you can go just go to this particular link and actually have more details about discussion about this particular data set so first of all what we will do is that will in order to apply anomaly detection I am going to use some techniques which is called as isolation polished algorithm and local out life for factor algorithms from boost both of this isolation forest algorithm has worked pretty much well and the grace is much more better than local outlier factor so we'll discuss about these two techniques make sure you watch this video Fillion so initially I'm just going to import all this particular library which I'm basically using I'm also importing SVM because with the help of SVM what's a try to find out how many outliers are there this is a completely imbalanced data set I'll just show you guys and even though it isn't imbalance data set this isolation forest and local outlier factorize work very very nicely and we have been able to get the exact right kind of output that we actually require so initially as usual we'll be reading the data set which is called a credit card or CSV after that we are just going to read the head part now here you have something like time variable apart from that you have the class variable class variable variable this is zero indicates you can see over here the description of the data set says that if the value is one it is basically the fraud if the value is zero it is not a fraud and it is a normal transaction so most of the data that you will be basically seeing over here is basically having zeros how I'm seeing that that is just because of some H engineering so I'm just doing data dot info and seeing some information about this particular data set and I'm just trying to check whether there is in any null values as a part of my exploratory data analysis there are no null values apart from this what I'm doing is that I'm trying to find out how many number of classes are basically there and with respect to that frequency is there and here I come to know that normal transaction of more than 25,000 oh sorry it is not netted over to 50,000 again records whereas fraudulent transaction is very very less you can see that just a small arrow or line that you see over here it is very very small amount of fraudulent run now how after seen this you can definitely see that it is an imbalance data set in this imbalance data set instead I'm just Iraqi applying algorithms like no isolation forest algorithm handler local outlet algorithm to basically solve this recognition Oh I'm doing is that wherever the value is one and taking it as our fraud data set and normal data set I'm taking zero this is just a condition then I'm just seeing the shape shape over here you can see that only 492 new records are actually having fraud and normal record some more than twenty to fifty thousand records itself then fraudulent amount description we have just seen to understand some more information and this is what it is basically having this basically helps you to see like what amount of transaction like if you are if you're doing some transaction how much amount that particular mean value was there for fraud data set on fraud transaction similarly for normal transaction what was the value then after this I'm also seeing I am using matplotlib okay a lot of math relevancy bond and trying to see um how is the transaction waves you can see for the fraud transaction the transaction war with respect to the amount right the dollars amount it was very small small small small transaction but whereas in the case of normal transaction you have you juice amounts not only smaller models what fraudulent transaction was mainly smaller amounts that you see from this particular graph and this is the code don't worry about this code I will already provide this particular code in my github or you can download it and can start exploring it and this is just by using some MATLAB and I'm just you know printing the fraud amounts and the normal amounts in the terms of histogram it's simple it's quite simple and this information basically says that the transaction amount is very small for fraud dataset okay Oh after seeing that I'm also trying to see that how many different different transaction are there for the fraud in terms of time okay now you can see that there are a lot of fraud transaction with respect to time and these are normal transaction by this we cannot explore too much so I've just drawn this particular diagram is the part of engineering then what I do is that instead of taking the whole sample a whole data and just taking a dot paper it our sample because reason I am taking it as a sample because it will take more time for pre-processing you know because there is a huge data set it has more than 280 thousand but in your case if you want to basically apply just local outlier algorithms and all you just take a small data set I'm just taking a fraction of 0.1% the whole dataset and this is what the seat now I just have 28,000 points you can take the whole amount but again I can just directly show it over here because it will take a lot of execution time what I can do is that then on this I am trying to determine how many are fraud how many are valid from data one then I'm just taking the outlier fraction so I have this fraud cases as 492 oh sorry 49 and two it for people yeah apart from that I can also do correlation and try to find out like how all the features are with respect to the class variable and they are different different colors that have come out you can go through this correlation they are having some positive and negative values oh then I create the independent and dependent features because I need to apply the model C cuz I'm not handling the imbalance data set will already be taken care by isolation forests and local outlash so here what I am doing this is my X basically will be giving my independent features Y will be speaking my dependent features I just I don't know smaller condition saying that whenever the columns column name is not having class just consider that as my dependent features otherwise whichever are having the class dispensers with that as my dependent sorry wherever there is no class as my column name take it as my independent features wherever there is a class take out my dependency so here x and y is basically created and i'm just seeing the shape over here finally model prediction simple isolation algorithm so let us just discuss about how isolation for a algorithm will be working on okay and this this isolation forest algorithm completely nique off random forests okay so if you know random forests I think you will not find much problem in understanding how does this basically work and how is the working of isolation for a cigar always remember whenever you have an outlier right in your data set right in that case you can also read it from here I've given a lot of techniques over here the main thing is basically to understand how isolation falls over here now you can see that the isolation for Shrugged listen for a cell Gotham isolates observation by randomly selecting a feature and then randomly selecting a split value between the Maxima minima splits off the selected feature the logic argument goes because of only a few condition are needed to separate those cases at the normal observation let me just provide you a brief scenario isolation are working now I hope everybody remembers random for this night you'll be doing a lot of split non-random father's uses multiple decision tree okay multiple decision tree and the division will be going on like this and this is with respect to suppose this is not decision tree one similarly I will decision tree to be for any number of missionaries isolation forests also works but in decision tree you know isolation forest also makes this kind of trees okay internally and many number of trees and for each and every split along with the needs node you know along with the leaf node sorry if not this along with this leaf node right it provides some some value and that value or some it is just like a you know it will select some value for this leaf node which will indicate which will indicate that based on the number of depth I go it based on the number of depth I go that value will be increasing okay let me just consider score some score value will be there okay now as the splitting goes on as a splitting goes acid splitting goes on along with the depth right so this core value will be increasing for the leaf nodes okay but suppose if we have an outlier remember outlier is okay outlier is a value which will not be actually nearer to the other values that I have in this leaf node it will be completely separated now see suppose if I have some data set which is populated densely okay populated then see now suppose I have some data over here so this data is basically an outlier because all the other data set is populated densely over here whereas in this case of so whenever I try to use this particular data set and start dividing my random forest decision tree attack these all values will be populated or it will be splitted quickly okay when it will be splitted quickly this core will be assigned a lower value but in this case this all values will be divided more and more and will be getting different different beliefs now and this core value increases for this okay so understand that why this particular algorithm works because when it is creating a decision tree right when it is creating a decision tree the outliers will be splitted initially at the initial depth itself because outlier is a completely different value so the root node will be selected in such a way that the outline will be splitted like suppose my all the values are over here less than hundred suppose I am just taking okay this example has it less there and there are some outliers right which is greater than funny okay now if my root nodes gets divided one will be less than hundred so all my data points will come over here which are my valid data points right will be coming over here whereas in the case of outliers which will be very very rare very very less number of values will be getting splitted now this particular score will be much more lesser than the signs for over here because this all are the depth is basically increasing in this case the outlier is rare the depth will be less you know the split will quickly get this leaf node so the score will be less now based on this particular score it will directly I understand that these are my out knives you know and then it will be able to understand how many error points are there how many outliers are so this was the basic understanding of isolation algorithm so isolation algorithm completely works on an anomaly score I was just talking about this anomaly spur here you can see I'll just read down this okay the isolation forest algorithm isolates absorption by randomly selecting a feature and then randomly selecting a split value between maximum minimum values of expected feature the logic argument those isolating anomaly observation is easier because only a few conditions are needed to separate the those cases from the normal observation because as I said that our outlier will be very rare so whenever decision trees are basically used by using a random forest on an isolation forest this will be you know this cases will be basically it can be very very easily separated from the normal that is what you have to note again I am repeating this point the logic argument goes isolating anomaly observation is easier because only a few conditions are needed to separate those cases from the normal observation because the outlines are very rare it will be separated very easily on the other hand yes - on the other hand other hand isolating normal observation require more conditions multiple if-else loops you know that is what decision tree basically uses multiple effects so this will basically take time and the score that will be getting assigned it will be based on the number of conditions required whose separate given of the observation that is basically mildly okay so that is how an isolation algorithm works there is also a different technique which is called as local outlier factor what I asked what I did for this particular problem statement I have ok now this particular thing works with it computes a local density deviation at a given data point with respect to its neighbor these considers and outlier samples that have a substantially lower density suppose my dataset has higher density over here yeah then it will consider this as normal observation suppose it has lower density somewhere like this like this then this will get treated as my outliers so that is the Phillie of phenomenon between the difference between isolation isolation polish algorithm and local outlier factor so let us just go and try to implement it now here I am creating additionally from an isolation forest I have told that I have to use N and s which signature which is my decision 3 of 100 how many samples have to use all the information is basically given for the local outlier factor I have to say that please consider n neighbors as 20 so suppose if the density is more than 20 neighbors or it is still 20 neighbors it will be considered as you know greater than 20 greater than or equal to 20 then it will be considered basically as a normal observation if it is less than 20 then that will be considered as you know and this is considered for a huge data set cancer as the data set size changes this value may change now here we are basically using the mineral Kowski metric in support vector machine this just to understand I'm also showing your support vector machine so here I've just created a digital you'll be seeing that this 2 will be outperforming the SVM that I'm using separating the Outlands because SVM it completely works on you know making a decision boundary between the points and I've also created a video on that you can go and have a look so here you can see as soon as I executed the classified this is my dictionary type and then I'm just putting a for loop and applying fit predict on X okay predict on X by this I'm getting my wife reading that's it you can see similarly for local outlier factors support vector machine and this is my else block which is math for all the for all these functionalities I'm actually checking for all this algorithms I'm checking and then I'm printing my creases for my classification report now here you can see for isolation forests I have got 73 error points that means it has determined 73 outliers okay the accuracy score is 0.997 while determining the outliers and apart from that you can also see for local outlier it has predicted 97 errors 97 outliers or anomalies you know so similarly you can see support vector machine is it's select it's you know finding 8,000 516 outliers which is actually again as I said you that isolation policy is outperforming both local outlier and there is VM so this is my observation that we found out from this particular notebook file is that this particular execution that I solution for is detected 73 error values versus local outlier factor which is detecting 97 errors versus SVM detecting eight five one six errors now this is a huge number okay now isolation policy has a ninety-nine point seven for more accurate than LOF this is my local outlier factor of ninety nine point six five and SVM here it is only giving me seventy percent LCOE a crazy score is only seventy percent to correctly determine whether it is an outlier or not okay now that outlier is basically right where it is able to determine whether it is for the reason white is not performing because first of all your dataset is not balanced and that imbalance data set may work well with isolation forests and local outlets and so because of that it is very very clear that the imbalance dataset is being handled properly and there is only something like seventy three errors whereas in case of local outlier factors it is 97 then apart from that you can also see all the other observation that I have noted over here just down this particular file you do it guys because this is the project many people who are asking for that is an anomaly detection detect whether the transaction is basically fraudulent or not based on a data set that so I hope you like this particular video guys make sure you subscribe this channel keep sharing share with all your friends whoever require this kind of cell I'll see y'all in the next video have a great day ahead or bless you all thank you money

Original Description

The Credit Card Fraud Detection Problem includes modeling past credit card transactions with the knowledge of the ones that turned out to be fraud. This model is then used to identify whether a new transaction is fraudulent or not. Github Url: https://github.com/krishnaik06/Credit-Card-Fraudlent Data Science Interview Question playlist: https://www.youtube.com/watch?v=820Qr4BH0YM&list=PLZoTAELRMXVPkl7oRvzyNnyj1HS4wt2K- Data Science Projects playlist: https://www.youtube.com/watch?v=5Txi0nHIe0o&list=PLZoTAELRMXVNUcr7osiU7CCm8hcaqSzGw NLP playlist: https://www.youtube.com/watch?v=6ZVf1jnEKGI&list=PLZoTAELRMXVMdJ5sqbCK2LiM0HhQVWNzm Statistics Playlist: https://www.youtube.com/watch?v=GGZfVeZs_v4&list=PLZoTAELRMXVMhVyr3Ri9IQ-t5QPBtxzJO Feature Engineering playlist: https://www.youtube.com/watch?v=NgoLMsaZ4HU&list=PLZoTAELRMXVPwYGE2PXD3x0bfKnR0cJjN Computer Vision playlist: https://www.youtube.com/watch?v=mT34_yu5pbg&list=PLZoTAELRMXVOIBRx0andphYJ7iakSg3Lk You can buy my book on Finance with Machine Learning and Deep Learning from the below url amazon url: https://www.amazon.in/Hands-Python-Finance-implementing-strategies/dp/1789346371/ref=sr_1_1?keywords=krish+naik&qid=1560943725&s=gateway&sr=8-1
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Krish Naik · Krish Naik · 58 of 60

1 Natural Language Processing|Stemming
Natural Language Processing|Stemming
Krish Naik
2 Natural Language Processing|BagofWords
Natural Language Processing|BagofWords
Krish Naik
3 Gaussian distribution or Normal Distribution in statisctics
Gaussian distribution or Normal Distribution in statisctics
Krish Naik
4 Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Krish Naik
5 Log Normal Distribution in Statistics
Log Normal Distribution in Statistics
Krish Naik
6 Covariance in Statistics
Covariance in Statistics
Krish Naik
7 Confusion matrix, Precision, Recall| Data Science Interview questions
Confusion matrix, Precision, Recall| Data Science Interview questions
Krish Naik
8 Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Krish Naik
9 Implementing a Spam classifier in python| Natural Language Processing
Implementing a Spam classifier in python| Natural Language Processing
Krish Naik
10 Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Krish Naik
11 Face Recognition using open CV and VGG 16 Transfer Learning
Face Recognition using open CV and VGG 16 Transfer Learning
Krish Naik
12 Pedestrian Detection using OpenCV from Videos
Pedestrian Detection using OpenCV from Videos
Krish Naik
13 Face and Eye Detection from Videos using HAAR Cascade Classifier
Face and Eye Detection from Videos using HAAR Cascade Classifier
Krish Naik
14 Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Krish Naik
15 OpenCV Installation | OpenCV tutorial
OpenCV Installation | OpenCV tutorial
Krish Naik
16 Face and Eye Detection from Images using HAAR Cascade Classifier
Face and Eye Detection from Images using HAAR Cascade Classifier
Krish Naik
17 Car Detection using HAAR Cascade and Opencv from Videos.
Car Detection using HAAR Cascade and Opencv from Videos.
Krish Naik
18 Using OpenFace for Face recognition in Keras
Using OpenFace for Face recognition in Keras
Krish Naik
19 OpenPose Tutorial with Tensorflow
OpenPose Tutorial with Tensorflow
Krish Naik
20 Multiple Linear Regression using python and sklearn
Multiple Linear Regression using python and sklearn
Krish Naik
21 Dimensional Reduction| Principal Component Analysis
Dimensional Reduction| Principal Component Analysis
Krish Naik
22 Movie Recommender System using Python
Movie Recommender System using Python
Krish Naik
23 TPR,FPR,FNR,TNR, Confusion Matrix
TPR,FPR,FNR,TNR, Confusion Matrix
Krish Naik
24 Precision, Recall and F1-Score
Precision, Recall and F1-Score
Krish Naik
25 Artificial Neural Network for Customer's Exit Prediction from Bank
Artificial Neural Network for Customer's Exit Prediction from Bank
Krish Naik
26 GridSearchCV- Select the best hyperparameter for any Classification Model
GridSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
27 RandomizedSearchCV- Select the best hyperparameter for any Classification Model
RandomizedSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
28 K Nearest Neighbor classification with Intuition and practical solution
K Nearest Neighbor classification with Intuition and practical solution
Krish Naik
29 K Means Clustering Intuition
K Means Clustering Intuition
Krish Naik
30 Create custom Alexa Skill- Lambda function- Part2
Create custom Alexa Skill- Lambda function- Part2
Krish Naik
31 Hierarchical Clustering intuition
Hierarchical Clustering intuition
Krish Naik
32 Implement Transfer Learning with a generic Code Template
Implement Transfer Learning with a generic Code Template
Krish Naik
33 Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Krish Naik
34 Unlock Your Application With Your Face using OpenCV
Unlock Your Application With Your Face using OpenCV
Krish Naik
35 Draw rectangle from webcam and sketch process it on a live feed
Draw rectangle from webcam and sketch process it on a live feed
Krish Naik
36 Complete Life Cycle of a Data Science Project
Complete Life Cycle of a Data Science Project
Krish Naik
37 How we can apply Machine Learning in Finance
How we can apply Machine Learning in Finance
Krish Naik
38 Deep Learning in Medical Science
Deep Learning in Medical Science
Krish Naik
39 How to switch your career to Data Science.
How to switch your career to Data Science.
Krish Naik
40 Linear Regression Mathematical Intuition
Linear Regression Mathematical Intuition
Krish Naik
41 Handle Categorical features using Python
Handle Categorical features using Python
Krish Naik
42 Machine Learning Algorithm- Which one to choose for your Problem?
Machine Learning Algorithm- Which one to choose for your Problem?
Krish Naik
43 DBSCAN Clustering Easily Explained with Implementation
DBSCAN Clustering Easily Explained with Implementation
Krish Naik
44 Curse of Dimensionality Easily explained| Machine Learning
Curse of Dimensionality Easily explained| Machine Learning
Krish Naik
45 Feature Selection Techniques Easily Explained | Machine Learning
Feature Selection Techniques Easily Explained | Machine Learning
Krish Naik
46 Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Krish Naik
47 Cross Validation using sklearn and python | Machine Learning
Cross Validation using sklearn and python | Machine Learning
Krish Naik
48 Handling Missing Data Easily Explained| Machine Learning
Handling Missing Data Easily Explained| Machine Learning
Krish Naik
49 Deploy Machine Learning Model using Flask
Deploy Machine Learning Model using Flask
Krish Naik
50 Deployment of Deep Learning Model using Flask
Deployment of Deep Learning Model using Flask
Krish Naik
51 How to Visualize Multiple Linear Regression in python
How to Visualize Multiple Linear Regression in python
Krish Naik
52 K Nearest Neighbour Easily Explained with Implementation
K Nearest Neighbour Easily Explained with Implementation
Krish Naik
53 Predicting Heart Disease using Machine Learning
Predicting Heart Disease using Machine Learning
Krish Naik
54 Predicting Lungs Disease using Deep Learning
Predicting Lungs Disease using Deep Learning
Krish Naik
55 Stock Sentiment Analysis using News Headlines
Stock Sentiment Analysis using News Headlines
Krish Naik
56 Random Forest(Bootstrap Aggregation) Easily Explained
Random Forest(Bootstrap Aggregation) Easily Explained
Krish Naik
57 Voting Classifier(Hard Voting and Soft Voting Classifier)
Voting Classifier(Hard Voting and Soft Voting Classifier)
Krish Naik
Credit Card Fraud Detection using Machine Learning from Kaggle
Credit Card Fraud Detection using Machine Learning from Kaggle
Krish Naik
59 Hyperparameter Optimization for Xgboost
Hyperparameter Optimization for Xgboost
Krish Naik
60 Tutorial 45-Handling imbalanced Dataset  using python- Part 1
Tutorial 45-Handling imbalanced Dataset using python- Part 1
Krish Naik

Related Reads

📰
A diagram is data, not a drawing
Learn to think of diagrams as data, not just drawings, and explore the implications for architecture and modeling
Dev.to · Siva Teja
📰
Monitor Amazon SageMaker Pipelines cross-account with custom Amazon CloudWatch dashboards
Monitor SageMaker Pipelines across AWS accounts with custom CloudWatch dashboards and learn how to centralize monitoring for improved visibility and control
AWS Machine Learning
📰
🛠️ Day 3 – Python Functions & Functional Power
Learn to create reusable and modular Python functions with flexible parameters and variable scope
Dev.to AI
📰
Your SAE Passed the Cosine Similarity Bar. That Doesn't Mean It
Learn why passing the cosine similarity bar doesn't guarantee a good sparse autoencoder (SAE) and how to audit its features
Dev.to · Mohamed Bal
Up next
Arrays vs Lists: What AI Actually Prefers | Common Tech Interview Questions
SCALER
Watch →