Skip to content

lotfiferaga/fake_news_detector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Documentation

# Importing necessary modules
import numpy as np
import pandas as pd
import itertools
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.metrics import accuracy_score, confusion_matrix

This section imports required libraries and modules for data manipulation, machine learning, and evaluation.

# Reading the data into a dataframe and getting the shape of the data 
# Read the data 
d = pd.read_csv('news.csv')

# Getting the shape and the head
d.shape
d.head()

This part reads a CSV file named 'news.csv' into a Pandas DataFrame (d) and displays its shape and the first few rows.

# Getting the labels from the dataframe 
labels = d.label
labels.head()

Extracts the 'label' column from the DataFrame, representing the target variable (fake or real news).

# Splitting the dataset 
x_train, x_test, y_train, y_test = train_test_split(d['text'], labels, test_size=0.2, random_state=7)

Splits the dataset into training and testing sets using the train_test_split function.

# Initializing a TfidfVectorizer 
tfidf_vectorizer = TfidfVectorizer(stop_words='english', max_df=0.7)

# Fitting and transforming the train set and testing set 
tfidf_train = tfidf_vectorizer.fit_transform(x_train)
tfidf_test = tfidf_vectorizer.transform(x_test)

Creates a TF-IDF vectorizer with English stop words and a maximum document frequency of 0.7. It then transforms the training and testing sets into TF-IDF features.

# Initializing a PassiveAggressive classifier
pac = PassiveAggressiveClassifier(max_iter=50)
pac.fit(tfidf_train, y_train)

# Predicting on the testing set and calculating accuracy 
y_predict = pac.predict(tfidf_test)
score = accuracy_score(y_test, y_predict)
print(f'Accuracy: {round(score * 100, 2)}%')

Initializes a PassiveAggressive classifier, fits it to the training data, and predicts labels for the testing set. Calculates and prints the accuracy of the model.

# Printing a confusion matrix 
confusion_matrix(y_test, y_predict, labels=['FAKE', 'REAL'])

Prints a confusion matrix for the model's predictions on the testing set, with specified class labels 'FAKE' and 'REAL'.

Releases

No releases published

Packages

No packages published

Languages