forked from sandeepchittilla/ensemble-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathELTP5project_script.py
More file actions
565 lines (442 loc) · 19.8 KB
/
Copy pathELTP5project_script.py
File metadata and controls
565 lines (442 loc) · 19.8 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"""
Python script to submit as a part of the project of ELTP 2020 course.
This script serves as a template. Please use proper comments and meaningful variable names.
"""
"""
Group Members:
(1) Sonia Kamaal
(2) Sai Sandeep Chittilla
(3) Taneja Ankisetty
"""
"""
Import necessary packages
"""
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
import pandas as pd
import matplotlib.pyplot as plt
import spacy
import re
import string
from nltk.stem import PorterStemmer
from nltk.stem.snowball import FrenchStemmer
stemmer = PorterStemmer()
stemmer_fr = FrenchStemmer()
import os.path
import scipy
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from tqdm.notebook import tqdm
from sklearn.decomposition import TruncatedSVD
from sklearn.model_selection import train_test_split
from numpy import loadtxt
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.ensemble import AdaBoostClassifier
from sklearn import tree
from sklearn.linear_model import LogisticRegression
# pip3 install quantulum3
from quantulum3 import parser
# pip install --upgrade scikit-learn
from sklearn.ensemble import StackingClassifier
# Dependencies for lightgbm
# pip install lightgbm
# brew install cmake
# brew install libomp
import lightgbm as lgb
# Installation spacy packages depends on the confirguration of laptop. So one of the below works
# spacy_nlp_fr = spacy.load('fr_core_news_md')
# spacy_nlp_en = spacy.load('en_core_web_md')
spacy_nlp_fr = spacy.load('fr_core_news_sm')
spacy_nlp_en = spacy.load('en_core_web_sm')
# pip install regex
import regex as re
print('########## SUCCESSFULLY INSTALLED PACKAGES ########## ')
# Preprocessing Steps
def remove_html_tags(text):
"""Remove html tags from a string"""
clean = re.compile('<.*?>')
return re.sub(clean, ' ', text)
def convert_html(string):
""" Replaces html tags """
string = string.replace('À','À')
string = string.replace('à','à')
string = string.replace('Â','Â')
string = string.replace('â','â')
string = string.replace('Æ','Æ')
string = string.replace('æ','æ')
string = string.replace('Ç','Ç')
string = string.replace('ç','ç')
string = string.replace('È','È')
string = string.replace('è','è')
string = string.replace('É','É')
string = string.replace('é','é')
string = string.replace('Ê','Ê')
string = string.replace('ê','ê')
string = string.replace('Ë','Ë')
string = string.replace('ë','ë')
string = string.replace('Î','Î')
string = string.replace('î','î')
string = string.replace('Ï','Ï')
string = string.replace('ï','ï')
string = string.replace('Ô','Ô')
string = string.replace('ô','ô')
string = string.replace('Œ','Œ')
string = string.replace('œ','œ')
string = string.replace('Ù','Ù')
string = string.replace('ù','ù')
string = string.replace('Û','Û')
string = string.replace('û','û')
string = string.replace('Ü','Ü')
string = string.replace('ü','ü')
string = string.replace('«','«')
string = string.replace('»','»')
string = string.replace('€','€')
string = string.replace('•',' ') # removing bullet points
string = string.replace('’',"'")
return string
# converting ascii characters
def convert_ascii(string):
# string = string.replace(''', "'")
string = re.sub('&#[0-9]+',r' ', string)
string = string.replace('\xa0',' ')
return string
# removing special characters
def remove_special(string):
string = string.replace('.nan',' ')
string = string.replace('®',' ')
string = string.replace('N°',' ')
string = ''.join([i if ord(i) < 128 else ' ' for i in string])
return string
def splitAlphaNumeric(string):
string = re.sub('([0-9]+)([aA-zZ]+)',r'\g<1> \g<2>', string)
string = re.sub('([aA-zZ]+)([0-9]+)',r'\g<1> \g<2>', string)
return string
def replaceMetrics(string):
string = string.replace('m³',' m3 ')
string = string.replace('m²', ' m2 ')
string = string.replace('m³la', ' m3 la')
string = string.replace('m³kitprov', ' m3 kitprov')
string = string.replace('m³kit', ' m3 kit')
string.replace('cm²',' cm2 ')
string.replace('cm³',' cm3 ')
string.replace('µ',' mu ')
string.replace('gm²',' gm2 ')
string.replace('ma²',' ma2 ')
string.replace('ß',' beta ')
string.replace('mm²',' mm2 ')
string.replace('m²',' m2 ')
return string
def normalize_accent(string):
string = string.replace('á', 'a')
string = string.replace('â', 'a')
string = string.replace('à', 'a')
string = string.replace('é', 'e')
string = string.replace('è', 'e')
string = string.replace('ê', 'e')
string = string.replace('ë', 'e')
string = string.replace('î', 'i')
string = string.replace('ï', 'i')
string = string.replace('ö', 'o')
string = string.replace('ô', 'o')
string = string.replace('ò', 'o')
string = string.replace('ó', 'o')
string = string.replace('ù', 'u')
string = string.replace('û', 'u')
string = string.replace('ü', 'u')
string = string.replace('ç', 'c')
string = string.replace('ñ', 'n')
string.replace('ª','a')
string.replace('ä','a')
string.replace('ã','a')
string.replace('ø','o')
string.replace('œ','ae')
string.replace('º',' ')
return string
def raw_to_tokens(raw_string):
string = raw_string.lower()
string = normalize_accent(string)
# french lemma
spacy_tokens = spacy_nlp_fr(string)
string_tokens = [token.lemma_ and token.orth_ for token in spacy_tokens if not token.is_punct if not token.is_stop]
string = " ".join(string_tokens)
# en lemma
spacy_tokens = spacy_nlp_en(string)
string_tokens = [token.lemma_ and token.orth_ for token in spacy_tokens if not token.is_punct if not token.is_stop]
string = " ".join(string_tokens)
string = " ".join([stemmer.stem(w.lstrip('0')) for w in string.split()]) # en stemmer and removing leading 0's
clean_string = " ".join([stemmer_fr.stem(w) for w in string.split()]) # fr stemmer
return clean_string
# Feature Engineering
# Identify if there is any year (eg. 1995, 20110) in the designation column
def get_year_info(all_data):
year = []
for i in all_data:
year1 = re.match(r'.*([^\d][1][8-9][\d]{2}[^\d])', str(i))
year2 = re.match(r'.*([^\d][2][0][1|2][\d][^\d])', str(i))
if year1 or year2 is not None:
year.append(1)
else:
year.append(0)
return(year)
# Identify if there is any dimension (eg. 10x10, 5-5) in the designation column
def get_dim_info(all_data):
dimensions = []
err_count=0
err_rec=[]
for i in all_data:
try:
dim_list = re.search(r'(?<!\S)\d+(?:,\d+)? ?x ?\d+(?:,\d+)?(?: ?x ?\d+(?:,\d+)?)*', i)
except:
dim_list = re.search(r'(?<!\S)\d+(?:,\d+)? ?x ?\d+(?:,\d+)?(?: ?x ?\d+(?:,\d+)?)*', str(i))
err_count+=1
err_rec.append(i)
if dim_list is not None:
dimensions.append(1)
else:
dimensions.append(0)
print(err_count)
print(err_rec)
return(dimensions)
# Identify if there is any color (eg. red, noir) in the designation column
def get_colour_info(all_data):
colours=[]
for i in all_data:
colour_list = re.search(r'(\L<colours>)', str(i), colours = [
'Red', 'rouge', 'yellow', 'jaune', 'blue', 'bleu', 'bleue' 'green', 'vert',
'verte', 'orange', 'white', 'blanc', 'blanche', 'black', 'noir', 'noire', 'gray',
'gris', 'grise', 'brown', 'marron', 'pink', 'rose', 'purple', 'violet', 'violette'])
if colour_list is not None:
colours.append(1)
else:
colours.append(0)
return(colours)
# Identify if there is any measurement units (eg. m2, cm3) in the designation column
def get_quants_info(all_data):
quants=[]
for i in all_data:
try:
quants_list = parser.parse(i)
if(str(quants_list[0].unit.name) == 'dimensionless'):
quants.append(0)
else:
try:
quants.append(1)
except Exception as e: # catches issues when there is no quant term detected
quants.append(0)
except: # cactches issues with the parsing itself (should be rare)
quants.append(0)
return(quants)
# Captures length of the designation string as a feature
def mapCharLength(string):
char_len = len(str(string))
if char_len<50:
return 0
elif char_len<100:
return 1
elif char_len<150:
return 2
elif char_len<200:
return 3
else:
return 4
# Encoding y_train labels
def mapPrdIdtoInteger(df, col_name):
uniqueProdIds = [i for i in pd.unique(df[col_name].values)]
prdIdDict = {}
for i in range(len(uniqueProdIds)):
prdIdDict.setdefault(uniqueProdIds[i],i)
return prdIdDict
# #SUBSAMPLING
# req_indices = y_train['rank']<=1000
# x_train_limit = x_train_svd_all[req_indices]
# y_train_limit = y_train[req_indices]
# x_train_limit.shape
"""
Each of your model should have a separate method. e.g. run_random_forest, run_decision_tree etc.
Your method should:
(1) create the proper instance of the model with the best hyperparameters you found
(2) fit the model with a given training data
(3) run the prediction on a given test data
(4) return accuracy and F1 score
Following is a sample method. Please note that the parameters given here are just examples.
"""
"""
@param: X_train - a numpy matrix containing features for training data (e.g. TF-IDF matrix)
@param: y_train - a numpy array containing labels for each training sample
@param: X_test - a numpy matrix containing features for test data (e.g. TF-IDF matrix)
@param: y_test - a numpy array containing labels for each test sample
"""
def model_random_forest(X_train, y_train, X_test, y_test):
clf = RandomForestClassifier(n_estimators = 100, max_depth=2, random_state=0) # please choose all necessary parameters
clf.fit(X_train, y_train)
y_predicted = clf.predict(X_test)
rf_accuracy = accuracy_score(y_test, y_predicted)
rf_f1 = f1_score(y_test, y_predicted, average="weighted")
return rf_accuracy, rf_f1
def model_xgboost(X_train, y_train, X_test, y_test):
model = XGBClassifier(max_depth=3, min_child_weight=1, n_estimators=100)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
xg_accuracy = f1_score(y_test, y_pred, average = 'weighted')
xg_f1 = accuracy_score(y_test, y_pred)
return xg_accuracy, xg_f1
def model_adaboost(X_train, y_train, X_test, y_test):
clf = AdaBoostClassifier(n_estimators=300, learning_rate=0.1)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
ada_accuracy = f1_score(y_test, y_pred, average = 'weighted')
ada_f1 = accuracy_score(y_test, y_pred)
return ada_accuracy, ada_f1
def model_decisiontree(X_train, y_train, X_test, y_test):
clf = tree.DecisionTreeClassifier(max_features='sqrt',random_state=7,)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
dt_accuracy = f1_score(y_test, y_pred, average = 'weighted')
dt_f1 = accuracy_score(y_test, y_pred)
return dt_accuracy, dt_f1
def model_lightgbm(X_train, y_train, X_test, y_test):
model = lgb.LGBMClassifier(min_child_samples=20, min_child_weight=0.001, n_estimators=100, num_leaves=31)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
lgb_accuracy = f1_score(y_test, y_pred, average = 'weighted')
lgb_f1 = accuracy_score(y_test, y_pred)
return lgb_accuracy, lgb_f1
def model_stack(X_train, y_train, X_test, y_test):
estimators = [('xgb', XGBClassifier()), ('lgb', lgb.LGBMClassifier())]
model = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
stack_accuracy = f1_score(y_test, y_pred, average = 'weighted')
stack_f1 = accuracy_score(y_test, y_pred)
return stack_accuracy, stack_f1
def model_train_lgb(X_train, y_train, X_test, y_test):
params = {
"objective" : "multiclass",
"metric" : "",
"learning_rate" : 0.01,
"num_class" : 27
}
lgtrain = lgb.Dataset(X_train, label=y_train)
lgval = lgb.Dataset(X_test, label=y_test)
evals_result = {}
model = lgb.train(params=params, train_set=lgtrain, num_boost_round=150,
valid_sets=[lgval],
early_stopping_rounds=60,
verbose_eval=50,
evals_result=evals_result)
y_pred = model.predict(X_test, num_iteration=model.best_iteration)
predictions = []
for x in y_pred:
predictions.append(np.argmax(x))
train_lgb_accuracy = f1_score(y_test, predictions, average = 'weighted')
train_lgb_f1 = accuracy_score(y_test, predictions)
return train_lgb_accuracy, train_lgb_f1
"""
The main function should print all the accuracies and F1 scores for all the models.
The names of the models should be sklearn classnames, e.g. DecisionTreeClassifier, RandomForestClassifier etc.
Please make sure that your code is outputting the performances in proper format, because your script will be run automatically by a meta-script.
"""
if __name__ == "__main__":
# Import Train and Test data
x_train = pd.read_csv('X_train.csv',header = 0, names=['IntegerId','Designation','Description','ProductId','ImageId'])
# x_test = pd.read_csv('X_test.csv',header = 0, names=['IntegerId','Designation','Description','ProductId','ImageId'])
y_train = pd.read_csv('Y_train.csv', header =0, names = ['IntegerId','PrdTypeCode'])
# Merge Designation and Description columns
x_train['Merged Des'] = x_train['Designation'].astype(str) + ' ' + x_train['Description'].astype(str)
# x_test['Merged Des'] = x_test['Designation'].astype(str) + ' ' + x_test['Description'].astype(str)
if not os.path.isfile('x_train_cleaned.csv'):
# Clean Designation column of train
clean = []
raw = list(x_train['Merged Des'])
for i in tqdm(raw):
text = convert_ascii(i)
text = remove_html_tags(text)
text = convert_html(text)
text = remove_special(text)
text = splitAlphaNumeric(text)
text = replaceMetrics(text)
clean.append(raw_to_tokens(text))
# Add the cleaned designation column back to the dataframe
x_train['CleanDesignation'] = clean
x_train.to_csv('x_train_cleaned.csv')
x_train = pd.read_csv('x_train_cleaned.csv')
# Imputations of Null values
# There are only 10 values, we can remove them from the training.
# However, if they are present in test data, we can directly predict them to be of 2705 PrdTypeCode
req_indices = ~ x_train['CleanDesignation'].isnull()
x_train = x_train[req_indices]
y_train = y_train[req_indices]
# Including all the pre-processing steps to x_train dataset
x_train['Year'] = get_year_info(x_train['CleanDesignation'])
x_train['Dimensions'] = get_dim_info(x_train['CleanDesignation'])
x_train['Colour'] = get_colour_info(x_train['CleanDesignation'])
x_train['Quants'] = get_quants_info(x_train['CleanDesignation'])
x_train['char_length_class'] = x_train['CleanDesignation'].apply(lambda x: mapCharLength(x))
print('########## SUCCESSFULLY FINISHED PRE-PROCESSING ########## ')
# saving and reading the file to avoid loss of information
x_train.to_csv('x_train_processed.csv')
x_train = pd.read_csv('x_train_processed.csv',index_col=0)
# tf-idf vectorizer on cleandesignation column which has all the pre-processing steps
tfidfconverter = TfidfVectorizer(min_df=5, max_df=0.9)
x_train_tfidf = tfidfconverter.fit_transform(list(x_train['CleanDesignation'])).toarray()
x_train_tfidf_all = pd.DataFrame(x_train_tfidf,columns=tfidfconverter.get_feature_names())
# In order to reduce number of features, we did dimension reduction using SVD
if not os.path.isfile('train4k.pkl'):
svd = TruncatedSVD(n_components = 4000)
svd.fit(x_train_tfidf_all)
print('Variance explained after SVD',svd.explained_variance_ratio_.sum())
x_train_svd = svd.transform(x_train_tfidf_all)
print("Shape of X_train after SVD",x_train_svd.shape)
feature_names = tfidfconverter.get_feature_names()
best_features = [feature_names[i] for i in svd.components_[0].argsort()[::-1]]
best_features = best_features[0:svd.n_components]
x_train_svd_all = pd.DataFrame(x_train_svd,columns=best_features)
# Save the updated x_train file to prevent loss of information
x_train_svd_all.to_pickle("train4k.pkl")
x_train_svd_all = pd.read_pickle("train4k.pkl")
print('########## SUCCESSFULLY FINISHED TF-IDF AND SVD ########## ')
# Adding feature engineering steps to the output of SVD
x_train_svd_all['Year'] = x_train['Year']
x_train_svd_all['Dimensions'] = x_train['Dimensions']
x_train_svd_all['Colour'] = x_train['Colour']
x_train_svd_all['Quants'] = x_train['Quants']
x_train_svd_all['char_length_class'] = x_train['char_length_class']
# Dropping the null values post SVD
x_train_svd_all = x_train_svd_all.drop(17433)
x_train_svd_all = x_train_svd_all.drop(9260)
y_train = y_train.drop(y_train.index[[17433,9260]])
# Reset index to match
y_train.reset_index(inplace=True)
x_train_svd_all.reset_index(inplace=True)
y_train_df = pd.DataFrame(y_train['PrdTypeCode'])
dictionary = mapPrdIdtoInteger(y_train_df,'PrdTypeCode')
y_train = pd.DataFrame.replace(y_train_df, to_replace = dictionary)
# Split the data to train data and validation data
x_train_cv, x_val, y_train_cv, y_val = train_test_split(x_train_svd_all, y_train, test_size = 0.2)
print('########## STARTING TO RUN EACH OF THE MODELS ########## ')
print('########## STARTING TRAINED LIGHTGBM ########## ')
train_lgb_accuracy, train_lgb_f1 = model_train_lgb(x_train_cv, y_train_cv, x_val, y_val)
print('########## STARTING DECISION TREES ########## ')
dt_accuracy, dt_f1 = model_decisiontree(x_train_cv, y_train_cv, x_val, y_val)
print('########## STARTING RANDOM FORESTS ########## ')
rf_accuracy, rf_f1 = model_random_forest(x_train_cv, y_train_cv, x_val, y_val)
print('########## STARTING ADABOOST ########## ')
ada_accuracy, ada_f1 = model_adaboost(x_train_cv, y_train_cv, x_val, y_val)
print('########## STARTING XGBOOST ########## ')
xg_accuracy, xg_f1 = model_xgboost(x_train_cv, y_train_cv, x_val, y_val)
print('########## STARTING LIGHTGBM ########## ')
lgb_accuracy, lgb_f1 = model_lightgbm(x_train_cv, y_train_cv, x_val, y_val)
print('########## STARTING STACKING CLASSIFIER ########## ')
stack_accuracy, stack_f1 = model_stack(x_train_cv, y_train_cv, x_val, y_val)
# print the results
print("resutls from Random Forest: accuracy = {} and f1 score = {}".format(rf_accuracy, rf_f1))
print("resutls from XGBosst: accuracy = {} and f1 score = {}".format(xg_accuracy, xg_f1))
print("resutls from AdaBoost: accuracy = {} and f1 score = {}".format(ada_accuracy, ada_f1))
print("resutls from Decision Trees: accuracy = {} and f1 score = {}".format(dt_accuracy, dt_f1))
print("resutls from LightGBM: accuracy = {} and f1 score = {}".format(lgb_accuracy, lgb_f1))
print("resutls from Stacking Classifier: accuracy = {} and f1 score = {}".format(stack_accuracy, stack_f1))
print("resutls from Trained LightGBM: accuracy = {} and f1 score = {}".format(train_lgb_accuracy, train_lgb_f1))