-
Notifications
You must be signed in to change notification settings - Fork 30
/
datasets.py
179 lines (159 loc) · 6.46 KB
/
datasets.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
"""
Dataset loading functions
@author Florent Forest
@version 1.0
"""
import numpy as np
def load_mnist(flatten=True, validation=False):
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Divide by 255.
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
if flatten: # flatten to 784-dimensional vector
x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)
else:
x_train = x_train.reshape(*x_train.shape, 1)
x_test = x_test.reshape(*x_test.shape, 1)
if validation: # Return train and test set
return (x_train, y_train), (x_test, y_test)
else: # Return only train set with all images
x = np.concatenate((x_train, x_test))
y = np.concatenate((y_train, y_test))
return (x, y), (None, None)
def load_fashion_mnist(flatten=True, validation=False):
from keras.datasets import fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
# Divide by 255.
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
if flatten: # flatten to 784-dimensional vector
x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)
else:
x_train = x_train.reshape(*x_train.shape, 1)
x_test = x_test.reshape(*x_test.shape, 1)
if validation: # Return train and test set
return (x_train, y_train), (x_test, y_test)
else: # Return only train set with all images
x = np.concatenate((x_train, x_test))
y = np.concatenate((y_train, y_test))
return (x, y), (None, None)
def load_usps(data_path='./data/usps', validation=False):
import h5py
with h5py.File(data_path+'/usps.h5', 'r') as hf:
train = hf.get('train')
x_train = train.get('data')[:]
y_train = train.get('target')[:]
test = hf.get('test')
x_test = test.get('data')[:]
y_test = test.get('target')[:]
if validation: # Return train and test set
return (x_train, y_train), (x_test, y_test)
else: # Return only train set with all images
x = np.concatenate((x_train, x_test))
y = np.concatenate((y_train, y_test))
return (x, y), (None, None)
def load_reuters(data_path='./data/reuters', validation=False):
import os
if not os.path.exists(os.path.join(data_path, 'reutersidf10k.npy')):
print('making reuters idf features')
make_reuters_data(data_path)
print(('reutersidf saved to ' + data_path))
data = np.load(os.path.join(data_path, 'reutersidf10k.npy')).item()
# has been shuffled
x = data['data']
y = data['label']
x = x.reshape((x.shape[0], -1)).astype('float64')
y = y.reshape((y.size,))
if validation:
x_train = x[:7769]
y_train = y[:7769]
x_test = x[7769:]
y_test = y[7769:]
return (x_train, y_train), (x_test, y_test)
else:
return (x, y), (None, None)
def make_reuters_data(data_dir):
"""
NOTE: RCV1-V2 data is heavy and not included.
The data can be downloaded from http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/lyrl2004_rcv1v2_README.htm
Necessary files are:
'rcv1-v2.topics.qrels'
'lyrl2004_tokens_test_pt0.dat'
'lyrl2004_tokens_test_pt1.dat',
'lyrl2004_tokens_test_pt2.dat',
'lyrl2004_tokens_test_pt3.dat',
'lyrl2004_tokens_train.dat'
"""
np.random.seed(1234)
from sklearn.feature_extraction.text import CountVectorizer
from os.path import join
did_to_cat = {}
cat_list = ['CCAT', 'GCAT', 'MCAT', 'ECAT']
with open(join(data_dir, 'rcv1-v2.topics.qrels')) as fin:
for line in fin.readlines():
line = line.strip().split(' ')
cat = line[0]
did = int(line[1])
if cat in cat_list:
did_to_cat[did] = did_to_cat.get(did, []) + [cat]
# did_to_cat = {k: did_to_cat[k] for k in list(did_to_cat.keys()) if len(did_to_cat[k]) > 1}
for did in list(did_to_cat.keys()):
if len(did_to_cat[did]) > 1:
del did_to_cat[did]
dat_list = ['lyrl2004_tokens_test_pt0.dat',
'lyrl2004_tokens_test_pt1.dat',
'lyrl2004_tokens_test_pt2.dat',
'lyrl2004_tokens_test_pt3.dat',
'lyrl2004_tokens_train.dat']
data = []
target = []
cat_to_cid = {'CCAT': 0, 'GCAT': 1, 'MCAT': 2, 'ECAT': 3}
del did
for dat in dat_list:
with open(join(data_dir, dat)) as fin:
for line in fin.readlines():
if line.startswith('.I'):
if 'did' in locals():
assert doc != ''
if did in did_to_cat:
data.append(doc)
target.append(cat_to_cid[did_to_cat[did][0]])
did = int(line.strip().split(' ')[1])
doc = ''
elif line.startswith('.W'):
assert doc == ''
else:
doc += line
print((len(data), 'and', len(did_to_cat)))
assert len(data) == len(did_to_cat)
x = CountVectorizer(dtype=np.float64, max_features=2000).fit_transform(data)
y = np.asarray(target)
from sklearn.feature_extraction.text import TfidfTransformer
x = TfidfTransformer(norm='l2', sublinear_tf=True).fit_transform(x)
x = x[:10000].astype(np.float32)
print(x.dtype, x.size)
y = y[:10000]
x = np.asarray(x.todense()) * np.sqrt(x.shape[1])
print('todense succeed')
p = np.random.permutation(x.shape[0])
x = x[p]
y = y[p]
print('permutation finished')
assert x.shape[0] == y.shape[0]
x = x.reshape((x.shape[0], -1))
np.save(join(data_dir, 'reutersidf10k.npy'), {'data': x, 'label': y})
def load_data(dataset_name, flatten=True, validation=False):
if dataset_name == 'mnist':
return load_mnist(flatten=flatten, validation=validation)
elif dataset_name == 'fmnist':
return load_fashion_mnist(flatten=flatten, validation=validation)
elif dataset_name == 'usps':
return load_usps(validation=validation)
elif dataset_name == 'reuters10k' or dataset_name == 'reuters':
return load_reuters(validation=validation)
else:
print('Dataset {} not available! Available datasets are mnist, fmnist, usps and reuters10k.'.format(dataset_name))
exit(0)