-
Notifications
You must be signed in to change notification settings - Fork 1
/
Assignment 1 (2).py
312 lines (171 loc) · 7.46 KB
/
Assignment 1 (2).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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# coding: utf-8
# In[3]:
from dataprep.eda import create_report
import sweetviz as sv
# In[4]:
import pandas as pd
import pytz
import numpy as np
# In[40]:
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from torchtext.data.utils import get_tokenizer
import torch
from torch import nn
import torch.nn.functional as F
from torch import utils
torch.manual_seed(0)
np.random.seed(0)
import warnings
warnings.filterwarnings("ignore")
# In[5]:
train=pd.read_csv('https://raw.githubusercontent.com/zerickzhan/Group-20/main/review_train.csv')
challenge=pd.read_csv('https://raw.githubusercontent.com/zerickzhan/Group-20/main/review_challenge.csv')
# In[6]:
analyze_report = sv.analyze(train)
# In[7]:
analyze_report.show_html('report.html')
# In[8]:
analyze_reportchallenge = sv.analyze(challenge)
# In[9]:
analyze_reportchallenge.show_html('challenge.html')
# In[10]:
count_blank = len(train[(train['user_timezone'].isna() | train['user_timezone'].eq('')) & (train['review_city'].isna() | train['review_city'].eq(''))])
print("Number of rows where both 'review_timestamp' and 'review_city' are blank:", count_blank)
# In[11]:
count_blank2 = len(train[
(train['user_timezone'].isna() | train['user_timezone'].eq('')) &
(train['review_city'].isna() | train['review_city'].eq('')) &
(train['review_coordinates'].isna() | train['review_coordinates'].eq(''))
])
print("Number of rows where 'user_timezone', 'review_city', and 'review_coordinates' are blank:", count_blank)
# In[12]:
debug_selection = len(train[(train['negative_reason_confidence'].isna()) & (train['airline_sentiment'] == 'negative')])
print(debug_selection)
# In[13]:
analyze_report.show_html('report.html')
# From the above EDA extracts we can see: <br>
# 1. The airline sentiment are separated into 3 different kinds with 63% in negative, 21% in neural, and 16% in positive.<br>
# 2. There are 10 negative reasons, and the top 1 accounts for 32% being: Customer service Issue.<br>
# 3. We can see the data is collected from 6 different airline companies, with the top 3 being Emirate (26%), Qatar Airways(20%), and Qantas (19%)<br>
#
# <br>
# The data is suffering from missing data:<br>
#
# 1. 92.24% of review coordinates are missing, we this varible is going to cause collinearity issue with review city and we decide to keep review city. <br>
#
# 2. Contrary to our believe,review city and user time zone do not share all its missing rows. Only 2499 rows are missing for both review city and review time_zone. The inclusion of review coordinate can bring this down to 2418.<br>
#
# 3. The missing values of negative_reason is caused by the airline_sentiment not being negative. we are going to create a new column called reivew and backfill " positive" or "neural" for the missing values.
#
# ## Feature Engineering
# In[14]:
train['review'] = train.apply(lambda row: row['negative_reason'] if not pd.isna(row['negative_reason']) and row['negative_reason'] != '' else row['airline_sentiment'], axis=1)
# Create a new columon the review column.
# In[15]:
filtered_data = train[train['user_timezone'].isna() & ~train['review_city'].isna()]
# In[33]:
# preprocessing data
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
target = le.fit_transform(train['airline_sentiment']) # convert target into integers
train['airline_sentiment'] = target
print(le.classes_) # this shows which index maps to which class
# In[37]:
np.random.seed(511365)
index = list(range(train.shape[0])) # an list of indices
np.random.shuffle(index) # shuffle the index in-place
p_val = 0.2
p_test = 0.2
N_test = int(train.shape[0] * p_test)
N_val = int(train.shape[0] * p_val)
# get training, val and test sets
test_data = train.iloc[ index[:N_test] ,:]
val_data = train.iloc[ index[N_test: (N_test+N_val)], :]
train_data = train.iloc[ index[(N_test+N_val):], :]
print(test_data.shape)
print(val_data.shape)
print(train_data.shape)
# In[46]:
# define our own torch dataset
# for a torch dataset, we need to define two functions:
# __len__: return the length of dataset
# __getitem__: given a index (integer), return the corresponding sample, both y and X
class SpamDataset(utils.data.Dataset):
def __init__(self, myData):
"""
myData should be a dataframe object containing both y (first col) and X (second col)
"""
super().__init__()
self.data = myData
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return (self.data.iloc[idx,2], self.data.iloc[idx,7]) # (target, text)
# In[48]:
# now we can build our torch dataset
train_torch = SpamDataset(train_data)
val_torch = SpamDataset(val_data)
test_torch = SpamDataset(test_data)
# In[49]:
# check
train_torch.__getitem__(3)
# In[51]:
tokenizer = get_tokenizer('basic_english')
# In[50]:
from torchtext.vocab import build_vocab_from_iterator
# ===== Build vocabulary =====
# an unknown token is added for all unknown words outside the documents
# you may specify the min_freq to filter out infrequent words
vocabulary = build_vocab_from_iterator(
[tokenizer(msg) for msg in train['review_text']],
specials=["<unk>"],
min_freq = 3, # filter out all words that appear less than three times
)
# Set to avoid errors with unknown words
vocabulary.set_default_index(vocabulary["<unk>"])
# In[52]:
len(vocabulary)
# In[65]:
# The vocab object maps a word to an idx (an integer)
print(vocabulary['better'])
print(vocabulary['sun'])
print(vocabulary['iertuei']) # something not in vocab will be mapped to default_index = 0
# In[66]:
# ========= Step 2 ==============
# Notice in a corpus, each document can have different size. Thus, we usually pad zeros to the maximum length of document.
# Alternatively, you can concat all documents into a long vector
# and the starting index of each document is identified in the variable called offsets.
def collate_batch(batch):
target_list, text_list, offsets = [], [], [0] # initalize the first offset to be 0 here
# loop through all samples in batch
for idx in range(len(batch)):
_label = batch[idx][0]
_text = batch[idx][1]
target_list.append( _label )
tokens = doc_tokenizer( _text )
text_list.append(tokens)
# ====== wrong ======
#if idx == 0:
# offsets.append(0) # the first document starts from idx 0
#else:
# offsets.append(offsets[-1] + tokens.size(0)) # the next document starts from (offsets[-1] + tokens.size(0))
# ===================
offsets.append(offsets[-1] + tokens.size(0))
offsets = offsets[:-1] # remove the last entry
# convert to torch tensor
target_list = torch.tensor(target_list, dtype=torch.int64)
offsets = torch.tensor(offsets)
text_list = torch.cat(text_list) # concat into a long vector
return target_list, text_list, offsets
# In[ ]:
torch.manual_seed(0)
batchSize = 8
train_loader = utils.data.DataLoader(train_torch, batch_size=batchSize, shuffle=True, collate_fn=collate_batch)
val_loader = utils.data.DataLoader(val_torch, batch_size=batchSize, shuffle=True, collate_fn=collate_batch)
test_loader = utils.data.DataLoader(test_torch, batch_size=batchSize, shuffle=False, collate_fn=collate_batch)
# In[ ]:
# In[ ]: