-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
149 lines (128 loc) · 5.15 KB
/
code.py
File metadata and controls
149 lines (128 loc) · 5.15 KB
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
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O
import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt
from textblob import TextBlob
df = pd.read_csv("../input/bitcoin-tweets/Bitcoin_tweets.csv")
df.head()
df.dropna(subset=['hashtags'], inplace=True)
df = df[['text']]
df.columns = ['tweets']
df.head()
import nltk
from nltk.stem.wordnet import WordNetLemmatizer
nltk.download('wordnet')
nltk.download('stopwords')
nltk.download('punkt')
stop_words = nltk.corpus.stopwords.words(['english'])
print(stop_words)
from nltk.tokenize import TweetTokenizer
from nltk.stem.wordnet import WordNetLemmatizer
lem = WordNetLemmatizer()
def cleaning(data):
#remove urls
tweet_without_url = re.sub(r'http\S+',' ', data)
#remove hashtags
tweet_without_hashtag = re.sub(r'#\w+', ' ', tweet_without_url)
#3. Remove mentions and characters that not in the English alphabets
tweet_without_mentions = re.sub(r'@\w+',' ', tweet_without_hashtag)
precleaned_tweet = re.sub('[^A-Za-z]+', ' ', tweet_without_mentions)
#2. Tokenize
tweet_tokens = TweetTokenizer().tokenize(precleaned_tweet)
#3. Remove Puncs
tokens_without_punc = [w for w in tweet_tokens if w.isalpha()]
#4. Removing Stopwords
tokens_without_sw = [t for t in tokens_without_punc if t not in stop_words]
#5. lemma
text_cleaned = [lem.lemmatize(t) for t in tokens_without_sw]
#6. Joining
return " ".join(text_cleaned)
df['cleaned_tweets'] = df['tweets'].apply(cleaning)
df.head()
def getSubjectivity(tweet):
return TextBlob(tweet).sentiment.subjectivity
def getPolarity(tweet):
return TextBlob(tweet).sentiment.polarity
df['subjectivity'] = df['cleaned_tweets'].apply(getSubjectivity)
df['polarity'] = df['cleaned_tweets'].apply(getPolarity)
df.head()
def getSentiment(score):
if score < 0:
return 'negative'
elif score == 0:
return 'neutral'
else:
return 'positive'
df['sentiment'] = df['polarity'].apply(getSentiment)
df.head()
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import tensorflow.keras.layers as Layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.optimizers import Adam
from keras.layers import Dense, Dropout, Embedding, LSTM, Conv1D, GlobalMaxPooling1D, Bidirectional, SpatialDropout1D
from keras.models import load_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
# Encode Categorical Variable
X = df['cleaned_tweets']
y = pd.get_dummies(df['sentiment']).values
num_classes = df['sentiment'].nunique()
seed = 101 # fix random seed for reproducibility
np.random.seed(seed)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
stratify=y,
random_state=seed)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
max_features = 20000
tokenizer = Tokenizer(num_words=max_features)
tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
from keras.preprocessing import sequence
max_words = 30
X_train = sequence.pad_sequences(X_train, maxlen=max_words)
X_test = sequence.pad_sequences(X_test, maxlen=max_words)
print(X_train.shape,X_test.shape)
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense,Embedding,Conv1D,MaxPooling1D,LSTM
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
batch_size = 128
epochs = 5
max_features = 20000
embed_dim = 100
np.random.seed(seed)
K.clear_session()
model = Sequential()
model.add(Embedding(max_features, embed_dim, input_length=X_train.shape[1]))
model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
history = model.fit(X_train, y_train, validation_data=(X_test, y_test),
epochs=epochs, batch_size=batch_size, verbose=2)
# predict class with test set
y_pred_test = np.argmax(model.predict(X_test), axis=1)
print('Accuracy:\t{:0.1f}%'.format(accuracy_score(np.argmax(y_test,axis=1),y_pred_test)*100))
print(classification_report(np.argmax(y_test,axis=1), y_pred_test))
#confusion matrix
confmat = confusion_matrix(np.argmax(y_test,axis=1), y_pred_test)
fig, ax = plt.subplots(figsize=(4, 4))
ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3)
for i in range(confmat.shape[0]):
for j in range(confmat.shape[1]):
ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center')
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.tight_layout()