Confusion Matrix & Cyber Crime

Manav Misra
6 min readJun 2, 2021

--

What is the Confusion Matrix and Why it is used?

It is a performance metric used in classification problems where the output classes maybe two or more and this matrix represents all the combinations of predicted values by a model with actual values associated with that input. It’s called ‘confusion matrix’ because going by the definitions it seems easy, but as we move forward to derive more valuable parameters, confusion arises regarding which parameter is best suited at a particular place. It is used in places where the classification problem is highly imbalanced and one class dominates over other classes. In such scenarios, you may be surprised to see the accuracy of the model peaking at 99% but in reality, the model is highly biased towards the dominant class. There is very little possibility that you will get predictions for minority classes. Therefore, to test such an imbalanced dataset, we consider the confusion matrix.

Structure of the Confusion Matrix

The size of the matrix is directly proportional to the number of output classes. It is a square matrix where we assume the column headers as actual values and the row headers as model predictions. The values which are true and predicted true by the model are True Positives (TP), correct negative value predictions are True Negatives (TN), values which were negative but predicted as true are False Positives (FP) and positive values predicted as negative are False Negatives (FN). Have a look at this image:

What can we learn from this?

A valid question arises that what we can do with this matrix. There are some important terminologies based on this:

  1. Precision: It is the portion of values that are identified by the model as correct and are relevant to the problem statement solution. We can also quote this as values, which are a portion of the total positive results given by the model and are positive. Therefore, we can give its formula as TP/ (TP + FP).
  2. Recall: It is the portion of values that are correctly identified as positive by the model. It is also termed as True Positive Rate or Sensitivity. Its formula comes out to be TP/ (TP+FN).
  3. F-1 Score: It is the harmonic mean of Precision and Recall. It means that if we were to compare two models, then this metric will suppress the extreme values and consider both False Positives and False Negatives at the same time. It can be quoted as 2*Precision*Recall/ (Precision+Recall).
  4. Accuracy: It is the portion of values that are identified correctly irrespective of whether they are positives or negatives. It means that all True positives and True negatives are included in this. The formula for this is (TP+TN)/ (TP+TN+FP+FN).

Out of all the terms, precision and recall are most widely used. Their tradeoff is a useful measure of the success of a prediction. The desired model is supposed to have high precision and high recall, but this is only in perfectly separable data. In practical use cases, the data is highly unorganized and imbalanced.

How to create code for Confusion Matrix in Python?

The sklearn library provides a variety of functionalities to perform all the machine learning tasks with utmost accuracy and almost everything has been implemented here. Consider the famous Iris dataset with all import statements already done, the code for confusion matrix would be:

iris = datasets.load_iris()

X = iris.data

y = iris.target

class_names = iris.target_names

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

classifier = svm.SVC(kernel=’linear’, C=0.01).fit(X_train, y_train)

plot_confusion_matrix(classifier, X_test, y_test,display_labels=class_names,cmap=plt.cm.Blues)

Note: The matrix returned by this has reversed sides, here on the left we have actual values and on the top, we have predicted values. If you want to avoid confusion, execute this function to get a detailed summary (classification report) instead of calculating it manually:

print(classification_report(y_true=y_test, y_pred=y_pred, target_names=class_names))

Which one to use and where?

This is the most common question that arises while modeling the Data and the solution lies in the problem’s statement domain. Consider these two cases:

1. Suppose you are predicting whether the person will get a cardiac arrest. In this scenario, you can’t afford any misclassification and all the predictions made should be accurate. With that said, the cost of False Negatives is high, so the person was prone to attack but was predicted as safe. These cases should be avoided. In these situations, we need a model with high recall.

2. Suppose a search engine provided random results that are all predicted as positive by the model, then there is very little possibility that the user will rely on it. Therefore, in this scenario, we need a model with high precision so that user experience improves, and the website grows in the right direction.

CYBER CRIME CASES AND CONFUSION MATRIX:

Especially in the last ten years, the use of the Internet has been growing rapidly. However, as the Internet becomes part of daily activities, cyber crime is also increasing. According to the 2020 Cybersecurity Investment Report, by 2021, cybercrime will cost nearly US$6 trillion each year. For illegal activities, cybercriminals use any network computing device as the main means of communicating with the victim’s device, so the attacker can use the loopholes in the system to conduct propaganda and other activities from finances. Cyber ​​crime is steadily increasing every day. Using existing technology methods to assess cybercrime attacks and provide protection measures through manual methods, and investigations often fail to control cybercrime attacks.

The existing literature in the field of cybercrime lacks computational methods to predict cybercrime, especially in terms of unstructured data. Therefore, this research proposes a flexible calculation tool that uses machine learning technology to analyze the incidence of cybercrime in a country/region that helps to classify cybercrime. The association of security analysis and data analysis methods helps us analyze and classify crimes from integrated data (which may be structured or unstructured) from India. The main advantage of this work is the test analysis report, which can accurately classify crimes with 99% accuracy.

Still, have a query, feel free to ask in the comment box.

Thank you !.. if you like this article then please share this post with your fellow tech enthusiast.

You can also follow me on my linked in profile by clicking on following link.

--

--