-
Notifications
You must be signed in to change notification settings - Fork 2
/
stumbleupon_final.py
1225 lines (1011 loc) · 61.5 KB
/
stumbleupon_final.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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import numpy as np
import pylab as pl
import pandas as p
from scipy.sparse import hstack, vstack, csc_matrix
from sklearn import linear_model, cross_validation
from sklearn.feature_selection import SelectPercentile, chi2
from sklearn.feature_extraction.text import *
from sklearn.metrics import roc_auc_score
from sklearn.grid_search import GridSearchCV
from sklearn.decomposition import KernelPCA
from nltk import word_tokenize, clean_html
from nltk.stem import SnowballStemmer
import re
import datetime
import json
import cPickle as pickle
import multiprocessing
from functools import partial
from mpldatacursor import datacursor
from StringIO import StringIO
# Set a memory limit.
import resource
resource.setrlimit(resource.RLIMIT_AS, (10100 * 1048576L, -1L)) # 10.1GiB.
SEED = 57846821 # 11617 57846821 4512971451315520
def load_data(tfidf_url, tfidf_title, tfidf_body, tfidf_html, tfidf_links, tfidf_htmlbody):
"""
Loading of the raw data and tf-idf processing based on the input parameters. First the data is sought in
the "dump/" folder, if not found, it processes it and dumps the result so that it should only be generated once.
Input: for each {url, title, body, html, links, htmlbody} a list
[tfidf analyzer, ngram_range, minimum term frequency, maximum term frequency].
Output: a dict with {'train_url', 'train_title', 'train_body','train_html', 'train_links',
'train_htmlbody','test_url', 'test_title', 'test_body', 'test_html',
'test_links', 'test_htmlbody', 'train_rest', 'test_rest', 'y'}.
"""
print "\n---------------------------------------"
print "Loading raw data ...\n"
# Read the boilerplate (json) data.
train_json = list(np.array(p.read_table('data/train.tsv'))[:, 2])
test_json = list(np.array(p.read_table('data/test.tsv'))[:, 2])
# Read labels from training.
y = np.array(p.read_table('data/train.tsv'))[:, -1]
# Read raw_html.
train_html = []
train_clean_html = []
for urlid in np.array(p.read_table('data/train.tsv'))[:, 1]:
try:
html = open('data/raw_content/' + str(urlid) + '-utf').read()
except:
html = open('data/raw_content/' + str(urlid) + '-iso').read()
train_html.append(html.replace("\n", " ").replace("'", "").replace("’", "").replace("“", "").replace("–", ""))
train_clean_html.append(clean_html(html).replace("\n", " ").replace("'", "").replace("’", "").replace("“", "").replace("–", ""))
test_html = []
test_clean_html = []
for urlid in np.array(p.read_table('data/test.tsv'))[:, 1]:
try:
html = open('data/raw_content/' + str(urlid) + '-utf').read()
except:
html = open('data/raw_content/' + str(urlid) + '-iso').read()
test_html.append(html.replace("\n", " ").replace("'", "").replace("’", "").replace("“", "").replace("–", ""))
test_clean_html.append(clean_html(html).replace("\n", " ").replace("'", "").replace("’", "").replace("“", "").replace("–", ""))
train_json_url = []
train_json_title = []
train_json_body = []
# First interpreting json and splitting in {body, url, title} for training data.
print " Splitting json training ..."
for json_raw in train_json:
io = StringIO(json_raw)
json_data = json.load(io)
if json_data.get('url') is not None:
train_json_url.append(json_data.get('url').replace("'", ""))
else:
train_json_url.append(u'url_missing')
if json_data.get('title') is not None:
train_json_title.append(json_data.get('title').replace("'", ""))
else:
train_json_title.append(u'title_missing')
if json_data.get('body') is not None:
train_json_body.append(json_data.get('body').replace("'", ""))
else:
train_json_body.append(u'body_missing')
test_json_url = []
test_json_title = []
test_json_body = []
# Now for test data.
print " Splitting json test ...\n"
for json_raw in test_json:
io = StringIO(json_raw)
json_data = json.load(io)
if json_data.get('url') is not None:
test_json_url.append(json_data.get('url').replace("'", ""))
else:
test_json_url.append(u'url_missing')
if json_data.get('title') is not None:
test_json_title.append(json_data.get('title').replace("'", ""))
else:
test_json_title.append(u'title_missing')
if json_data.get('body') is not None:
test_json_body.append(json_data.get('body').replace("'", ""))
else:
test_json_body.append(u'body_missing')
# Pre-process non-text features.
print "---------------------------------------"
print "Preprocessing non-text ...\n"
try:
train_rest = pickle.load(open("dump/train_rest.pkl", "rb"))
test_rest = pickle.load(open("dump/test_rest.pkl", "rb"))
except:
# Could not read from dump, process again and dump.
# Read other features.
train_rest = p.read_table('data/train.tsv', na_values=['?'])
test_rest = p.read_table('data/test.tsv', na_values=['?'])
with open("dump/train_rest.pkl", "wb") as f:
pickle.dump(train_rest, f, pickle.HIGHEST_PROTOCOL)
with open("dump/test_rest.pkl", "wb") as f:
pickle.dump(test_rest, f, pickle.HIGHEST_PROTOCOL)
print "Dumped."
# Pre-process text.
print "\n---------------------------------------"
print "Preprocessing text (TF-IDF) ... \n"
class SnowballTokenizer(object):
def __init__(self):
self.wnl = SnowballStemmer('english')
def __call__(self, doc):
return [self.wnl.stem(t) for t in word_tokenize(" ".join(re.findall(r'\w+',
doc,
flags=re.UNICODE | re.LOCALE)))]
stopwords_uni = ['i', 'http', 'www']
tfidf = TfidfVectorizer(strip_accents='unicode', analyzer='word', tokenizer=SnowballTokenizer(),
lowercase=True, norm='l2', ngram_range=(1, 2), sublinear_tf=True,
use_idf=True, smooth_idf=True, max_features=None,
stop_words=stopwords_uni)
# TF-IDF
tfidf_stemmer = 'snowball'
#########################################################################################
# URL
#
tfidf.set_params(analyzer=tfidf_url[0], ngram_range=tfidf_url[1], min_df=tfidf_url[2], max_df=tfidf_url[3])
print(" Fitting TfidfVectorizer for url with params %s" % tfidf_url)
# First try to read from cache/dump.
try:
X_train_url = pickle.load(open("dump/tf_idf_TRAIN_URL_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
X_test_url = pickle.load(open("dump/tf_idf_TEST_URL_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
except:
# If it fails, then calculate again.
print "\t Could not read from dump, fitting ..."
tfidf.fit(train_json_url + test_json_url)
# Transform and dump.
X_train_url = tfidf.transform(train_json_url)
with open("dump/tf_idf_TRAIN_URL_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_train_url, f, pickle.HIGHEST_PROTOCOL)
X_test_url = tfidf.transform(test_json_url)
with open("dump/tf_idf_TEST_URL_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_test_url, f, pickle.HIGHEST_PROTOCOL)
with open("dump/tf_idf_FEATURES_URL_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(tfidf.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
print "\t Dumped."
print "\t Number of features: %i." % X_train_url.shape[1]
#########################################################################################
# TITLE
#
tfidf.set_params(analyzer=tfidf_title[0], ngram_range=tfidf_title[1], min_df=tfidf_title[2], max_df=tfidf_title[3])
print("\n Fitting TfidfVectorizer for title with params %s" % tfidf_title)
# First try to read from cache/dump.
try:
X_train_title = pickle.load(open("dump/tf_idf_TRAIN_TITLE_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
X_test_title = pickle.load(open("dump/tf_idf_TEST_TITLE_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
except:
# If it fails, then calculate again.
print "\t Could not read from dump, fitting ..."
tfidf.fit(train_json_title + test_json_title)
# Transform and dump.
X_train_title = tfidf.transform(train_json_title)
with open("dump/tf_idf_TRAIN_TITLE_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_train_title, f, pickle.HIGHEST_PROTOCOL)
X_test_title = tfidf.transform(test_json_title)
with open("dump/tf_idf_TEST_TITLE_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_test_title, f, pickle.HIGHEST_PROTOCOL)
with open("dump/tf_idf_FEATURES_TITLE_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(tfidf.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
print "\t Dumped."
print "\t Number of features: %i." % X_train_title.shape[1]
#########################################################################################
# BODY
#
tfidf.set_params(analyzer=tfidf_body[0], ngram_range=tfidf_body[1], min_df=tfidf_body[2], max_df=tfidf_body[3])
print("\n Fitting TfidfVectorizer for body with params %s" % tfidf_body)
try:
X_train_body = pickle.load(open("dump/tf_idf_TRAIN_BODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
X_test_body = pickle.load(open("dump/tf_idf_TEST_BODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
except:
# If it fails, then calculate again.
print "\t Could not read from dump, fitting ..."
tfidf.fit(train_json_body + test_json_body)
# Transform and dump.
X_train_body = tfidf.transform(train_json_body)
with open("dump/tf_idf_TRAIN_BODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_train_body, f, pickle.HIGHEST_PROTOCOL)
X_test_body = tfidf.transform(test_json_body)
with open("dump/tf_idf_TEST_BODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_test_body, f, pickle.HIGHEST_PROTOCOL)
with open("dump/tf_idf_FEATURES_BODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(tfidf.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
print "\t Dumped."
print "\t Number of features: %i." % X_train_body.shape[1]
#########################################################################################
# LINKS
#
class LinksTokenizer(object):
def __init__(self):
self.wnl = SnowballStemmer('english')
def __call__(self, doc):
return [self.wnl.stem(t) for t in word_tokenize(" ".join(re.findall(r'\[[^\]]*\]\([^\)]*\)',
doc,
flags=re.UNICODE | re.LOCALE)))]
tfidf = TfidfVectorizer(strip_accents='unicode', analyzer='word', tokenizer=LinksTokenizer(),
lowercase=True, norm='l2', ngram_range=(1, 2), sublinear_tf=True,
use_idf=True, smooth_idf=True, max_features=None,
stop_words=stopwords_uni)
tfidf.set_params(analyzer=tfidf_links[0], ngram_range=tfidf_links[1], min_df=tfidf_links[2], max_df=tfidf_links[3])
print("\n Fitting TfidfVectorizer for links with params %s" % tfidf_links)
try:
X_train_links = pickle.load(open("dump/tf_idf_TRAIN_LINKS_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
X_test_links = pickle.load(open("dump/tf_idf_TEST_LINKS_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
except:
# If it fails, then calculate again.
print "\t Could not read from dump, fitting ..."
tfidf.fit(train_clean_html + test_clean_html)
# Transform and dump.
X_train_links = tfidf.transform(train_clean_html)
with open("dump/tf_idf_TRAIN_LINKS_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_train_links, f, pickle.HIGHEST_PROTOCOL)
X_test_links = tfidf.transform(test_clean_html)
with open("dump/tf_idf_TEST_LINKS_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_test_links, f, pickle.HIGHEST_PROTOCOL)
with open("dump/tf_idf_FEATURES_LINKS_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(tfidf.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
print "\t Dumped."
print "\t Number of features: %i." % X_train_links.shape[1]
#########################################################################################
# HTML
#
class HTMLTokenizer(object):
def __init__(self):
self.wnl = SnowballStemmer('english')
def __call__(self, doc):
# Remove some special chars (get removed by word_tokenize too).
repl = re.sub(r'\#\#+', ' ', doc, flags=re.UNICODE | re.LOCALE)
repl = re.sub(r'\#\s', ' ', repl, flags=re.UNICODE | re.LOCALE)
# Remove the links from the html.
repl = re.sub(r'\[[^\]]*\]\([^\)]*\)', ' ', repl, flags=re.UNICODE | re.LOCALE)
return [self.wnl.stem(t) for t in word_tokenize(" ".join(re.findall(r'\w+',
repl,
flags=re.UNICODE | re.LOCALE)))]
tfidf = TfidfVectorizer(strip_accents='unicode', analyzer='word', tokenizer=HTMLTokenizer(),
lowercase=True, norm='l2', ngram_range=(1, 2), sublinear_tf=True,
use_idf=True, smooth_idf=True, max_features=None,
stop_words=stopwords_uni)
tfidf.set_params(analyzer=tfidf_html[0], ngram_range=tfidf_html[1], min_df=tfidf_html[2], max_df=tfidf_html[3])
print("\n Fitting TfidfVectorizer for html with params %s" % tfidf_html)
try:
X_train_html = pickle.load(open("dump/tf_idf_TRAIN_HTML_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
X_test_html = pickle.load(open("dump/tf_idf_TEST_HTML_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
except:
# If it fails, then calculate again.
print "\t Could not read from dump, fitting ..."
tfidf.fit(train_clean_html + test_clean_html)
# Transform and dump.
X_train_html = tfidf.transform(train_clean_html)
with open("dump/tf_idf_TRAIN_HTML_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_train_html, f, pickle.HIGHEST_PROTOCOL)
X_test_html = tfidf.transform(test_clean_html)
with open("dump/tf_idf_TEST_HTML_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_test_html, f, pickle.HIGHEST_PROTOCOL)
with open("dump/tf_idf_FEATURES_HTML_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(tfidf.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
print "\t Dumped."
print "\t Number of features: %i." % X_train_html.shape[1]
#########################################################################################
# HTML + BODY
#
tfidf.set_params(analyzer=tfidf_htmlbody[0], ngram_range=tfidf_htmlbody[1], min_df=tfidf_htmlbody[2], max_df=tfidf_htmlbody[3])
print("\n Fitting TfidfVectorizer for htmlbody with params %s" % tfidf_htmlbody)
try:
X_train_htmlbody = pickle.load(open("dump/tf_idf_TRAIN_HTMLBODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
X_test_htmlbody = pickle.load(open("dump/tf_idf_TEST_HTMLBODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "rb"))
except:
# If it fails, then calculate again.
print "\t Could not read from dump, fitting ..."
concat_train = [''.join((train_clean_html[i], train_json_body[i].encode('unicode-escape'))) for i in range(len(train_clean_html))]
concat_test = [''.join((test_clean_html[i], test_json_body[i].encode('unicode-escape'))) for i in range(len(test_clean_html))]
tfidf.fit(concat_train + concat_test)
#Transform and dump.
X_train_htmlbody = tfidf.transform(concat_train)
with open("dump/tf_idf_TRAIN_HTMLBODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_train_htmlbody, f, pickle.HIGHEST_PROTOCOL)
X_test_htmlbody = tfidf.transform(concat_test)
with open("dump/tf_idf_TEST_HTMLBODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(X_test_htmlbody, f, pickle.HIGHEST_PROTOCOL)
with open("dump/tf_idf_FEATURES_HTMLBODY_analyzer_%s_range_%s_mindf_%f_maxdf_%f_%s.pkl"
% (tfidf.get_params()['analyzer'], tfidf.get_params()['ngram_range'],
tfidf.get_params()['min_df'], tfidf.get_params()['max_df'],
tfidf_stemmer), "wb") as f:
pickle.dump(tfidf.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
print "\t Dumped."
print "\t Number of features: %i.\n" % X_train_htmlbody.shape[1]
return {'train_url': X_train_url, 'train_title': X_train_title, 'train_body': X_train_body,
'train_html': X_train_html, 'train_links': X_train_links, 'train_htmlbody': X_train_htmlbody,
'test_url': X_test_url, 'test_title': X_test_title, 'test_body': X_test_body,
'test_html': X_test_html, 'test_links': X_test_links, 'test_htmlbody': X_test_htmlbody,
'train_rest': train_rest, 'test_rest': test_rest, 'y': y}
# Model functions.
def model_Chi2LR(x_train, y_train, train_predict, x_predict, params):
"""
LR after chi2 feature selection.
Input: x_train = training_set,
y_train = labels for the training set,
train_predict = the first set to be predicted,
x_predict = the second set to be predicted,
params, the parameters for the model:
[lr_penalty, lr_C, lr_class_weight, gridsearch_bool, gridsearch_params,, chi2_perc],
chi2_perc is the percentile parameter for chi2 selection.
Output: [prediction for first set, prediction for second set].
"""
y_train = np.asarray(y_train, dtype='int')
select = SelectPercentile(chi2, percentile=params[5])
x_train = select.fit_transform(x_train, y_train)
train_predict = select.transform(train_predict)
x_predict = select.transform(x_predict)
model = linear_model.LogisticRegression(penalty=params[0], dual=True, tol=0.000000001,
C=params[1], fit_intercept=True, intercept_scaling=1,
class_weight=params[2])
# Gridsearch, not used.
if params[3]:
parameters = params[4]
gs = GridSearchCV(model, parameters, scoring='roc_auc', cv=4)
gs.fit(x_train, y_train)
#print "Gridsearch scores:", gs.grid_scores_, "\n"
print "Best params:", gs.best_params_
print "Best score:", gs.best_score_
model = gs.best_estimator_
else:
model.fit(x_train, y_train)
# Predict the sets.
pred_train = model.predict_proba(train_predict)[:, 1]
pred_predict = model.predict_proba(x_predict)[:, 1]
return pred_train, pred_predict
def model_LR(x_train, y_train, train_predict, x_predict, params):
"""
LR.
Input: x_train = training_set,
y_train = labels for the training set,
train_predict = the first set to be predicted,
x_predict = the second set to be predicted,
params, the parameters for the model: [lr_penalty, lr_C, lr_class_weight].
Output: [prediction for first set, prediction for second set].
"""
model = linear_model.LogisticRegression(penalty=params[0], dual=True, tol=0.000000001,
C=params[1], fit_intercept=True, intercept_scaling=1,
class_weight=params[2])
# Gridsearch, not used.
if params[3]:
parameters = params[4]
gs = GridSearchCV(model, parameters, scoring='roc_auc', cv=4)
gs.fit(x_train, y_train)
#print "Gridsearch scores:", gs.grid_scores_, "\n"
print "Best params:", gs.best_params_
print "Best score:", gs.best_score_
model = gs.best_estimator_
else:
model.fit(x_train, y_train)
# Predict the sets.
pred_train = model.predict_proba(train_predict)[:, 1]
pred_predict = model.predict_proba(x_predict)[:, 1]
return pred_train, pred_predict
def model_RIDGE(x_train, y_train, train_predict, x_predict, params):
"""
Ridge regression.
Input: x_train = training_set,
y_train = labels for the training set,
train_predict = the first set to be predicted,
x_predict = the second set to be predicted,
params, the parameters for the model: [alpha, normalize, max_iter, solver].
Output: [prediction for first set, prediction for second set].
"""
model = linear_model.Ridge(alpha=params[0], fit_intercept=True, normalize=params[1],
copy_X=True, max_iter=params[2], tol=0.000001, solver=params[3])
# Gridsearch, not used.
if params[4]:
parameters = params[5]
gs = GridSearchCV(model, parameters, scoring='roc_auc', cv=4)
gs.fit(x_train, y_train)
#print "Gridsearch scores:", gs.grid_scores_, "\n"
print "Best params:", gs.best_params_
print "Best score:", gs.best_score_
model = gs.best_estimator_
else:
model.fit(x_train, y_train)
# Predict the sets.
pred_train = model.predict(train_predict)
pred_predict = model.predict(x_predict)
return pred_train, pred_predict
def complete_model_loop(i, train_sets, y_train, predict_sets, sample_size, n_samples, params, models):
"""
This is the loop needed for the multiprocessing in complete_model.
It will train multiple models on one sample and return those predictions.
"""
# Sample the training set if we have a positive sample size.
if sample_size > 0:
s_y_train, s_y_cv = cross_validation.train_test_split(y_train,
test_size=sample_size,
random_state=SEED+11*i)
cv_sets = [0 for k in range(len(models))]
s_train_sets = [0 for k in range(len(models))]
for j in range(len(models)):
s_train_sets[j], cv_sets[j] = cross_validation.train_test_split(train_sets[j],
test_size=sample_size,
random_state=SEED+11*i)
else:
s_train_sets, s_y_train = train_sets, y_train
scores = [[] for k in range(len(models))]
predict_preds, train_preds = [[] for k in range(len(models))], [[] for k in range(len(models))]
for k in range(len(models)):
train_pred, predict_pred = models[k](s_train_sets[k], s_y_train, train_sets[k], predict_sets[k], params[k])
print "\t Sample %i/%i: \t\t %0.5f" % (i+1, n_samples, roc_auc_score(y_train, train_pred))
predict_preds[k] = predict_pred
train_preds[k] = train_pred
scores[k] = roc_auc_score(y_train, train_pred)
return train_preds, predict_preds, scores
def complete_model(train_sets, y_train, predict_sets, sample_size, n_samples, params, models,
do_ensemble, ensemble_model, ensemble_params):
"""
This function will train multiple models on multiple training sets (but with the same output vector)
and return one one-dimensional prediction.
A "model" consists of a feature or training set, a model function, an appropriate test set and
a list of parameters for the model function.
Input: train_sets = a list of feature sets of a training set,
y_train = labels for the training set (list),
predict_sets = a list of corresponding feature sets of a test set whose labels
are to be predicted.
sample_size = sample size in two layer approach (float in ]0,1[),
n_samples = number of samples in two layer approach,
params = list of parameter lists for each corresponding model,
models = list of model functions,
do_ensemble = whether to use an ensemble model (True) or just a simple average (False),
ensemble_model = when do_ensemble is True, the first element in this list is the ensemble
model that will be used to combine predictions, otherwise it has no use,
ensemble_params = corresponding parameters for the ensemble model, when used.
Output: one prediction for the test set.
Complete model works as follows:
1. Take n_samples random samples s_x_train of sample size sample_size.
2. Train different models on each sample to predict (the base set) x_train and x_predict.
3. Iterate n_samples times to get an array of predictions for x_train and x_predict.
4. Average these predictions to get one prediction of x_train and x_predict per model.
5. Feed the predictions for x_train to another model (or use a simple average).
6. Use this last model to predict with x_predict.
"""
# Multiprocessing for 2 procs.
pool = multiprocessing.Pool(2)
partial_ = partial(complete_model_loop, train_sets=train_sets, y_train=y_train, predict_sets=predict_sets,
sample_size=sample_size, n_samples=n_samples, params=params, models=models)
res = pool.map_async(partial_, range(n_samples), 1)
result = np.asarray(res.get())
pool.close()
# Gather the results.
train_preds = np.vstack([np.asarray(result[j, 0, i]) for i in range(len(models)) for j in range(n_samples)]).T
predict_preds = np.vstack([np.asarray(result[j, 1, i]) for i in range(len(models)) for j in range(n_samples)]).T
scores = np.hstack([np.asarray(result[j, 2, i]) for i in range(len(models)) for j in range(n_samples)])
# The combined model: 1. train a new model on the predictions or 2. use a simple average.
if do_ensemble:
# Train with the predictions made by the models on the training set (predicted with samples of this set).
tmp, predict_result = ensemble_model[0](train_preds, y_train, train_preds, predict_preds, ensemble_params[0])
else:
# Simple average.
predict_result = np.mean(predict_preds, axis=1)
#predict_result = stats.gmean(predict_preds, axis=1)
return predict_result
def diagnostics(ind_cv, cv_preds, y_cv, full_model, models, train_rest, iter, hist, show_plots):
"""
This is a diagnostics function which can show some simple interactive plots with labels.
(Must be enabled in the cross_validate.)
"""
# Color set for the plots.
cols = ['r', 'g', 'k', 'b', 'm', 'r', 'b', 'g', 'k', 'm', 'r', 'g', 'k', 'b']
# We will make a vector of differences (per model): label - predicted.
# Thus, positive difference <=> label 1 and predicted towards 0.
# negative difference <=> label 0 and predicted towards 1.
# If we have predictions from the full model (thus only one prediction).
if full_model:
# The diffs vector.
cv_diffs = np.asarray([a - b for a, b in zip(y_cv, cv_preds)])
df = p.DataFrame(cv_diffs, dtype=object, index=ind_cv)
# The points of differences.
points = df.sort(columns=[0]).ix[:, 0].astype('float64')
# Some labels to easily identify misclassified points (according to trainset).
labels = ["{%i}:%s\n" % (train_rest.ix[row_nr, 'urlid'],
train_rest.ix[row_nr, 'url']) for row_nr in df.sort(columns=[0]).ix[:, 0].index]
fig, ax = pl.subplots()
if hist:
ax.hist(points, color=cols.pop())
ax.margins(0.1)
ax.set_xlim([-1, 1])
ax.set_ylim([0, 1500])
ax.set_title("Full model\n AUC: %0.5f" % roc_auc_score(y_cv, cv_preds))
else:
ax.plot(points, 'ro', color=cols.pop())
ax.margins(0.1)
ax.set_ylim([-1, 1])
ax.set_title("Full model\n AUC: %0.5f" % roc_auc_score(y_cv, cv_preds))
if not hist:
datacursor(axes=ax, point_labels=labels, draggable=True)
# Save image.
now = datetime.datetime.now()
timestamp = now.strftime("%d-%m-%Y")
pl.savefig('figs/%s_full_model_iter_%i_%s_%0.4f.png' % (timestamp, iter, models, roc_auc_score(y_cv, cv_preds)),
bbox_inches=0)
# Show plot.
if show_plots:
pl.show()
else:
# One column/plot per model.
if len(models) > 1:
ncols = len(models)
else:
ncols = 2
fig, axes = pl.subplots(ncols=ncols)
for i in range(len(models)):
# The diffs vector.
cv_diffs = np.asarray([a - b for a, b in zip(y_cv, cv_preds[models[i]])])
df = p.DataFrame(cv_diffs, dtype=object, index=ind_cv)
# The points of differences.
points = df.sort(columns=[0]).ix[:, 0].astype('float64')
# The labels.
if hist:
axes[i].hist(points, color=cols.pop())
axes[i].margins(0.1)
axes[i].set_xlim([-1, 1])
axes[i].set_ylim([0, 1500])
axes[i].set_title("Model %i\n AUC: %0.5f" % (models[i].__name__,
roc_auc_score(y_cv, cv_preds[models[i]])))
else:
labels = ["{%i}:%s\n" % (train_rest.ix[row_nr, 'urlid'],
train_rest.ix[row_nr, 'url'])
for row_nr in df.sort(columns=[0]).ix[:, 0].index]
axes[i].plot(points, 'ro', color=cols.pop())
axes[i].margins(0.1)
axes[i].set_ylim([-1, 1])
axes[i].set_title("Model %i\n AUC: %0.5f" % (models[i].__name__,
roc_auc_score(y_cv, cv_preds[models[i]])))
if not hist:
datacursor(axes=axes[i], point_labels=labels, draggable=True)
# Save image.
now = datetime.datetime.now()
timestamp = now.strftime("%d-%m-%Y")
pl.savefig('figs/%s_models_iter_%i_%s.png' % (timestamp, iter, models),
bbox_inches=0)
# Show plot(s).
if show_plots:
pl.show()
def cross_validate(train_sets, y_train, sample_size, n_samples, cv_size, cv_times, params, full_model,
models, do_ensemble, ensemble_models, ensemble_params, train_rest):
"""
Cross validate the models. Two possibilities:
1. Cross validate on the full model: ensemble predictions of different models.
2. Cross validate each model separately.
Input: train_sets = a list of feature sets of a training set,
y_train = list of labels for the training set,
sample_size = sample size in in two layer approach,
n_samples = number of samples in two layer approach,
cv_size = the percentage (as float in ]0,1[) of the training set to be
used for validation,
cv_times = number of times to do cross validation,
full_model = whether to train on separate models (False) or just the ensemble (True),
params = list of parameter lists for each corresponding model,
models = list of model functions,
do_ensemble = whether to do use an ensemble model (True) or just a simple average (False),
ensemble_model = when do_ensemble is True, the first element in this list is the ensemble
model that will be used to combine predictions, otherwise it has no use,
ensemble_params = corresponding parameters for the ensemble model, when used,
train_rest = some original features to make it easier to use the diagnostics function.
Output: list of AUCs of the cv_times validations.
"""
if full_model:
cv_aucs = []
else:
cv_aucs = [[] for i in range(len(models))]
print "Cross validating ...\n"
for i in range(cv_times):
print "\n****************** CV iteration %i/%i. ******************\n" % (i+1, cv_times)
indices = [k for k in range(len(y_train))]
s_y_train, s_y_cv, ind_train, ind_cv = cross_validation.train_test_split(y_train, indices,
test_size=cv_size,
random_state=SEED+11*i)
cv_sets = [0 for k in range(len(models))]
s_train_sets = [0 for k in range(len(models))]
for j in range(len(models)):
s_train_sets[j], cv_sets[j] = cross_validation.train_test_split(train_sets[j],
test_size=cv_size,
random_state=SEED+11*i)
# Train the combined full model or each model separately?
if full_model:
cv_pred = complete_model(s_train_sets, s_y_train, cv_sets, sample_size, n_samples, params, models,
do_ensemble, ensemble_models, ensemble_params)
cv_aucs.append(roc_auc_score(s_y_cv, cv_pred))
print "\n\t Score: %0.5f.\n" % roc_auc_score(s_y_cv, cv_pred)
# Give predictions cv_pred to diagnostics function.
#diagnostics(ind_cv, cv_pred, s_y_cv, full_model, models, train_rest, i, hist=True, show_plots=False)
else:
# The vector which will contain a prediction per model.
cv_preds = [[] for j in range(len(models))]
# Test every model.
for j in range(len(models)):
print "Model %s \n\t %s\n" % (models[j].__name__, params[j])
cv_pred = complete_model([s_train_sets[j]], s_y_train, [cv_sets[j]], sample_size, n_samples,
[params[j]], [models[j]], do_ensemble, ensemble_models, ensemble_params)
cv_preds[j] = cv_pred
cv_aucs[j].append(roc_auc_score(s_y_cv, cv_pred))
print "Score on CV fold: %0.5f.\n" % roc_auc_score(s_y_cv, cv_pred)
# Give predictions to diagnostics function.
#diagnostics(ind_cv, cv_preds, s_y_cv, full_model, models, train_rest, i, hist=True, show_plots=False)
return cv_aucs
def write_submission(predictions, filename):
print "Writing submission file.\n"
testfile = p.read_csv('data/test.tsv', sep="\t", na_values=['?'], index_col=1)
pred_df = p.DataFrame(predictions, index=testfile.index, columns=['label'])
pred_df.to_csv(filename)
def main():
# Settings for the TF-IDF processing.
sets_ids = [0, # url
0, # title
0, # body
0, # html
0, # links
0] # htmlbody
url_sets = [['word', (1, 1), 0.0001, 0.8], # 1-grams 16774
['word', (2, 2), 0.0002, 0.8], # 2-grams 7500
['word', (3, 3), 0.0002, 0.8], # 3-grams 3700
['char', (3, 6), 0.0005, 0.8]] # orig
title_sets = [['word', (1, 1), 0.0001, 0.8], # 1-grams 10245
['word', (2, 2), 0.0002, 0.8], # 2-grams 7500
['word', (3, 3), 0.0002, 0.8], # 3-grams 3500
['char', (3, 6), 0.0007, 0.80]] # orig
body_sets = [['word', (1, 1), 0.0002, 0.8], # 1-grams 32000
['word', (2, 2), 0.0010, 0.8], # 2-grams 45200
['word', (3, 3), 0.0010, 0.8], # 3-grams 22902
['word', (1, 2), 0.0085, 0.8]] # orig
html_sets = [['word', (1, 1), 0.00015, 0.8], # 1-grams 66074
['word', (2, 2), 0.0010, 0.8], # 2-grams 116000
['word', (3, 3), 0.0010, 0.8], # 3-grams 91278
['word', (1, 1), 0.0045, 0.8]] # orig
links_sets = [['word', (1, 1), 0.0025, 0.8], # 1-grams 13789
['word', (2, 2), 0.0020, 0.8], # 2-grams 35934
['word', (3, 3), 0.0020, 0.8], # 3-grams 41569
['word', (1, 1), 0.0070, 0.8]] # orig
htmlbody_sets = [['word', (1, 1), 0.0002, 0.8], # 1-grams 72000
['word', (2, 2), 0.0010, 0.8], # 2-grams 123000
['word', (3, 3), 0.0010, 0.8], # 3-grams 96500
['word', (1, 1), 0.0065, 0.8]] # orig
names = ['url', 'title', 'body', 'html', 'links', 'htmlbody', 'comb']
########################################################################################
# Kernel PCA 1-grams
sets_ids = [0, 0, 0, 0, 0, 0, 0]
tfidf_url, tfidf_title = url_sets[sets_ids[0]], title_sets[sets_ids[1]]
tfidf_body, tfidf_html = body_sets[sets_ids[2]], html_sets[sets_ids[3]]
tfidf_links, tfidf_htmlbody = links_sets[sets_ids[4]], htmlbody_sets[sets_ids[5]]
data_1grams = load_data(tfidf_url, tfidf_title, tfidf_body, tfidf_html, tfidf_links, tfidf_htmlbody)
data_1grams['train_comb'] = csc_matrix(hstack((data_1grams['train_' + 'url'],
data_1grams['train_' + 'title'],
data_1grams['train_' + 'body'],
data_1grams['train_' + 'html'],
data_1grams['train_' + 'links'],
data_1grams['train_' + 'htmlbody'])))
data_1grams['test_comb'] = csc_matrix(hstack((data_1grams['test_' + 'url'],
data_1grams['test_' + 'title'],
data_1grams['test_' + 'body'],
data_1grams['test_' + 'html'],
data_1grams['test_' + 'links'],
data_1grams['test_' + 'htmlbody'])))
train_rest = data_1grams['train_rest']
test_rest = data_1grams['test_rest']
y = data_1grams['y']
train_kpca_1grams = {}
test_kpca_1grams = {}
n_components = [800, 500, 100, 600, 1200, 700, 300] # 800, 500, 100, 600, 1200, 700, 300
# Get KPCA-transformated data.
print "\n Doing KPCA transformations for 1-grams with %s components ...\n" % n_components
for j in range(len(names)):
# Try to get from dump first.
try:
train_kpca_1grams[names[j]] = pickle.load(open("dump/kpca_train_%s_%i_comp_%i.pkl" % (names[j],
sets_ids[j],
n_components[j]),
"rb"))
test_kpca_1grams[names[j]] = pickle.load(open("dump/kpca_test_%s_%i_comp_%i.pkl" % (names[j],
sets_ids[j],
n_components[j]),
"rb"))
# If it fails, then fit again.
except:
print "\t Could not read KPCA for %s from dump, fitting ..." % names[j]
kpca = KernelPCA(n_components=n_components[j], kernel='linear')
kpca.fit(vstack((data_1grams['train_' + names[j]], data_1grams['test_' + names[j]])))
train_kpca_1grams[names[j]] = kpca.transform(data_1grams['train_' + names[j]])
test_kpca_1grams[names[j]] = kpca.transform(data_1grams['test_' + names[j]])
with open("dump/kpca_train_%s_%i_comp_%i.pkl" % (names[j], sets_ids[j], n_components[j]), "wb") as f:
pickle.dump(train_kpca_1grams[names[j]], f, pickle.HIGHEST_PROTOCOL)
with open("dump/kpca_test_%s_%i_comp_%i.pkl" % (names[j], sets_ids[j], n_components[j]), "wb") as f:
pickle.dump(test_kpca_1grams[names[j]], f, pickle.HIGHEST_PROTOCOL)
print "\t Dumped."
print " Done.\n"
########################################################################################
# Kernel PCA 2-grams
sets_ids = [1, 1, 1, 1, 1, 1, 1]
tfidf_url, tfidf_title = url_sets[sets_ids[0]], title_sets[sets_ids[1]]
tfidf_body, tfidf_html = body_sets[sets_ids[2]], html_sets[sets_ids[3]]
tfidf_links, tfidf_htmlbody = links_sets[sets_ids[4]], htmlbody_sets[sets_ids[5]]
data_2grams = load_data(tfidf_url, tfidf_title, tfidf_body, tfidf_html, tfidf_links, tfidf_htmlbody)
data_2grams['train_comb'] = csc_matrix(hstack((data_2grams['train_' + 'url'],
data_2grams['train_' + 'title'],
data_2grams['train_' + 'body'],
data_2grams['train_' + 'html'],
data_2grams['train_' + 'links'],
data_2grams['train_' + 'htmlbody'])))
data_2grams['test_comb'] = csc_matrix(hstack((data_2grams['test_' + 'url'],
data_2grams['test_' + 'title'],
data_2grams['test_' + 'body'],
data_2grams['test_' + 'html'],
data_2grams['test_' + 'links'],
data_2grams['test_' + 'htmlbody'])))
# Get KPCA-transformed data.
train_kpca_2grams = {}
test_kpca_2grams = {}
n_components = [1300, 1100, 500, 500, 500, 500, 300] # 1300, 1100, 500, 500, 500, 500, 300
print "\n Doing KPCA transformations for 2-grams with %s components ...\n" % n_components
for j in range(len(names)):
# Try to get from dump first.
try: