Fake News Detection Project in Python [With Coding]

Ever read a piece of news which just seems bogus? We all encounter such news articles, and instinctively recognise that something doesn’t feel right. Because of so many posts out there, it is nearly impossible to separate the right from the wrong. Here, we are not only talking about spurious claims and the factual points, but rather, the things which look wrong intricately in the language itself. 

Did you ever wonder how to develop a fake news detection project? But there is no easy way out to find which news is fake and which is not, especially these days, with the speed of spread of news on social media. Still, some solutions could help out in identifying these wrongdoings. 

No Coding Experience Required. 360° Career support. PG Diploma in Machine Learning & AI from IIIT-B and upGrad.

There are two ways of claiming that some news is fake or not: First, an attack on the factual points. Second, the language. The former can only be done through substantial searches into the internet with automated query systems. It could be an overwhelming task, especially for someone who is just getting started with data science and natural language processing. 

The latter is possible through a natural language processing pipeline followed by a machine learning pipeline. It is how we would implement our fake news detection project in Python. It is another one of the problems that are recognized as a machine learning problem posed as a natural language processing problem. 

Read: Python Project Ideas & Topics

There are many datasets out there for this type of application, but we would be using the one mentioned here. The data contains about 7500+ news feeds with two target labels: fake or real. The dataset also consists of the title of the specific news piece. 

The steps in the pipeline for natural language processing would be as follows:

  1.     Acquiring and loading the data
  2.     Cleaning the dataset
  3.     Removing extra symbols 
  4.     Removing punctuations
  5.     Removing the stopwords
  6.     Stemming
  7.     Tokenization
  8.     Feature extractions
  9.     TF-IDF vectorizer
  10.     Counter vectorizer with TF-IDF transformer
  11.     Machine learning model training and verification

Before we start discussing the implementation steps of the fake news detection project, let us import the necessary libraries:


import pandas as pd
import numpy as np
import re
import string
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

stop_words = set(stopwords.words(‘english’))


The first step is to acquire the data. We have already provided the link to the CSV file; but, it is also crucial to discuss the other way to generate your data. 

One of the methods is web scraping. For this, we need to code a web crawler and specify the sites from which you need to get the data. But be careful, there are two problems with this approach. 

First, it may be illegal to scrap many sites, so you need to take care of that. And second, the data would be very raw. The whole pipeline would be appended with a list of steps to convert that raw data into a workable CSV file or dataset. Hence, we use the pre-set CSV file with organised data.

However, if interested, you can check out upGrad’s course on Data science, in which there are enough resources available with proper explanations on Data engineering and web scraping. Do make sure to check those out here.

It is how we import our dataset and append the labels. Do note how we drop the unnecessary columns from the dataset. If required on a higher value, you can keep those columns up. But right now, our fake news detection project would work smoothly on just the text and target label columns. 


df_text = pd.read_csv(‘fake_or_real_news.csv’, encoding=‘latin-1’)
df_text.columns = [‘id’, ‘title’, ‘text’, ‘label’]df_text.drop([‘id’, ‘title’], axis=1)


Moving on, the next step is to clean the existing data. Why is this step necessary? It is crucial to understand that we are working with a machine and teaching it to bifurcate the fake and the real. Right now, we have textual data, but computers work on numbers. So first is required to convert them to numbers, and a step before that is to make sure we are only transforming those texts which are necessary for the understanding. 

The first step in the cleaning pipeline is to check if the dataset contains any extra symbols to clear away. It could be web addresses or any of the other referencing symbol(s), like at(@) or hashtags. Here is the code:


# Remove urls
    text = re.sub(r”http\S+|www\S+|https\S+”, , text, flags=re.MULTILINE)
    # Remove user @ references and ‘#’ from text
    text = re.sub(r’\@\w+|\#’,, text)

Once we remove that, the next step is to clear away the other symbols: the punctuations. If we think about it, the punctuations have no clear input in understanding the reality of particular news. Sometimes, it may be possible that if there are a lot of punctuations, then the news is not real, for example, overuse of exclamations.

But those are rare cases and would require specific rule-based analysis. So, for this fake news detection project, we would be removing the punctuations. Here is how to do it:


text = text.translate(str.maketrans(, , string.punctuation))

The next step is to stem the word to its core and tokenize the words. Tokenization means to make every sentence into a list of words or tokens. Here is a two-line code which needs to be appended:


tokens = word_tokenize(text)
words = [w for w in tokens if not w in stop_words]

The next step is a crucial one. The conversion of tokens into meaningful numbers. This step is also known as feature extraction. For our application, we are going with the TF-IDF method to extract and build the features for our machine learning pipeline. 

TF-IDF essentially means term frequency-inverse document frequency. As suggested by the name, we scoop the information about the dataset via its frequency of terms as well as the frequency of terms in the entire dataset, or collection of documents. 

TF-IDF can easily be calculated by mixing both values of TF and IDF. Both formulas involve simple ratios. 

TF = no. of times the term appears in the document / total number of terms.

IDF = log of ( total no. of documents / no. of documents in which the term appears )

Now Python has two implementations for the TF-IDF conversion. First is a TF-IDF vectoriser and second is the TF-IDF transformer. The difference is that the transformer requires a bag-of-words implementation before the transformation, while the vectoriser combines both the steps into one. We could also use the count vectoriser that is a simple implementation of bag-of-words. But the TF-IDF would work better on the particular dataset. 

Here is how TF-IDF could be implemented. 


from sklearn.feature_extraction.text import TfidfVectorizer

tf_vector = TfidfVectorizer(sublinear_tf=True)[‘text’])

The next step is the Machine learning pipeline. Just like the typical ML pipeline, we need to get the data into X and y. To do so, we use X as the matrix provided as an output by the TF-IDF vectoriser, which needs to be flattened.

We can use the travel function in Python to convert the matrix into an array. The y values cannot be directly appended as they are still labels and not numbers. To convert them to 0s and 1s, we use sklearn’s label encoder. 

Data Science Advanced Certification, 250+ Hiring Partners, 300+ Hours of Learning, 0% EMI

This encoder transforms the label texts into numbered targets. For example, assume that we have a list of labels like this: [‘real’, ‘fake’, ‘fake’, ‘fake’]

What we essentially require is a list like this: [1, 0, 0, 0]. What label encoder does is, it takes all the distinct labels and makes a list. For our example, the list would be [‘fake’, ‘real’]. 

Here is the implementation of the same:


X_text = tf_vector.transform(df_text[‘text’].ravel())
y_values = np.array(df_text[‘label’].ravel())


from sklearn import preprocessing
le = preprocessing.LabelEncoder()

Once done, the training and testing splits are done. It can be achieved by using sklearn’s preprocessing package and importing the train test split function. 


from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X_text, y_values, test_size=0.15, random_state=120)

The final step is to use the models. There are many good machine learning models available, but even the simple base models would work well on our implementation of fake news detection projects. We first implement a logistic regression model. Here is how to implement using sklearn. 


from sklearn.metrics import accuracy_score

from sklearn.linear_model import LogisticRegression

model = LogisticRegression(solver=‘lbfgs’), y_train)
y_predict = model.predict(X_test)
print(accuracy_score(y_test, y_predict))

The model performs pretty well. A 92 percent accuracy on a regression model is pretty decent. You can also implement other models available and check the accuracies. 

So, this is how you can implement a fake news detection project using Python. 

Note that there are many things to do here. The NLP pipeline is not yet fully complete. There are many other functions available which can be applied to get even better feature extractions. 

Also Read: Python Open Source Project Ideas

The way fake news is adapting technology, better and better processing models would be required. And these models would be more into natural language understanding and less posed as a machine learning model itself. The models can also be fine-tuned according to the features used. The dataset could be made dynamically adaptable to make it work on current data. But that would require a model exhaustively trained on the current news articles. 

So, if more data is available, better models could be made and the applicability of fake news detection projects can be improved. But the internal scheme and core pipelines would remain the same. The pipelines explained are highly adaptable to any experiments you may want to conduct. Feel free to try out and play with different functions.

If you are curious about learning data science to be in the front of fast-paced technological advancements, check out upGrad & IIIT-B’s PG Diploma in Data Science and upskill yourself for the future.

Prepare for a Career of the Future

Learn More

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Data Science Course