Credit Card Fraud Detection In ASP.NET Core Using ML.NET

In this article, we'll learn how ML.NET framework is used to build a Credit Card Fraud Detection machine learning solution. Then we will see how to integrate that into ASP.NET Core MVC applications.

Problem

 
This problem is centered around predicting if a credit card transaction (with its related info/variables) is a fraud or not. It is important that credit card companies are able to recognize fraudulent credit card transactions so that customers are not charged for items that they did not purchase.
 
The input data set of the transaction contains only numerical input variables which are the result of a PCA transformation. Unfortunately, due to confidentiality issues, we cannot provide the original features and more background information about the data.
 
Features V1, V2, ... V28 are the principal components obtained with PCA, the only features which have not been transformed with PCA are 'Time' and 'Amount'. Feature 'Time' contains the seconds elapsed between each transaction and the first transaction in the dataset. The feature 'Amount' is the transaction Amount, this feature can be used for example-dependant cost-sensitive learning. Feature 'Class' is the response variable and it takes value 1 in case of fraud and 0 otherwise. 
 
The dataset is highly unbalanced, the positive class (frauds) account for 0.172% of all transactions. 
 

Dataset

 
The training and testing data is based on a public dataset available at Kaggle originally from Worldline and the Machine Learning Group (http://mlg.ulb.ac.be) of ULB (Université Libre de Bruxelles), collected and analyzed during a research collaboration.
 
The datasets contain transactions made by credit cards in September 2013 by European cardholders. This dataset presents transactions that occurred in two days, where we have 492 frauds out of 284,807 transactions.
 

Solution

 
Prerequisites 
  1. Visual Studio (I'm using VS2019)
  2. ML.NET Model Builder
  3. ASP.NET Core (I'm using 2.2)
  4. Credit Card Fraud Detection Dataset
Let's start.
  • Open Visual Studio and create a new project, and select ASP.NET Core. 



  • Enter the project name and click on the Create button.



  • Select ASP.NET Web Application (Model View Controller).



  • Our ASP.NET Core MVC project template has been created.

  • So first of all, we will build, train and evaluate our model using ML.NET Model Builder. 

  • Right-Click on the project and Select Add>Machine Learning.



  • ML.NET Model Builder window will be shown. Now select custom scenario because we are going to build our model from our Credit Card Fraud detection dataset.



  • We will use the Credit Card Fraud Detection data set file in .csv format. Select the dataset from your local directories where you place the .csv file then Select the column that will be used to prediction label.



  • After training dataset selection, select the Machine learning task as binary-classification because this model will tell us only the transaction is fraud or not. 

  • Select the time to train the model. Click on start training.



  • ML.NET Model builder will start training our Machine Learning Model. I've selected only 5 minutes (300 seconds). You can choose the duration of training as you want.



  • Now our model has been trained successfully. Click on the Evaluate button to evaluate the new Credit Card Fraud Detection MLModel.



  • The Evaluate window shows the performance of the best and worst algorithms on this dataset. 



  • Now, click on the Code button to consume this model into our ASP.NET Core project. When the Code window shows up, click on Add Project. This will include two newly built projects into our Visual Studio solution.



  • Our ML Modle project has been added to our Visual Studio solution. Now we will use the shown ML.NET Model builder window to consume this model.



  • So, we have built, trained, and evaluated our model successfully. Now, it is time to integrate this and consume it in our ASP.NET Core MVC project. 
  • Here is how our complete solution looks like.

     
  • Now, install Microsoft.ML in ASP.NET Core MVC project. Right-click on Dependencies and select Manage NuGet Package and install Microsoft.ML or Open NuGet Package Manager console and use this command Install-Package Microsoft.ML.

Now, we will create our User Interface to interact with the Credit Card Fraud Detection Model. So, first of all, create a new controller named FraudDetection and insert the following snippet.
  1. using System.Linq;  
  2. using Microsoft.AspNetCore.Mvc;  
  3. using CreditCardFraudDetectionML.Model;  
  4. using Microsoft.ML;  
  5.   
  6. namespace CreditCardFraudDetection.Controllers  
  7. {  
  8.     public class CreditCardController : Controller  
  9.     {  
  10.         //Dataset to use for predictions   
  11.         private const string DATA_FILEPATH = @"C:\Users\mhabi\Desktop\creditcard.csv";  
  12.         [HttpGet]  
  13.         public IActionResult FraudPrediction()  
  14.         {  
  15.             ModelInput sampleData = CreateSingleDataSample(DATA_FILEPATH);  
  16.             return View(sampleData);  
  17.         }  
  18.         [HttpPost]  
  19.         public IActionResult FraudPrediction(ModelInput input)  
  20.         {  
  21.             ModelOutput prediction = ConsumeModel.Predict(input);  
  22.             ViewBag.Prediction = prediction;  
  23.             return View();  
  24.         }  
  25.         private static ModelInput CreateSingleDataSample(string dataFilePath)  
  26.         {  
  27.             // Create MLContext  
  28.             MLContext mlContext = new MLContext();  
  29.   
  30.             // Load dataset  
  31.             IDataView dataView = mlContext.Data.LoadFromTextFile<ModelInput>(  
  32.                                             path: dataFilePath,  
  33.                                             hasHeader: true,  
  34.                                             separatorChar: ',',  
  35.                                             allowQuoting: true,  
  36.                                             allowSparse: false);  
  37.   
  38.             // Use first line of dataset as model input  
  39.             // You can replace this with new test data (hardcoded or from end-user application)  
  40.             ModelInput sampleForPrediction = mlContext.Data.CreateEnumerable<ModelInput>(dataView, false)  
  41.                                                                         .First();  
  42.             return sampleForPrediction;  
  43.         }  
  44.     }  
  45. }  
Now, we need a View that will be used to post input data from user and show output result to the user. Create a new View inside Views>CreditCard folder named FraudDetection and paste the following snippet.
  1. @model CreditCardFraudDetectionML.Model.ModelInput  
  2.   
  3. @{  
  4.     ViewData["Title"] = "FraudPrediction";  
  5. }  
  6.   
  7. <h1>Credit Card Fraud Prediction in ASP.NET Core using ML.NET</h1>  
  8.   
  9. <hr />  
  10. @if(ViewBag.Prediction != null)  
  11. {  
  12.     <div class="row">  
  13.         <div class="col-md-6">  
  14.             <h3>Prediction:@ViewBag.Prediction.Prediction</h3>  
  15.         </div>  
  16.         <div class="col-md-6">  
  17.             <h3>Score:@ViewBag.Prediction.Score</h3>  
  18.         </div>  
  19.     </div>  
  20.     <hr />  
  21. }  
  22. <div class="row">  
  23.     <div class="col-md-12">  
  24.         <form asp-action="FraudPrediction">  
  25.             <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  26.             <div class="row">  
  27.                 <div class="form-group col-md-2">  
  28.                     <label asp-for="Time" class="control-label"></label>  
  29.                     <input asp-for="Time" class="form-control" />  
  30.                     <span asp-validation-for="Time" class="text-danger"></span>  
  31.                 </div>  
  32.                 <div class="form-group col-md-2">  
  33.                     <label asp-for="V1" class="control-label"></label>  
  34.                     <input asp-for="V1" class="form-control" />  
  35.                     <span asp-validation-for="V1" class="text-danger"></span>  
  36.                 </div>  
  37.                 <div class="form-group col-md-2">  
  38.                     <label asp-for="V2" class="control-label"></label>  
  39.                     <input asp-for="V2" class="form-control" />  
  40.                     <span asp-validation-for="V2" class="text-danger"></span>  
  41.                 </div>  
  42.                 <div class="form-group col-md-2">  
  43.                     <label asp-for="V3" class="control-label"></label>  
  44.                     <input asp-for="V3" class="form-control" />  
  45.                     <span asp-validation-for="V3" class="text-danger"></span>  
  46.                 </div>  
  47.                 <div class="form-group col-md-2">  
  48.                     <label asp-for="V4" class="control-label"></label>  
  49.                     <input asp-for="V4" class="form-control" />  
  50.                     <span asp-validation-for="V4" class="text-danger"></span>  
  51.                 </div>  
  52.                 <div class="form-group col-md-2">  
  53.                     <label asp-for="V5" class="control-label"></label>  
  54.                     <input asp-for="V5" class="form-control" />  
  55.                     <span asp-validation-for="V5" class="text-danger"></span>  
  56.                 </div>  
  57.                 <div class="form-group col-md-2">  
  58.                     <label asp-for="V6" class="control-label"></label>  
  59.                     <input asp-for="V6" class="form-control" />  
  60.                     <span asp-validation-for="V6" class="text-danger"></span>  
  61.                 </div>  
  62.                 <div class="form-group col-md-2">  
  63.                     <label asp-for="V7" class="control-label"></label>  
  64.                     <input asp-for="V7" class="form-control" />  
  65.                     <span asp-validation-for="V7" class="text-danger"></span>  
  66.                 </div>  
  67.                 <div class="form-group col-md-2">  
  68.                     <label asp-for="V8" class="control-label"></label>  
  69.                     <input asp-for="V8" class="form-control" />  
  70.                     <span asp-validation-for="V8" class="text-danger"></span>  
  71.                 </div>  
  72.                 <div class="form-group col-md-2">  
  73.                     <label asp-for="V9" class="control-label"></label>  
  74.                     <input asp-for="V9" class="form-control" />  
  75.                     <span asp-validation-for="V9" class="text-danger"></span>  
  76.                 </div>  
  77.                 <div class="form-group col-md-2">  
  78.                     <label asp-for="V10" class="control-label"></label>  
  79.                     <input asp-for="V10" class="form-control" />  
  80.                     <span asp-validation-for="V10" class="text-danger"></span>  
  81.                 </div>  
  82.                 <div class="form-group col-md-2">  
  83.                     <label asp-for="V11" class="control-label"></label>  
  84.                     <input asp-for="V11" class="form-control" />  
  85.                     <span asp-validation-for="V11" class="text-danger"></span>  
  86.                 </div>  
  87.                 <div class="form-group col-md-2">  
  88.                     <label asp-for="V12" class="control-label"></label>  
  89.                     <input asp-for="V12" class="form-control" />  
  90.                     <span asp-validation-for="V12" class="text-danger"></span>  
  91.                 </div>  
  92.                 <div class="form-group col-md-2">  
  93.                     <label asp-for="V13" class="control-label"></label>  
  94.                     <input asp-for="V13" class="form-control" />  
  95.                     <span asp-validation-for="V13" class="text-danger"></span>  
  96.                 </div>  
  97.                 <div class="form-group col-md-2">  
  98.                     <label asp-for="V14" class="control-label"></label>  
  99.                     <input asp-for="V14" class="form-control" />  
  100.                     <span asp-validation-for="V14" class="text-danger"></span>  
  101.                 </div>  
  102.                 <div class="form-group col-md-2">  
  103.                     <label asp-for="V15" class="control-label"></label>  
  104.                     <input asp-for="V15" class="form-control" />  
  105.                     <span asp-validation-for="V15" class="text-danger"></span>  
  106.                 </div>  
  107.                 <div class="form-group col-md-2">  
  108.                     <label asp-for="V16" class="control-label"></label>  
  109.                     <input asp-for="V16" class="form-control" />  
  110.                     <span asp-validation-for="V16" class="text-danger"></span>  
  111.                 </div>  
  112.                 <div class="form-group col-md-2">  
  113.                     <label asp-for="V17" class="control-label"></label>  
  114.                     <input asp-for="V17" class="form-control" />  
  115.                     <span asp-validation-for="V17" class="text-danger"></span>  
  116.                 </div>  
  117.                 <div class="form-group col-md-2">  
  118.                     <label asp-for="V18" class="control-label"></label>  
  119.                     <input asp-for="V18" class="form-control" />  
  120.                     <span asp-validation-for="V18" class="text-danger"></span>  
  121.                 </div>  
  122.                 <div class="form-group col-md-2">  
  123.                     <label asp-for="V19" class="control-label"></label>  
  124.                     <input asp-for="V19" class="form-control" />  
  125.                     <span asp-validation-for="V19" class="text-danger"></span>  
  126.                 </div>  
  127.                 <div class="form-group col-md-2">  
  128.                     <label asp-for="V20" class="control-label"></label>  
  129.                     <input asp-for="V20" class="form-control" />  
  130.                     <span asp-validation-for="V20" class="text-danger"></span>  
  131.                 </div>  
  132.                 <div class="form-group col-md-2">  
  133.                     <label asp-for="V21" class="control-label"></label>  
  134.                     <input asp-for="V21" class="form-control" />  
  135.                     <span asp-validation-for="V21" class="text-danger"></span>  
  136.                 </div>  
  137.                 <div class="form-group col-md-2">  
  138.                     <label asp-for="V22" class="control-label"></label>  
  139.                     <input asp-for="V22" class="form-control" />  
  140.                     <span asp-validation-for="V22" class="text-danger"></span>  
  141.                 </div>  
  142.                 <div class="form-group col-md-2">  
  143.                     <label asp-for="V23" class="control-label"></label>  
  144.                     <input asp-for="V23" class="form-control" />  
  145.                     <span asp-validation-for="V23" class="text-danger"></span>  
  146.                 </div>  
  147.                 <div class="form-group col-md-2">  
  148.                     <label asp-for="V24" class="control-label"></label>  
  149.                     <input asp-for="V24" class="form-control" />  
  150.                     <span asp-validation-for="V24" class="text-danger"></span>  
  151.                 </div>  
  152.                 <div class="form-group col-md-2">  
  153.                     <label asp-for="V25" class="control-label"></label>  
  154.                     <input asp-for="V25" class="form-control" />  
  155.                     <span asp-validation-for="V25" class="text-danger"></span>  
  156.                 </div>  
  157.                 <div class="form-group col-md-2">  
  158.                     <label asp-for="V26" class="control-label"></label>  
  159.                     <input asp-for="V26" class="form-control" />  
  160.                     <span asp-validation-for="V26" class="text-danger"></span>  
  161.                 </div>  
  162.                 <div class="form-group col-md-2">  
  163.                     <label asp-for="V27" class="control-label"></label>  
  164.                     <input asp-for="V27" class="form-control" />  
  165.                     <span asp-validation-for="V27" class="text-danger"></span>  
  166.                 </div>  
  167.                 <div class="form-group col-md-2">  
  168.                     <label asp-for="V28" class="control-label"></label>  
  169.                     <input asp-for="V28" class="form-control" />  
  170.                     <span asp-validation-for="V28" class="text-danger"></span>  
  171.                 </div>  
  172.                 <div class="form-group col-md-2">  
  173.                     <label asp-for="Amount" class="control-label"></label>  
  174.                     <input asp-for="Amount" class="form-control" />  
  175.                     <span asp-validation-for="Amount" class="text-danger"></span>  
  176.                 </div>  
  177.             </div>  
  178.             <div class="form-group">  
  179.                 <input type="submit" value="Predict" class="btn btn-primary" />  
  180.             </div>  
  181.         </form>  
  182.     </div>  
  183. </div>  
  184.   
  185. @section Scripts {  
  186.     @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
  187. }  
Now finally, build your project and run.
 

Demo

 
 
 

Conclusion

 
So in this article, we learned how to build, train, evaluate and consume Credit Card Fraud Detection model using ML.NET Model builder and consume the resultant model into ASP.NET Core MVC application. Here is the overview.
  •  Setup a Prerequisite environment.
  • Download Credit Card Fraud Detection datasets.
  • Create ASP.NET Core MVC project template.
  • Start adding a machine learning model for our project.
  • Select Machine Learning scenario.
  • Select the dataset file and predicted column label.
  • Set training tasks and time to train the model.
  • Train model.
  • Evaluate model.
  • Add model to ASP.NET Core Project.
  • Create a User interface.
  • Consume Credit Card Fraud Detection model into ASP.NET Core MVC project.
  • Finally, build and run the project and test the application using the first record from the dataset file.
Note
In this article, we have used ML.NET Model builder to build our Credit Card Fraud Detection machine learning model.
 
You can also access the complete project source code from my GitHub repository habib-developer/Credit-Card-Fraud-Detection.
 
For more information about training a dataset, please visit kaggle Credit Card Fraud Detection repository.