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.
Just like supervised and unsupervised there are other categories of machine learning such as:
- Reinforcement Learning
- Batch Learning
- Online Learning
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:
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
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, random_state=7)
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.