-
Notifications
You must be signed in to change notification settings - Fork 38
/
datasets.py
560 lines (413 loc) · 20.5 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
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
# -*- coding: utf-8 -*-
"""
Datasets module.
This module contains functions that allow loading datasets for easy
testing of pipelines and primitives over multiple data modalities
and task types.
The available datasets by data modality and task type are:
+------------------+---------------+---------------------------+-------------------+
| Dataset | Data Modality | Task Type | Task Subtype |
+==================+===============+===========================+===================+
| Amazon | Graph | Community Detection | |
+------------------+---------------+---------------------------+-------------------+
| DIC28 | Graph | Graph Matching | |
+------------------+---------------+---------------------------+-------------------+
| UMLs | Graph | Link Prediction | |
+------------------+---------------+---------------------------+-------------------+
| Nomination | Graph | Vertex Nomination | |
+------------------+---------------+---------------------------+-------------------+
| USPS | Image | Classification | Binary |
+------------------+---------------+---------------------------+-------------------+
| Hand Geometry | Image | Regression | Univariate |
+------------------+---------------+---------------------------+-------------------+
| Iris | Single Table | Classification | Multiclass |
+------------------+---------------+---------------------------+-------------------+
| Census | Single Table | Classification | Binary |
+------------------+---------------+---------------------------+-------------------+
| Jester | Single Table | Collaborative Filtering | |
+------------------+---------------+---------------------------+-------------------+
| Boston | Single Table | Regression | Univariate |
+------------------+---------------+---------------------------+-------------------+
| Boston Multitask | Single Table | Regression | Multivariate |
+------------------+---------------+---------------------------+-------------------+
| Wiki QA | Multi Table | Classification | Binary |
+------------------+---------------+---------------------------+-------------------+
| Personae | Text | Classification | Binary |
+------------------+---------------+---------------------------+-------------------+
| News Groups | Text | Classification | Multiclass |
+------------------+---------------+---------------------------+-------------------+
| Paper Reviews | Text | Regression | Univariate |
+------------------+---------------+---------------------------+-------------------+
"""
import io
import logging
import os
import tarfile
import urllib
import networkx as nx
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.metrics import accuracy_score, normalized_mutual_info_score, r2_score
from sklearn.model_selection import KFold, StratifiedKFold, train_test_split
LOGGER = logging.getLogger(__name__)
INPUT_SHAPE = [224, 224, 3]
DATA_PATH = os.path.join(
os.path.dirname(__file__),
'data'
)
DATA_URL = 'http://mlprimitives.s3.amazonaws.com/{}.tar.gz'
class Dataset():
"""Dataset class.
This class represents the abstraction of a dataset and works as
a container of all the things needed in order to use a dataset
for testing.
Among other things, it includes the actual dataset data, information
about its origin, a score function that works for this dataset,
and a method to split the data in multiple ways for goodnes-of-fit
evaluation.
Attributes:
name (str): Name of this dataset.
description (str): Short description about the data that composes this dataset.
data (array-like): Numpy array or pandas DataFrame containing all the data of
this dataset, excluding the labels or target values.
target (array-like): Numpy array or pandas Series containing the expected labels
or values
**kwargs: Any additional keyword argument passed on initailization is also
available as instance attributes.
Args:
description (str): Short description about the data that composes this dataset.
The first line of the description is expected to be a human friendly
name for the dataset, and will be set as the `name` attribute.
data (array-like): Numpy array or pandas DataFrame containing all the data of
this dataset, excluding the labels or target values.
target (array-like): Numpy array or pandas Series containing the expected labels
or values
score (callable): Function that will be used to compute the score of this dataset.
shuffle (bool): Whether or not to shuffle the data before splitting.
stratify (bool): Whther to use a stratified or regular KFold for splitting.
**kwargs: Any additional keyword argument passed on initialization will be made
available as instance attributes.
"""
def __init__(self, description, data, target, score, data_modality, task_type,
task_subtype=None, shuffle=True, stratify=False, **kwargs):
self.name = description.splitlines()[0]
self.description = description
self.data = data
self.target = target
self.metric = score.__name__
self.data_modality = data_modality
self.task_type = task_type
self.task_subtype = task_subtype
self._stratify = stratify
self._shuffle = shuffle
self._score = score
self.extras = kwargs
self.__dict__.update(kwargs)
def score(self, *args, **kwargs):
"""Scoring function for this dataset.
Args:
\\*args, \\*\\*kwargs: Any given arguments and keyword arguments will be
directly passed to the given scoring function.
Returns:
float:
The computed score.
"""
return self._score(*args, **kwargs)
def __repr__(self):
return self.name
def describe(self):
"""Print the description of this Dataset on stdout."""
print(self.description)
print('Data Modality: {}'.format(self.data_modality))
print('Task Type: {}'.format(self.task_type))
print('Task Subtype: {}'.format(self.task_subtype))
print('Data shape: {}'.format(self.data.shape))
print('Target shape: {}'.format(self.target.shape))
print('Metric: {}'.format(self.metric))
print('Extras: {}'.format(', '.join(self.extras.keys())))
@staticmethod
def _get_split(data, index):
if hasattr(data, 'iloc'):
return data.iloc[index]
else:
return data[index]
def get_splits(self, n_splits=1, random_state=0):
"""Return splits of this dataset ready for Cross Validation.
If n_splits is 1, a tuple containing the X for train and test
and the y for train and test is returned.
Otherwise, if n_splits is bigger than 1, a list of such tuples
is returned, one for each split.
Args:
n_splits (int): Number of times that the data needs to be splitted.
Returns:
tuple or list:
if n_splits is 1, a tuple containing the X for train and test
and the y for train and test is returned.
Otherwise, if n_splits is bigger than 1, a list of such tuples
is returned, one for each split.
"""
if n_splits == 1:
stratify = self.target if self._stratify else None
return train_test_split(
self.data,
self.target,
shuffle=self._shuffle,
stratify=stratify,
random_state=random_state
)
else:
cv_class = StratifiedKFold if self._stratify else KFold
cv = cv_class(n_splits=n_splits, shuffle=self._shuffle, random_state=random_state)
splits = list()
for train, test in cv.split(self.data, self.target):
X_train = self._get_split(self.data, train)
y_train = self._get_split(self.target, train)
X_test = self._get_split(self.data, test)
y_test = self._get_split(self.target, test)
splits.append((X_train, X_test, y_train, y_test))
return splits
def _download(dataset_name, dataset_path):
url = DATA_URL.format(dataset_name)
LOGGER.debug('Downloading dataset %s from %s', dataset_name, url)
response = urllib.request.urlopen(url)
bytes_io = io.BytesIO(response.read())
LOGGER.debug('Extracting dataset into %s', DATA_PATH)
with tarfile.open(fileobj=bytes_io, mode='r:gz') as tf:
tf.extractall(DATA_PATH)
def _load(dataset_name):
if not os.path.exists(DATA_PATH):
os.makedirs(DATA_PATH)
dataset_path = os.path.join(DATA_PATH, dataset_name)
if not os.path.exists(dataset_path):
_download(dataset_name, dataset_path)
return dataset_path
def _load_images(image_dir, filenames):
# Lazy loading of keras to avoid unnecessary keras initializations
from keras.preprocessing.image import img_to_array, load_img # noqa
LOGGER.debug('Loading %s images from %s', len(filenames), image_dir)
images = []
for filename in filenames:
filename = os.path.join(image_dir, filename)
image = load_img(filename)
image = image.resize(tuple(INPUT_SHAPE[0:2]))
image = img_to_array(image)
image = image / 255.0 # Quantize images.
images.append(image)
return np.array(images)
def _load_csv(dataset_path, name, set_index=False):
csv_path = os.path.join(dataset_path, name + '.csv')
LOGGER.debug('Loading csv %s', csv_path)
df = pd.read_csv(csv_path)
if set_index:
df = df.set_index(df.columns[0], drop=False)
return df
def load_usps():
"""USPs Digits dataset.
The data of this dataset is a 3d numpy array vector with shape (224, 224, 3)
containing 9298 224x224 RGB photos of handwritten digits, and the target is
a 1d numpy integer array containing the label of the digit represented in
the image.
"""
dataset_path = _load('usps')
df = _load_csv(dataset_path, 'data')
X = _load_images(os.path.join(dataset_path, 'images'), df.image)
y = df.label.values
return Dataset(load_usps.__doc__, X, y, accuracy_score, 'image',
'classification', 'binary', stratify=True)
def load_handgeometry():
"""Hand Geometry dataset.
The data of this dataset is a 3d numpy array vector with shape (224, 224, 3)
containing 112 224x224 RGB photos of hands, and the target is a 1d numpy
float array containing the width of the wrist in centimeters.
"""
dataset_path = _load('handgeometry')
df = _load_csv(dataset_path, 'data')
X = _load_images(os.path.join(dataset_path, 'images'), df.image)
y = df.target.values
return Dataset(load_handgeometry.__doc__, X, y, r2_score, 'image', 'regression', 'univariate')
def load_personae():
"""Personae dataset.
The data of this dataset is a 2d numpy array vector containing 145 entries
that include texts written by Dutch users in Twitter, with some additional
information about the author, and the target is a 1d numpy binary integer
array indicating whether the author was extrovert or not.
"""
dataset_path = _load('personae')
X = _load_csv(dataset_path, 'data')
y = X.pop('label').values
return Dataset(load_personae.__doc__, X, y, accuracy_score, 'text',
'classification', 'binary', stratify=True)
def load_reviews():
"""Paper Reviews Dataset.
The data set consists of paper reviews sent to an international conference mostly in Spanish
(some are in English). It has a total of N = 405 instances evaluated with a 5-point scale
('-2': very negative, '-1': negative, '0': neutral, '1': positive, '2': very positive),
expressing the reviewer's opinion about the paper and the orientation perceived by a reader
who does not know the reviewer's evaluation (more details in the attributes' section).
The distribution of the original scores is more uniform in comparison to the revised scores.
This difference is assumed to come from a discrepancy between the way the paper is evaluated
and the way the review is written by the original reviewer.
source: "UCI
sourceURI: "https://archive.ics.uci.edu/ml/datasets/Paper+Reviews"
"""
dataset_path = _load('reviews')
X = _load_csv(dataset_path, 'data')
y = X.pop('evaluation').values
return Dataset(load_reviews.__doc__, X, y, r2_score, 'text', 'regression', 'univariate')
def load_umls():
"""UMLs dataset.
The data consists of information about a 135 Graph and the relations between
their nodes given as a DataFrame with three columns, source, target and type,
indicating which nodes are related and with which type of link. The target is
a 1d numpy binary integer array indicating whether the indicated link exists
or not.
"""
dataset_path = _load('umls')
X = _load_csv(dataset_path, 'data')
y = X.pop('label').values
graph = nx.Graph(nx.read_gml(os.path.join(dataset_path, 'graph.gml')))
return Dataset(load_umls.__doc__, X, y, accuracy_score, 'graph', 'link_prediction',
stratify=True, graph=graph)
def load_dic28():
"""DIC28 dataset from Pajek.
This network represents connections among English words in a dictionary.
It was generated from Knuth's dictionary. Two words are connected by an
edge if we can reach one from the other by
- changing a single character (e. g., work - word)
- adding / removing a single character (e. g., ever - fever).
There exist 52,652 words (vertices in a network) having 2 up to 8 characters
in the dictionary. The obtained network has 89038 edges.
"""
dataset_path = _load('dic28')
X = _load_csv(dataset_path, 'data')
y = X.pop('label').values
graph1 = nx.Graph(nx.read_gml(os.path.join(dataset_path, 'graph1.gml')))
graph2 = nx.Graph(nx.read_gml(os.path.join(dataset_path, 'graph2.gml')))
graph = graph1.copy()
graph.add_nodes_from(graph2.nodes(data=True))
graph.add_edges_from(graph2.edges)
graph.add_edges_from(X[['graph1', 'graph2']].values)
graphs = {
'graph1': graph1,
'graph2': graph2,
}
return Dataset(load_dic28.__doc__, X, y, accuracy_score, 'graph', 'graph_matching',
stratify=True, graph=graph, graphs=graphs)
def load_nomination():
"""Nomination dataset.
Sample 1 of graph vertex nomination data from MIT Lincoln Lab.
Data consists of one graph whose nodes contain two attributes, attr1 and attr2.
Associated with each node is a label that has to be learned and predicted.
"""
dataset_path = _load('nomination')
X = _load_csv(dataset_path, 'data')
y = X.pop('label').values
graph = nx.Graph(nx.read_gml(os.path.join(dataset_path, 'graph.gml')))
return Dataset(load_nomination.__doc__, X, y, accuracy_score, 'graph', 'vertex_nomination',
stratify=True, graph=graph)
def load_amazon():
"""Amazon dataset.
Amazon product co-purchasing network and ground-truth communities.
Network was collected by crawling Amazon website. It is based on Customers Who Bought
This Item Also Bought feature of the Amazon website. If a product i is frequently
co-purchased with product j, the graph contains an undirected edge from i to j.
Each product category provided by Amazon defines each ground-truth community.
"""
dataset_path = _load('amazon')
X = _load_csv(dataset_path, 'data')
y = X.pop('label').values
graph = nx.Graph(nx.read_gml(os.path.join(dataset_path, 'graph.gml')))
return Dataset(load_amazon.__doc__, X, y, normalized_mutual_info_score, 'graph',
'community_detection', graph=graph)
def load_jester():
"""Jester dataset.
Ratings from the Jester Online Joke Recommender System.
This dataset consists of over 1.7 million instances of (user_id, item_id, rating)
triples, which is split 50-50 into train and test data.
source: "University of California Berkeley, CA"
sourceURI: "http://eigentaste.berkeley.edu/dataset/"
"""
dataset_path = _load('jester')
X = _load_csv(dataset_path, 'data')
y = X.pop('rating').values
return Dataset(load_jester.__doc__, X, y, r2_score, 'single_table', 'collaborative_filtering')
def load_census():
"""Adult Census dataset.
Predict whether income exceeds $50K/yr based on census data. Also known as "Adult" dataset.
Extraction was done by Barry Becker from the 1994 Census database. A set of reasonably clean
records was extracted using the following conditions: ((AAGE>16) && (AGI>100) &&
(AFNLWGT>1)&& (HRSWK>0))
Prediction task is to determine whether a person makes over 50K a year.
source: "UCI
sourceURI: "https://archive.ics.uci.edu/ml/datasets/census+income"
"""
dataset_path = _load('census')
X = _load_csv(dataset_path, 'data')
y = X.pop('income').values
return Dataset(load_census.__doc__, X, y, accuracy_score, 'single_table',
'classification', 'binary', stratify=True)
def load_wikiqa():
"""WikiQA dataset.
A Challenge Dataset for Open-Domain Question Answering.
WikiQA dataset is a publicly available set of question and sentence (QS) pairs,
collected and annotated for research on open-domain question answering.
source: "Microsoft"
sourceURI: "https://www.microsoft.com/en-us/research/publication/wikiqa-a-challenge-dataset-for-open-domain-question-answering/#"
""" # noqa
dataset_path = _load('wikiqa')
data = _load_csv(dataset_path, 'data', set_index=True)
questions = _load_csv(dataset_path, 'questions', set_index=True)
sentences = _load_csv(dataset_path, 'sentences', set_index=True)
vocabulary = _load_csv(dataset_path, 'vocabulary', set_index=True)
entities = {
'data': (data, 'd3mIndex', None),
'questions': (questions, 'qIndex', None),
'sentences': (sentences, 'sIndex', None),
'vocabulary': (vocabulary, 'index', None)
}
relationships = [
('questions', 'qIndex', 'data', 'qIndex'),
('sentences', 'sIndex', 'data', 'sIndex')
]
target = data.pop('isAnswer').values
return Dataset(load_wikiqa.__doc__, data, target, accuracy_score, 'multi_table',
'classification', 'binary', startify=True,
entities=entities, relationships=relationships)
def load_newsgroups():
"""20 News Groups dataset.
The data of this dataset is a 1d numpy array vector containing the texts
from 11314 newsgroups posts, and the target is a 1d numpy integer array
containing the label of one of the 20 topics that they are about.
"""
dataset = datasets.fetch_20newsgroups()
return Dataset(load_newsgroups.__doc__, np.array(dataset.data), dataset.target,
accuracy_score, 'text', 'classification', 'multiclass', stratify=True)
def load_iris():
"""Iris dataset."""
dataset = datasets.load_iris()
return Dataset(load_iris.__doc__, dataset.data, dataset.target,
accuracy_score, 'single_table', 'classification',
'multiclass', stratify=True)
def load_boston():
"""Boston House Prices dataset."""
dataset = datasets.load_boston()
return Dataset(load_boston.__doc__, dataset.data, dataset.target, r2_score,
'single_table', 'regression', 'univariate')
def load_boston_multitask():
"""Boston House Prices dataset.
Modified version of the Boston dataset with a synthetic multitask output.
The multitask output is obtained by applying a linear transformation
to the original y and adding it as a second output column.
"""
dataset = datasets.load_boston()
y = dataset.target
target = np.column_stack([y, 2 * y + 5])
return Dataset(load_boston.__doc__, dataset.data, target, r2_score,
'single_table', 'regression', 'multivariate')
def load_dataset(name):
loader_name = 'load_' + name
loader = globals().get(loader_name)
if not loader:
raise ValueError('Unknown dataset: "{}"'.format(name))
return loader()