Fake news detection using machine learning source code

Fake news detection using machine learning source code

Sometimes, we have too much information and don’t know what is true or false. Fake news is when people lie or make mistakes on the internet. Fake news can make people confused or angry. Technology is getting better and brighter. It can help us find out what is fake and what is real. This article discusses fake news and how technology can help us stop it.
I will also provide source code for it, which you can use to make your own Fake news detection system using machine learning.

Fake News Detection Using Machine Learning

We want to know what’s real and what’s not. We have an intelligent system that can read text (like a message, tweet, or news story) and let us know how likely it is to be fake. The system is unique because it sees a lot of real and fake news from many different places and ways. Based on what it learned, the system can answer each word with either “yes” or “no.”
It can’t read words like we can. There must be numbers. So, we must find ways to turn the words into numbers. After that, we can use Naive Bayes, Logistic Regression, and Random Forests to teach the system and check its performance.
These algorithms must work better sometimes for the system to learn well. After that, we can use more complex methods like Attention or LSTM. These ways can help the computer read the words better.

Fake news detection using machine learning source code

To develop a fake news detection using machine learning, follow these steps.

Download Fake news detection datasets

Step 1: Download the dataset

This folder contains 2 datasets. One is for fake news, and the other is for actual news. The folder size is 43MB; you can download it here.

Step 2: Folder Structure

Extract both datasets and place them in the new folder. In the new folder, make a python file and open it with Vs. Code.The extenstion of file should be .py

Step 3: Import required libraries

				import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
from sklearn.metrics import accuracy_score, classification_report
			

Step 4: Load both datasets

Load both datasets using the pandas library and label them 0 and 1. For True new, label it as 0, and For Fake news, label it as 1

				# Load true news dataset
true_df = pd.read_csv('True.csv')
true_df['label'] = 0  # Add a label column for true news

# Load fake news dataset
fake_df = pd.read_csv('Fake.csv')
fake_df['label'] = 1  # Add a label column for fake news
			

Step 5: Combine both datasets

				# Combine the datasets
df = pd.concat([true_df, fake_df], ignore_index=True)
			

Step 6: Split the data into training and testing sets

				# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(df['text'], df['label'], test_size=0.2, random_state=42)

			

Step 7: Train the Model

				model = make_pipeline(TfidfVectorizer(), MultinomialNB())
model.fit(X_train, y_train)
			

Step 8: Make predictions and Evaluate the model

				# Make predictions on the test set
predictions = model.predict(X_test)

# Evaluate the model
accuracy = accuracy_score(y_test, predictions)
classification_report_result = classification_report(y_test, predictions)
			

Step 9: Display results

				# Display the result
print(f'Accuracy: {accuracy}')
print('Classification Report:\n', classification_report_result)
			

Step 10: User Input

By adding this step we can give input of our choice give input from the list of datasets it will tell if the news is fake or true

				 Take input from the user
user_input = input("Enter a news article: ")

# Make a prediction
prediction = model.predict([user_input])

# Display the result
if prediction[0] == 0:
    print("The news is likely to be true.")
else:
    print("The news is likely to be fake.")
			

Output

Fake news detection using machine learning source code

External Links for Further Exploration

While machine learning greatly improves fake news identification, complete elimination remains a difficult task. Human interaction and critical thinking are necessary when working with machine learning algorithms.

Machine learning models should be updated on a regular basis to reflect changing language trends and new methods used by fake news publishers. Continuous learning is crucial.

Yes, individuals play an important role as key users of knowledge. Reporting suspicious content and improving media literacy are helpful approaches to help struggle false news.

Final Year Project Ideas image

Final Year Projects

Data Science Project Ideas

Data Science Projects

project ideas on blockchain

Blockchain Projects

Python Project Ideas

Python Projects

CyberSecurity Projects

Cyber Security Projects

Web Development Projects

Web dev Projects

IOT Project Ideas

IOT Projects

Web Development Project Ideas

C++ Projects

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock
Scroll to Top