<jupyter_start><jupyter_text>1. Make Necessary imports<jupyter_code>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<jupyter_output><empty_output><jupyter_text>2. Load the data into a dataframe and get the shape of the data<jupyter_code>#Read the data
df=pd.read_csv('news.csv')
#Get shape and head
df.shape
df.head()<jupyter_output><empty_output><jupyter_text>3. Get the labels from the dataframe<jupyter_code>labels=df.label
labels.head()<jupyter_output><empty_output><jupyter_text>4. Split the dataset into training and testing sets<jupyter_code>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>5. 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.<jupyter_code>tfidf_vectorizer=TfidfVectorizer(stop_words='english', max_df=0.7)<jupyter_output><empty_output><jupyter_text>6.  Fit and transform the vectorizer on the train set, and transform the vectorizer on the test set.<jupyter_code>tfidf_train=tfidf_vectorizer.fit_transform(x_train) 
tfidf_test=tfidf_vectorizer.transform(x_test)<jupyter_output><empty_output><jupyter_text>7. Initialize a PassiveAggressiveClassifier which we will fit on on tfidf_train and y_train.<jupyter_code>pac=PassiveAggressiveClassifier(max_iter=50)
pac.fit(tfidf_train,y_train)<jupyter_output><empty_output><jupyter_text>8. Predict on the test set from the TfidfVectorizer and calculate the accuracy.<jupyter_code>y_pred=pac.predict(tfidf_test)
score=accuracy_score(y_test,y_pred)
print(f'Accuracy: {round(score*100,2)}%')<jupyter_output>Accuracy: 92.66%<jupyter_text>9. Print out a confusion matrix to gain insight into the number of false and true negatives and positives.<jupyter_code>confusion_matrix(y_test,y_pred, labels=['FAKE','REAL'])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup