Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Credit Card Fraud Detection In ASP.NET Core Using ML.NET
WhatsApp
Habibul Rehman
5y
32.3k
0
5
100
Article
CreditCardFraudDetection.zip
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
Visual Studio (I'm using VS2019)
ML.NET Model Builder
ASP.NET Core (I'm using 2.2)
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.
using
System.Linq;
using
Microsoft.AspNetCore.Mvc;
using
CreditCardFraudDetectionML.Model;
using
Microsoft.ML;
namespace
CreditCardFraudDetection.Controllers
{
public
class
CreditCardController : Controller
{
//Dataset to use for predictions
private
const
string
DATA_FILEPATH = @
"C:\Users\mhabi\Desktop\creditcard.csv"
;
[HttpGet]
public
IActionResult FraudPrediction()
{
ModelInput sampleData = CreateSingleDataSample(DATA_FILEPATH);
return
View(sampleData);
}
[HttpPost]
public
IActionResult FraudPrediction(ModelInput input)
{
ModelOutput prediction = ConsumeModel.Predict(input);
ViewBag.Prediction = prediction;
return
View();
}
private
static
ModelInput CreateSingleDataSample(
string
dataFilePath)
{
// Create MLContext
MLContext mlContext =
new
MLContext();
// Load dataset
IDataView dataView = mlContext.Data.LoadFromTextFile<ModelInput>(
path: dataFilePath,
hasHeader:
true
,
separatorChar:
','
,
allowQuoting:
true
,
allowSparse:
false
);
// Use first line of dataset as model input
// You can replace this with new test data (hardcoded or from end-user application)
ModelInput sampleForPrediction = mlContext.Data.CreateEnumerable<ModelInput>(dataView,
false
)
.First();
return
sampleForPrediction;
}
}
}
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.
@model CreditCardFraudDetectionML.Model.ModelInput
@{
ViewData[
"Title"
] =
"FraudPrediction"
;
}
<h1>Credit Card Fraud Prediction
in
ASP.NET Core
using
ML.NET</h1>
<hr />
@
if
(ViewBag.Prediction !=
null
)
{
<div
class
=
"row"
>
<div
class
=
"col-md-6"
>
<h3>Prediction:@ViewBag.Prediction.Prediction</h3>
</div>
<div
class
=
"col-md-6"
>
<h3>Score:@ViewBag.Prediction.Score</h3>
</div>
</div>
<hr />
}
<div
class
=
"row"
>
<div
class
=
"col-md-12"
>
<form asp-action=
"FraudPrediction"
>
<div asp-validation-summary=
"ModelOnly"
class
=
"text-danger"
></div>
<div
class
=
"row"
>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"Time"
class
=
"control-label"
></label>
<input asp-
for
=
"Time"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Time"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V1"
class
=
"control-label"
></label>
<input asp-
for
=
"V1"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V1"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V2"
class
=
"control-label"
></label>
<input asp-
for
=
"V2"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V2"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V3"
class
=
"control-label"
></label>
<input asp-
for
=
"V3"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V3"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V4"
class
=
"control-label"
></label>
<input asp-
for
=
"V4"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V4"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V5"
class
=
"control-label"
></label>
<input asp-
for
=
"V5"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V5"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V6"
class
=
"control-label"
></label>
<input asp-
for
=
"V6"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V6"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V7"
class
=
"control-label"
></label>
<input asp-
for
=
"V7"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V7"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V8"
class
=
"control-label"
></label>
<input asp-
for
=
"V8"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V8"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V9"
class
=
"control-label"
></label>
<input asp-
for
=
"V9"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V9"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V10"
class
=
"control-label"
></label>
<input asp-
for
=
"V10"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V10"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V11"
class
=
"control-label"
></label>
<input asp-
for
=
"V11"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V11"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V12"
class
=
"control-label"
></label>
<input asp-
for
=
"V12"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V12"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V13"
class
=
"control-label"
></label>
<input asp-
for
=
"V13"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V13"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V14"
class
=
"control-label"
></label>
<input asp-
for
=
"V14"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V14"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V15"
class
=
"control-label"
></label>
<input asp-
for
=
"V15"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V15"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V16"
class
=
"control-label"
></label>
<input asp-
for
=
"V16"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V16"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V17"
class
=
"control-label"
></label>
<input asp-
for
=
"V17"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V17"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V18"
class
=
"control-label"
></label>
<input asp-
for
=
"V18"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V18"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V19"
class
=
"control-label"
></label>
<input asp-
for
=
"V19"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V19"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V20"
class
=
"control-label"
></label>
<input asp-
for
=
"V20"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V20"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V21"
class
=
"control-label"
></label>
<input asp-
for
=
"V21"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V21"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V22"
class
=
"control-label"
></label>
<input asp-
for
=
"V22"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V22"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V23"
class
=
"control-label"
></label>
<input asp-
for
=
"V23"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V23"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V24"
class
=
"control-label"
></label>
<input asp-
for
=
"V24"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V24"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V25"
class
=
"control-label"
></label>
<input asp-
for
=
"V25"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V25"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V26"
class
=
"control-label"
></label>
<input asp-
for
=
"V26"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V26"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V27"
class
=
"control-label"
></label>
<input asp-
for
=
"V27"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V27"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"V28"
class
=
"control-label"
></label>
<input asp-
for
=
"V28"
class
=
"form-control"
/>
<span asp-validation-
for
=
"V28"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-2"
>
<label asp-
for
=
"Amount"
class
=
"control-label"
></label>
<input asp-
for
=
"Amount"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Amount"
class
=
"text-danger"
></span>
</div>
</div>
<div
class
=
"form-group"
>
<input type=
"submit"
value=
"Predict"
class
=
"btn btn-primary"
/>
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync(
"_ValidationScriptsPartial"
);}
}
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 the
kaggle Credit Card Fraud Detection repository
.
ASP.NET Core
Machine Learning
ML.NET
Visual Studio
Up Next
Ebook Download
View all
Yatharth Machine Learning
Read by 335 people
Download Now!
Learn
View all
Finchship
We Provide Web, Desktop and Mobile Apps Solution
Membership not found