-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord2VecAvgWord2Vec.py
154 lines (129 loc) · 5.12 KB
/
Word2VecAvgWord2Vec.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
import re
from tqdm import tqdm
import numpy as np
import pandas as pd
import nltk
from nltk.corpus import stopwords
from nltk import sent_tokenize
from nltk.stem import WordNetLemmatizer
import gensim.downloader as api
from gensim.models import Word2Vec
from gensim.utils import simple_preprocess
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
messages = pd.read_csv('Datasets/smsspamcollection/SMSSpamCollection', sep='\t', names=['label', 'message'])
print(messages.head())
print(messages.shape)
print(messages['message'].loc[451])
# Data cleaning and preprocessing (Using Lemmatization)
lemmatizer = WordNetLemmatizer()
corpus = []
for i in range(len(messages)):
review = re.sub('[^a-zA-Z0-9]', ' ', messages['message'][i])
review = review.lower()
review = review.split()
review = [lemmatizer.lemmatize(word) for word in review if word not in stopwords.words('english')]
review = ' '.join(review)
corpus.append(review)
print(corpus[0])
"""Word2Vec Implementation"""
# Pretrained Word2Vec Model
# wv = api.load('word2vec-google-news-300')
# vec_king = wv['king']
# print(vec_king)
# print(wv.most_similar('man'))
# print(wv.most_similar('king'))
# print('Cosine Similarity of man and king', wv.similarity('man', 'king'))
# print('Cosine Similarity of html and programmer', wv.similarity('html', 'programmer'))
# vec = wv['king'] - wv['man'] + wv['woman']
# print(wv.most_similar([vec]))
# Lets train Word2vec from scratch
# Getting all the words of sentences
words = []
for sentence in corpus:
sent_token = sent_tokenize(sentence)
for sent in sent_token:
words.append(simple_preprocess(sent))
# print(words)
model = Word2Vec(words, window=5, min_count=2)
print('All the vocabulary of the dataset:', model.wv.index_to_key) # All the vocabulary of the dataset
print('Total Vocabulary size:', model.corpus_count) #
print('Number of epochs used for training:', model.epochs)
print("Similar words of 'kid'", model.wv.similar_by_word('kid'))
print("Shape of word 'kid'", model.wv['kid'].shape)
print(words[73])
print(type(model.wv.index_to_key))
"""Average Word2Vec Implementation"""
# def avg_word2vec(doc):
# # remove out-of-vocabulary words
# # sent = [word for word in doc if word in model.wv.index_to_key]
# # print(sent)
#
# return np.mean([model.wv[word] for word in doc if word in model.wv.index_to_key], axis=0)
# # or [np.zeros(len(model.wv.index_to_key))], axis=0)
#
#
# # apply for the entire sentences
# X = []
# for i in tqdm(range(len(words))):
# X.append(avg_word2vec(words[i]))
# print(type(X))
#
# X_new = np.array(X)
# print(X_new[3])
# print(X_new.shape)
w2v_words = list(model.wv.index_to_key)
sent_vectors = [] # the avg-w2v for each sentence/review is stored in this list
for sent in tqdm(words): # for each review/sentence
sent_vec = np.zeros(
100) # as word vectors are of zero length 100, you might need to change this to 300 if you use google's w2v
cnt_words = 0 # num of words with a valid vector in the sentence/review
for word in sent: # for each word in a review/sentence
if word in w2v_words:
vec = model.wv[word]
sent_vec += vec
cnt_words += 1
if cnt_words != 0:
sent_vec /= cnt_words
sent_vectors.append(sent_vec)
print(len(sent_vectors)) # to check the total rows of the matrix
print(len(sent_vectors[0]))
print(type(sent_vectors))
# Independent Features
X_new = np.array(sent_vectors)
print('Independent Features Shape:', X_new.shape)
# print(X_new[3])
print(X_new[3].shape)
# Display all the data where corpus length < 1
print('Removed Corpus:', [[i, j, k] for i, j, k in zip(list(map(len, corpus)), corpus, messages['message']) if i < 1])
# Dependent Features
y = messages[list(map(lambda x: len(x) > 0, corpus))]
y = pd.get_dummies(y['label'])
y = y.iloc[:, 0].values
print('Dependent Features Shape:', y.shape)
print(sent_vectors[0].reshape(1, -1).shape)
# this is the final independent features
df = pd.DataFrame()
for i in range(0, len(sent_vectors)):
sent_vector_reshaped = sent_vectors[i].reshape(1, -1)
df = pd.concat([df, pd.DataFrame(sent_vector_reshaped)], ignore_index=True)
print(df.head())
df['Output'] = y
print(df.head())
df.dropna(inplace=True) # Dropping Null Values
# print(df.isnull().sum())
X = df.drop('Output', axis=1)
y = df['Output']
# print(X.isnull().sum())
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
classifier = RandomForestClassifier()
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
score = accuracy_score(y_test, y_pred)
print('Accuracy Score (AvgWord2Vec):', score)
report = classification_report(y_test, y_pred)
print(report)
"""https://www.kaggle.com/datasets/lakshmi25npathi/imdb-dataset-of-50k-movie-reviews"""