<jupyter_start><jupyter_text>Detecting Fake News Importing Libraries<jupyter_code>#import - necessary libraries
import numpy as np
import pandas as pd
import itertools
import matplotlib.pyplot as plt
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

df = pd.read_csv('News.csv')
df.head(5)
df.shape
df.info()
df.describe()
df.isnull().sum()
labels =df['label']
labels.head()
plt.hist(labels)
df.columns
# Split the dataset
x_train,x_test,y_train,y_test=train_test_split(df['text'], labels, test_size=0.2, random_state=7)<jupyter_output><empty_output><jupyter_text>Let’s initialize a TfidfVectorizer with stop words from the English language and a maximum document frequency of 0.7 (terms with a higher document frequency will be discarded). Stop words are the most common words in a language that are to be filtered out before processing the natural language data. And a TfidfVectorizer turns a collection of raw documents into a matrix of TF-IDF features.Now, fit and transform the vectorizer on the train set, and transform the vectorizer on the test set.<jupyter_code># Initialize a TfidfVectorizer
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)<jupyter_output><empty_output><jupyter_text>Next, we’ll initialize a PassiveAggressiveClassifier. This is. We’ll fit this on tfidf_train and y_train.Then, we’ll predict on the test set from the TfidfVectorizer and calculate the accuracy with accuracy_score() from sklearn.metrics.<jupyter_code>#Initialize a PassiveAggressiveClassifier
pac=PassiveAggressiveClassifier(max_iter=50)
pac.fit(tfidf_train,y_train)

#Predict on the test set and calculate accuracy
y_pred=pac.predict(tfidf_test)
score=accuracy_score(y_test,y_pred)
print(f'Accuracy: {round(score*100,2)}%')<jupyter_output>Accuracy: 92.74%<jupyter_text>We got an accuracy of 92.74% with this model.Finally, let’s print out a confusion matrix to gain insight into the number of false and true negatives and positives<jupyter_code># Build confusion matrix
df2 = pd.DataFrame(confusion_matrix(y_test,y_pred, labels=['FAKE','REAL']),
                   index = ["Actual Real",'Actual Fake'], columns =['Predicted Real','Predicted Fake'])
df2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08260869565217391, 'fraction_numerical': 0.009486166007905139, 'mean_word_length': 7.217532467532467, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '513635', 'license': 'MIT', 'n_tokens_mistral': 839, 'n_tokens_neox': 805, 'n_words': 241, 'path': 'Detecting Fake news.ipynb', 'repo_name': 'sdumza/Detecting-Fake-news', 'size': '2530'}