Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import numpy as np #machine learning tool used for efficient array processing import pandas as pd #machine learning tool used for data sets and data

import numpy as np #machine learning tool used for efficient array processing
import pandas as pd #machine learning tool used for data sets and data frames
from sklearn.model_selection import train_test_split #traditional machine learning
from sklearn.feature_extraction.text import TfidfVectorizer#text is converted into vectrorizeor (numbers) to feed into computer
#tf-how much times a term is repeated,idf-inverse documentry frequency-no of documents/no of documents has the term
from sklearn.linear_model import PassiveAggressiveClassifier # this is for text classification
from sklearn.metrics import accuracy_score, confusion_matrix #for result
# Read the data
df = pd.read_csv('/content/fake_or_real_news.csv') #reading the data and lebelling them,for accuracy
# Get shape and head
print(df.shape)#This line prints the shape of the DataFrame df, which represents the number of rows and columns in the DataFrame.
print(df.head())# This line prints the first few rows of the DataFrame df. By default, it prints the first 5 rows
#DataFlair - Get the labels
labels=df.label
labels.head()
class TextClassification:
def __init__(self, df, labels):#here we split the data into train and test so that we can see the accurcy
self.df = df #df (pandas.DataFrame):The DataFrame containing the text data and labels.
self.labels = labels #The Series containing the labels (target variable)
self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(df['text'], labels, test_size=0.2, random_state=7)# Split data into training and testing sets (80% train, 20% test)
self.tfidf_vectorizer = TfidfVectorizer(stop_words='english', max_df=0.7)# Create a TF-IDF vectorizer with English stop words removed and a maximum document frequency threshold of 0.7
self.tfidf_train = None
self.tfidf_test = None
self.pac = PassiveAggressiveClassifier(max_iter=50)# Instantiate a PassiveAggressiveClassifier with a maximum number of iterations of 50
def preprocess_data(self):#Preprocesses the text data using TF-IDF vectorization.
self.tfidf_train = self.tfidf_vectorizer.fit_transform(self.x_train)
self.tfidf_test = self.tfidf_vectorizer.transform(self.x_test)
def train_model(self):#Trains the text classification model using the PassiveAggressiveClassifier.
self.pac.fit(self.tfidf_train, self.y_train)
def evaluate_model(self):#Evaluates the trained model's performance using accuracy and confusion matrix.
y_pred = self.pac.predict(self.tfidf_test)
score = accuracy_score(self.y_test, y_pred)
print(f'Accuracy: {round(score*100,2)}%')
confusion_mat = confusion_matrix(self.y_test, y_pred, labels=['FAKE', 'REAL'])
print("Confusion Matrix:")
print(confusion_mat)
if __name__=="__main__":
# Sample usage
df = pd.read_csv('/content/fake_or_real_news.csv')
labels = df['label']
# Create an instance of TextClassification
classifier = TextClassification(df, labels)
# Preprocess the data
classifier.preprocess_data()
# Train the model
classifier.train_model()
# Evaluate the model
classifier.evaluate_model()..this is code and output screenshot for fake news detection using python pls do provide with detailed ellaborate content for Abstract
Introduction
Methodology
Results (Results Screenshot)
Conclusion for this project
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions