Skip to content

Instantly share code, notes, and snippets.

@corochann
Created August 21, 2017 04:39
Show Gist options
  • Save corochann/978430e62197baf379d208acb116d28e to your computer and use it in GitHub Desktop.
Save corochann/978430e62197baf379d208acb116d28e to your computer and use it in GitHub Desktop.
Access Chainer dataset using indexer
import numpy as np
import chainer
from chainer.datasets import TupleDataset
train, test = chainer.datasets.get_mnist()
# 1. Extract data easily without explicitly call concat_examples
imgs, labels = train.ix[:, :]
#imgs, labels = train.ix[:] # same
print('imgs[2]', imgs[2].shape)
#print('imgs[2]', imgs[2])
print('labels[2]', labels[2])
# 2. Extract only labels, without extracting unnecessary imgs data
labels = train.ix[:, 1]
print('labels, len', len(labels))
print('labels', labels[:10])
# 3. Extract index whose label is 3, only for first 1000 dataset.
label3_index = np.argwhere(train.ix[:1000, 1] == 3)
print('label3_index', label3_index)
# Get image whose label is 3
imgs3 = train.ix[label3_index, 0]
print('imgs3', imgs3.shape)
# 4. Extract images whose label is 5 (Test for bool index access at axis=0)
imgs5 = train.ix[train.ix[:, 1] == 5, 0]
print('imgs5', imgs5.shape)
# 5. Test for bool index access at axis=0
#labels2 = train.ix[train.ix[:, 1] == 2, [False, True]]
#print('labels2', len(labels2), labels2)
tuple_dataset = TupleDataset([0, 1, 2], [0, 1, 4])
targets = tuple_dataset.ix[:, 1]
print('targets', targets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment