-
Notifications
You must be signed in to change notification settings - Fork 2
/
Model.py
79 lines (62 loc) · 2.5 KB
/
Model.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import io
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.linear_model import LogisticRegression
###########################################################################
#generate training/test data
###########################################################################
bragging_file = io.open("cleantweets/Happy_clean.txt",'r',encoding="utf-8")
bitching_file = io.open("cleantweets/FML_clean.txt",'r',encoding="utf-8")
bragging_tweets = []
bitching_tweets = []
for _ in range(249000):
bragging_tweets.append([bragging_file.readline().strip("\n"),0])
bitching_tweets.append([bitching_file.readline().strip("\n"),1])
tweets = bragging_tweets+bitching_tweets
np.random.seed(42)
np.random.shuffle(tweets)
tweets_train = np.array(tweets[:348600])
tweets_test = np.array(tweets[348600:498000])
X_train = tweets_train[:,0]
Y_train = tweets_train[:,1]
X_test = tweets_test[:,0]
Y_test = tweets_test[:,1]
#########################################################################
#bag of words
#########################################################################
vectorizer = CountVectorizer()
X_train_counts = vectorizer.fit_transform(X_train)
X_test_counts = vectorizer.transform(X_test)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
#########################################################################
#logistic regression
#########################################################################
LR = LogisticRegression().fit(X_train_counts, Y_train)
predicted_LR = LR.predict(X_test_counts)
print('Accuracy with logistic regression (BOW):....',(np.round(np.mean(predicted_LR==Y_test),2)))
#uncomment to see results
'''
for i in range(50):
predicted_LR = LR.predict(X_test_counts[i])
try:
print(predicted_LR)
print(X_test[i],Y_test[i])
except:
print("error")
'''
LR = LogisticRegression().fit(X_train_counts, Y_train)
predicted_LR = LR.predict(X_test_tfidf)
print('Accuracy with logistic regression (tfidf):....',(np.round(np.mean(predicted_LR==Y_test),2)))
#uncomment to see results
'''
for i in range(50):
predicted_LR = LR.predict(X_test_counts[i])
try:
print(predicted_LR)
print(X_test[i],Y_test[i])
except:
print("error")
'''