Fack News Detection Using Machine Learning
In this project, we will do the fack News Detection.
First import all the related libraries then load datasets. Fake news encapsulates pieces of news that may be hoaxes and is generally spread through social media and other online media.
Such news items may contain false and/or exaggerated claims and may end up being virtualized by algorithms, and users may end up in a filter bubble.
First Launch the Jupyter notebook
Install all the related packages like numpy, pandas and sklearn.
#Intall packages
pip install numpy pandas sklearn
Load the related libraries:
#import all the related libraries
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
Load the dataset
#Read the data
df=pd.read_csv('news.csv')
#Get shape and head
df.shape
df.head()
Now data is loaded successfully after this split the dataset into train and test.
#split the dataset
x_train,x_test,y_train,y_test=train_test_split(df['text'], labels, test_size=0.2, random_state=7)
Use TfidfVectorizer to change non-numeric data into the numeric form
#TfidfVectorizer to change non-numeric to numeric form
tfidf_vectorizer=TfidfVectorizer(stop_words='english', max_df=0.7)
#Fit and transform train set, transform test set
tfidf_train=tfidf_vectorizer.fit_transform(x_train) tfidf_test=tfidf_vectorizer.transform(x_test)
Train data using the PassiveAggressiveClassifier
#train data using PassiveAggressiveClassifier
pac=PassiveAggressiveClassifier(max_iter=50)
pac.fit(tfidf_train,y_train)
Find the score
#Predicting model and finding the Score
y_pred=pac.predict(tfidf_test)
score=accuracy_score(y_test,y_pred)
print(f'Accuracy: {round(score*100,2)}%')
Finding the confusion-matrix
#Build confusion matrix
confusion_matrix(y_test,y_pred, labels=['FAKE','REAL'])
So with this model, we have true positives, true negatives, false positives, and false negatives.
Get your project or assignment completed by Deep learning expert and experienced developers and researchers.
OR
If you have project files, You can send at codersarts@gmail.com directly