-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakenewsdetection.py
57 lines (41 loc) · 1.63 KB
/
fakenewsdetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""FakeNewsDetection.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1devttGsOx6KMDdLcOROAu0hK006NLVPR
# Pendeteksian Berita Palsu dengan Python
Selasa, 21 Desember 2021
Muhamad Abdul Karim
Sumber: [Data-Flair](https://data-flair.training/)
"""
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
#Membaca data
dataframenya = pd.read_csv("contoh-berita.csv")
#menampilkan shape dan lima baris pertama dalam dataset
print("shape : ", dataframenya.shape)
dataframenya.head()
#Menampilkan label-labelnya
labels = dataframenya.label
labels.head()
#Split dataset menjadi training dan testing sets
x_train, x_test, y_train, y_test = train_test_split(dataframenya['text'], labels, test_size=0.2, random_state=7)
#Inisialisasi TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(stop_words='english', max_df=0.7)
#Fit dan transform train set, transform test set
tfidf_train = tfidf_vectorizer.fit_transform(x_train)
tfidf_test = tfidf_vectorizer.transform(x_test)
#Inisialisasi PassiveAggressiveClassifier
pac = PassiveAggressiveClassifier(max_iter=50)
pac.fit(tfidf_train, y_train)
#Prediksi test set dan kalkulasi keakuratan
y_pred = pac.predict(tfidf_test)
score = accuracy_score(y_test, y_pred)
print(f'Akurasi : {round(score*100,2)}%')
#Membuat confusion matrix
confusion_matrix(y_test,y_pred, labels = ['FAKE','REAL'])