0

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

I'm still in the middle of learning the basic Python functions, and this is from the tensorflow website. What does this line do?

I've already learned Java, but so this is new to me. This line seems to initiate the variables x_train, y_train, x_test, and y_test, but if so I don't understand why there would be four variables formatted in such a manner.

Please explain it to a person who doesn't know Python, but has already learned Java.

According to tensorflow, mnist is a dataset.

  • Do you mean what's the purpose or how it's possible to assign the values this way? – Ignatius Reilly Jul 13 '22 at 02:50
  • This is a multiple assignment, like `t = (0, 1, (2, 3, 4))` you can try using `a, b, c = t` , and `x_train` contains the image to be trained, `y_train` is the label, which number of the corresponding x_train is marked, `x_test` , `y_test` are used to verify the use of the model at the end, observe the size of the data set – Ian Jul 13 '22 at 02:55
  • This is called destructuring. A tutorial on this topic: [Destructuring in Python](https://blog.teclado.com/destructuring-in-python/) – DarrylG Jul 13 '22 at 03:36
  • 1
    You should really search this site, we have very similar questions asked and answered. – Dr. Snoopy Jul 13 '22 at 06:44

1 Answers1

2

If x = (1,2) is binding a tuple value to a variable.

If (1, 2) is a structure.

Then (x, y) = (1, 2) is a de-structuring bind giving x=1 and y=2.

The structures of variables and data must match. The structure is removed, and the values are bound to the variables in the same position.

(x_train, y_train), (x_test, y_test) = (1, 2), (3, 4)

gives x_train = 1, y_train = 2, x_test = 3, y_test = 4.

It's a convenience instead of writing:

temp = mnist.load_data()
x_train = temp[0][0]
y_train = temp[0][1]
x_test  = temp[1][0]
y_test  = temp[1][1]

or etc.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87