-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.py
180 lines (152 loc) · 5.45 KB
/
Script.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# SentimentTwitter
# Sentiment Analysis on a Twitter Dataset, found on Kaggle. Used Python and Naive Bayes
# Author: Corvi Andrea
### Libraries
import re
from PIL import Image
from wordcloud import WordCloud, STOPWORDS
from IPython.display import Image as im
import pandas as pd
pd.set_option("display.max_colwidth", 200)
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import string
import nltk # for text manipulation
from nltk.corpus import stopwords
from nltk import ngrams
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, recall_score, roc_curve, precision_score
import sklearn.metrics as metrics
### Importing data
data = pd.read_csv(r'C:\Users\corvi\Desktop\TM Project\train1.csv')
### Data Cleaning
def remove_pattern(input_txt, pattern):
r = re.findall(pattern, input_txt)
for i in r:
input_txt = re.sub(i, '', input_txt)
return input_txt
data['clean_tweet'] = np.vectorize(remove_pattern)(data['tweet'], "@[\w]*")
data.head()
## EDA
data.head(3)
data[data['label'] == 1].head(10)
data[data['label'] == 0].head(10)
data.shape
data["label"].value_counts()
length_data = data['tweet'].str.len()
plt.hist(length_data, bins=20, label="Tweets")
plt.legend()
plt.show()
## Stopwords
data.clean_tweet = [w for w in data.clean_tweet if w.lower() not in stopwords.words('english')]
## Stemmer
ps = nltk.PorterStemmer()
data.clean_tweet = [ps.stem(l) for l in data.clean_tweet]
## Data Splitting
X = data.clean_tweet
y = data.label
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.7, random_state=100)
train1=pd.concat([X_train,y_train], axis=1)
test1=pd.concat([X_test,y_test], axis=1)
# Tokenization
from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(train1.clean_tweet)
X_train_counts.shape
from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
X_train_tfidf.shape
# Train
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(X_train_tfidf, train1.label)
from sklearn.pipeline import Pipeline
text_clf = Pipeline([
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', MultinomialNB()),
])
text_clf.fit(train1.clean_tweet, train1.label)
# Test
y_pred = text_clf.predict(test1.clean_tweet)
# Classification Evaluation Measures
accuracy_score(y_test, y_pred)
precision_score(y_test, y_pred)
recall_score(y_test, y_pred)
# Plot of the confusion matrix
cm = confusion_matrix(y_test, y_pred)
ax= plt.subplot()
df_cm = pd.DataFrame(cm, index = [i for i in "AB"],columns = [i for i in "AB"])
plt.figure( figsize=(320,320) )
sns.heatmap(df_cm, annot=True, ax = ax, cmap='Blues', fmt='g'); #annot=True to annotate cells
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels');
ax.set_title('Confusion Matrix');
plt.show()
### Roc Curve and AUC
fpr, tpr, threshold = metrics.roc_curve(y_test, y_pred)
roc_auc = metrics.auc(fpr, tpr)
roc_curve(y_test, y_pred)
plt.figure(figsize=(32,10))
plt.plot(fpr, tpr, marker='.')
print("Area under the curve=" + str(roc_auc))
#### Support Vector Machine
from sklearn.linear_model import SGDClassifier
text_clf = Pipeline([
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', SGDClassifier(loss='hinge', penalty='l2',
alpha=1e-3, random_state=42,
max_iter=5, tol=None)),
])
text_clf.fit(train1.clean_tweet, train1.label)
y_pred = text_clf.predict(test1.clean_tweet)
np.mean(predicted == twenty_test.target)
### Wordclouds
mask = np.array(Image.open(r'C:\Users\corvi\Desktop\TM Project\twitter.jpg'))
# Negative
nwords = ' '.join([text for text in data['clean_tweet'][data['label'] == 1]])
wcn = WordCloud(width=800, height=400,background_color="white", max_words=2000, mask=mask).generate(nwords)
plt.figure( figsize=(50,40) )
plt.imshow(wcn, interpolation='bilinear')
plt.axis("off")
plt.show()
# Positive
pwords = ' '.join([text for text in data['clean_tweet'][data['label'] == 0]])
wcp = WordCloud(width=800, height=400,background_color="white", max_words=2000, mask=mask).generate(pwords)
plt.figure( figsize=(50,40) )
plt.imshow(wcp, interpolation='bilinear')
plt.axis("off")
plt.show()
## Main Hashtags
def hashtag_extract(x):
hashtags = []
# Loop over the words in the tweet
for i in x:
ht = re.findall(r"#(\w+)", i)
hashtags.append(ht)
return hashtags
# Pos
HT_regular = hashtag_extract(data['clean_tweet'][data['label'] == 0])
HT_regular = sum(HT_regular,[])
a = nltk.FreqDist(HT_regular)
d = pd.DataFrame({'Hashtag': list(a.keys()),'Count': list(a.values())})
d = d.nlargest(columns="Count", n = 15)
plt.figure(figsize=(32,10))
ax = sns.barplot(data=d, x= "Hashtag", y = "Count")
ax.set(ylabel = 'Count')
plt.show()
# Neg
HT_negative = hashtag_extract(data['clean_tweet'][data['label'] == 1])
HT_negative = sum(HT_negative,[])
b = nltk.FreqDist(HT_negative)
e = pd.DataFrame({'Hashtag': list(b.keys()), 'Count': list(b.values())})
e = e.nlargest(columns="Count", n = 15)
plt.figure(figsize=(32,10))
ax = sns.barplot(data=e, x= "Hashtag", y = "Count")
ax.set(ylabel = 'Count')
plt.show()