Detecting Fake News with Python and Machine Learning

What is Fake News?

A type of yellow journalism, fake news encapsulates pieces of news that may be hoaxes and is generally spread through social media and other online media. This is often done to further or impose certain ideas and is often achieved with political agendas. Such news items may contain false and/or exaggerated claims, and may end up being viralized by algorithms, and users may end up in a filter bubble.

What is a TfidfVectorizer?

TF (Term Frequency): The number of times a word appears in a document is its Term Frequency. A higher value means a term appears more often than others, and so, the document is a good match when the term is part of the search terms.

IDF (Inverse Document Frequency): Words that occur many times a document, but also occur many times in many others, may be irrelevant. IDF is a measure of how significant a term is in the entire corpus.

The TfidfVectorizer converts a collection of raw documents into a matrix of TF-IDF features.

What is a PassiveAggressiveClassifier?

Passive Aggressive algorithms are online learning algorithms. Such an algorithm remains passive for a correct classification outcome, and turns aggressive in the event of a miscalculation, updating and adjusting. Unlike most other algorithms, it does not converge. Its purpose is to make updates that correct the loss, causing very little change in the norm of the weight vector.

About Detecting Fake News with Python

This advanced python project of detecting fake news deals with fake and real news. Using sklearn, we build a TfidfVectorizer on our dataset. Then, we initialize a PassiveAggressive Classifier and fit the model. In the end, the accuracy score and the confusion matrix tell us how well our model fares.

The fake news Dataset

The dataset we’ll use for this python project- we’ll call it news.csv. The first column identifies the news, the second and third are the title and text, and the fourth column has labels denoting whether the news is REAL or FAKE.

Steps for detecting fake news with Python:

  1. Make necessary imports:

2. Now, read the data into a DataFrame, and get the shape of the data and the first 5 records.

3. And get the labels from the DataFrame.

4. Split the dataset into training and testing sets.

5. Let’s initialize a TfidfVectorizer with stop words from the English language and a maximum document frequency of 0.7 (terms with a higher document frequency will be discarded). Stop words are the most common words in a language that are to be filtered out before processing the natural language data. And a TfidfVectorizer turns a collection of raw documents into a matrix of TF-IDF features.

Now, fit and transform the vectorizer on the train set, and transform the vectorizer on the test set.

6. Next, we’ll initialize a PassiveAggressiveClassifier. This is. We’ll fit this on tfidf_train and y_train.Then, we’ll predict on the test set from the TfidfVectorizer and calculate the accuracy with accuracy_score() from sklearn.metrics.

7. We got an accuracy of 92.82% with this model. Finally, print out a confusion matrix to gain insight into the number of false and true negatives and positives.

So with this model, we have 589 true positives, 587 true negatives, 42 false positives, and 49 false negatives.




Love podcasts or audiobooks? Learn on the go with our new app.

Computer Vision with Open Source Architectures in 2020

Fake News Disambiguation: Why Is It Fake?

13 Solutions to Multi-Arm Bandit Problem for Non-Mathematicians

Review — Tensorflow Quantum(TFQ) whitepaper — Part1

From Y-DATA to ACDL 2019

At Y-DATA Demo Day: (left to right) Shlomo Kashani — project mentor, Roman, Ilia, Kostya Kilimnik — head of Y-DATA, and Tal

Shuffling Paragraphs: Using Data Augmentation in NLP to Increase Accuracy

Predicting Customer Churn with Neural Networks in Keras

[ Archived Post ] Identifying and attacking the saddle point problem in high-dimensional…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rutvi Pan

Rutvi Pan

More from Medium

Protecting Your Identity and Data In The Decentralized World.

Detecting bias in the Swedish language using NLP machine learning models

Natural Language Processing for Fake News Detection

Critical analysis with