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
(
8
0
%
train,
2
0
%
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
=
5
0
)
# Instantiate a PassiveAggressiveClassifier with a maximum number of iterations of
5
0
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
*
1
0
0
,
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 in detail very big
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_2

Step: 3

blur-text-image_3

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

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions

Question

What is the specific purpose of an acceptable use policy?

Answered: 1 week ago