Nicolas Gervais
Nicolas Gervais

Reputation: 36684

Why is it so important to load the Keras MNIST with a pair of tuples?

In the documentation, they recommend using this line to load the mnist:

from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

If you don't use tuples or lists, it will raise an error:

ValueError: not enough values to unpack (expected 4, got 2)

It also works with lists. Why is it so important to use tuples or lists? With sklearn, it isn't a problem to use

X_train, X_test, y_train, y_test = train_test_split(X, y)

So why can't it work here?

Upvotes: 1

Views: 304

Answers (1)

thushv89
thushv89

Reputation: 11333

If you look at the keras.mnist.load_data() it just returns two tuples exactly. So you need exactly two tuples or lists for the unpacking to happen correctly.

But if you look at scikit.model_selection.train_test_split, it does this smartly by creating a chain from iteratables using itertools.chain, which allows you to do the unpacking using four elements.

If you want keras.mnist.load_data() to work similarly do the following.

from itertools import chain
x1, y1, x2, y2 = chain.from_iterable(tf.keras.datasets.mnist.load_data())

Upvotes: 2

Related Questions