-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlr_classify.py
62 lines (45 loc) · 2.11 KB
/
lr_classify.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
58
59
60
61
62
import pandas as pd
train = pd.read_csv('defense.csv')
test = pd.read_csv('attack.csv')
import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
def format_data(data, max_features, maxlen, tokenizer=None):
data = data.sample(frac=1).reset_index(drop=True)
data['text'] = data['text'].apply(lambda x: str(x))
Y = data['label'].values # 0: Real; 1: Fake
X = data['text']
if not tokenizer:
filters = "\"#$%&()*+./<=>@[\\]^_`{|}~\t\n"
tokenizer = Tokenizer(num_words=max_features, filters=filters)
tokenizer.fit_on_texts(list(X))
X = tokenizer.texts_to_sequences(X)
X = pad_sequences(X, maxlen=maxlen)
return X, Y, tokenizer
max_features, max_len = 3000, 25
X_train, Y_train, tokenizer = format_data(train, max_features, max_len)
X_test, Y_test, _ = format_data(test, max_features, max_len, tokenizer)
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(max_iter=500).fit(X_train, Y_train)
preds = lr.predict(X_test)
def accuracy_percentile(preds, Y_validate):
"""Return the percentage of correct predictions for each class and in total"""
real_correct, fake_correct, total_correct = 0, 0, 0
_, (real_count, fake_count) = np.unique(Y_validate, return_counts=True)
for i, r in enumerate(preds):
if r == Y_validate[i]:
total_correct += 1
if r == 1:
fake_correct += 1
else:
real_correct += 1
print('Real Accuracy:', real_correct/real_count * 100, '%')
print('Fake Accuracy:', fake_correct/fake_count * 100, '%')
print('Total Accuracy:', total_correct/(real_count + fake_count) * 100, '%')
accuracy_percentile(preds, Y_test)
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score, roc_auc_score
print('AUC: {}'.format(roc_auc_score(preds, Y_test)))
print('Accuracy: {}'.format(accuracy_score(preds, Y_test)))
print('Precision: {}'.format(precision_score(preds, Y_test)))
print('Recall: {}'.format(recall_score(preds, Y_test)))
print('F1: {}'.format(f1_score(preds, Y_test)))