# Fake Currency Detection with Machine Learning

Fake Currency Detection is a real problem for both individuals and businesses. Counterfeiters are constantly finding new methods and techniques to produce counterfeit banknotes, which are essentially indistinguishable from real money. At least for the human eye. In this article, I will introduce you to Fake Currency Detection with Machine Learning.

Fake Currency Detection is a task of binary classification in machine learning. If we have enough data on real and fake banknotes, we can use that data to train a model that can classify the new banknotes as real or fake.

## Fake Currency Detection

The dataset I will use in this task for fake currency detection can be downloaded from here. The dataset contains these four input characteristics:

• The variance of the image transformed into wavelets
• The asymmetry of the image transformed into wavelets
• Kurtosis of the image transformed into wavelets
• Image entropy

The target value is simply 0 for real banknotes and 1 for fake banknotes. Now let’s get started with this task of Fake Currency Detection with Machine Learning. I will start this task by importing the necessary packages:

Now, let’s have a look at the dataset, the data does not have headings so I will also assign headings in the process and then I will print the first 5 rows from the data:

```       var    skew    curt     entr  auth
0  3.62160  8.6661 -2.8073 -0.44699     0
1  4.54590  8.1674 -2.4586 -1.46210     0
2  3.86600 -2.6383  1.9242  0.10645     0
3  3.45660  9.5228 -4.0112 -3.59440     0
4  0.32924 -4.4552  4.5718 -0.98880     0```

## Data Exploration

Now let’s start exploring the dataset. First, I’ll check the data types and if there are any missing values â€‹â€‹in the data:

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
`print(data.info)`Code language: CSS (css)```

We, therefore, have no missing values â€‹â€‹in the data. We can now draw a pair diagram to get an overview of the relationship between all the entities. I will also colour the observations: blue for genuine banknotes and orange for counterfeit banknotes:

``````sns.pairplot(data, hue='auth')
plt.show()
```Code language: JavaScript (javascript)```

From this pair plot we can make several interesting observations:

• The distribution of both variance and skewness appears to be quite different for the two target characteristics, while kurtosis and entropy appear to be more similar.
• There are clear linear and nonlinear trends in the input features.
• Some characteristics seem to be correlated.
• Some features seem to separate genuine and fake banknotes quite well.

Now let’s check if our data is balanced against the target values:

The dataset is fairly balanced, but for the binary classification task, we need to balance it perfectly. So let’s start preprocessing the data by doing just that.

## Data Processing

Now we need to balance our data, the easiest way to do this is to randomly drop a number of instances of the overrepresented target function. This is called random undersampling.

Otherwise, we could also create new synthetic data for the under-represented target class. This is called oversampling. For now, let’s start by randomly deleting 152 observations of actual banknotes:

```1  610
0  610
Name: auth, dtype: int64```

Now we have a perfectly balanced dataset. Next, we need to divide the data into training and test sets:

Now I will standardize the data by using the StandardScalar method provided by Scikit-learn:

## Logistic Regression for Fake Currency Detection

Now, I will train and test our model for fake currency detection by using the Logistic Regressing Algorithm. Let’s first fit the data on the Logistic Regression model to train the model:

``````clf = LogisticRegression(solver='lbfgs', random_state=42, multi_class='auto')
clf.fit(x_train, y_train.values.ravel())```Code language: JavaScript (javascript)```

Now let’s test the accuracy of our model:

```              Pred.Negative   Pred.Positive
Act.Negative     187               6
Act.Positive      0                173
Accuracy =  98.36%```

The logistic regression model achieved an accuracy of 98.36%. And not only that, when our fake currency detection model predicted that a banknote was real, it was correct 100% of the time.

Now let’s simulate the prediction of a single banknote. All we need to do is extract the features, scale them, and integrate them into our pre-trained model. We can also inspect the banknote probabilities of belonging to each target class:

`Prediction: Class0Probability [0/1]: [0.61112576 0.38887424]`

Our model predicts that this banknote is real. I hope you liked this article on fake currency detection with machine learning. Feel free to ask your valuable questions in the comments section below.

Also, Read – Python Coding Interview Tips.