Passive Aggressive Classifier in Machine Learning

Passive Aggressive Classifier belongs to the category of online learning algorithms in machine learning. It works by responding as passive for correct classifications and responding as aggressive for any miscalculation. In this article, I will walk you through what Passive Aggressive Classifier is in Machine Learning and its implementation using Python.

Passive Aggressive Classifier in Machine Learning

Passive Aggressive Classifier is a classification algorithm that falls under the category of online learning in machine learning. So what is online learning? If you’ve never heard of “online learning” before, you must have heard that supervised and unsupervised are the main categories of machine learning.

Also, Read – 200+ Machine Learning Projects Solved and Explained.

Just like supervised and unsupervised there are other categories of machine learning such as:

  1. Reinforcement Learning
  2. Batch Learning
  3. Online Learning
  4. Instance-Based
  5. Model-Based

As a newbie to machine learning, you only solve problems using supervised and unsupervised learning algorithms. This is the reason why most practitioners think that supervised and unsupervised are the only categories of machine learning.

So, as mentioned above, Passive Aggressive Classifier is an online learning algorithm where you train a system incrementally by feeding it instances sequentially, individually or in small groups called mini-batches.

In online learning, a machine learning model is trained and deployed in production in a way that continues to learn as new data sets arrive. So we can say that an algorithm like Passive Aggressive Classifier is best for systems that receive data in a continuous stream.

Passive Aggressive Classifier using Python

Hope you understand what the Passive Aggressive classifier is in machine learning. Simply put, it remains passive for correct predictions and responds aggressively to incorrect predictions. Now let’s see how to implement the aggressive passive classifier using the Python programming language.

To implement the Passive Aggressive algorithm using Python, I will be using a fake news dataset where our task will be to train a model to detect fake news. I’ll start this task by importing the necessary Python libraries and the dataset:

Data Preparation:

Now let’s get the target values from the dataset and have a look at the data whether it is equally distributed or not:

0    FAKE
1    FAKE
2    REAL
3    FAKE
4    REAL
Name: label, dtype: object
REAL    3171
FAKE    3164
fake news detection: passive aggressive classifier

So as you can see that the dataset is equally distributed with Real and Fake news, let’s split the data into training and test sets:

# spliting the dataset 
from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(data['text'], 
                                                labels, test_size=0.2, 

As we are working in a dataset which contains textual data, so it is necessary to remove the stop words before training the model:

Training Passive Aggressive Algorithm:

Now let’s train the fake news detection model by using the Passive Aggressive algorithm and check the accuracy of the model:

Accuracy Score of Passive Aggresive Scassifier: 93.05%

So as you can see that we have received a good accuracy score of around 93% which is not bad. So we end this tutorial by printing the confusion matrix of our model:

print(confusion_matrix(ytest, ypred, labels=["FAKE", "REAL"]))
[[589  49]
 [ 39 590]]

So I hope you liked this article on what is Passive Aggressive algorithm in Machine Learning and its implementation using Python. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply

Data Science | Machine Learning | Python | C++ | Coding | Programming | JavaScript