Reputation: 105
There is an numpy calculation process as below, which has been approved correctly, but I don't understand the process clearly and would like to ask for help. The steps as following:
May I ask for your help to explain it a bit?
Thanks.
Upvotes: 0
Views: 40
Reputation: 1068
u_train
is a 2-dimensional array. In NumPy, you can use lists as slices. It's easiest to imagine with an example: Assume I have a 4x4 array named arr
. Then,
arr[3, 1]
gives me the value in row 3, column 1 (remember that we count from 0).arr[[1, 3], [0, 2]]
gives me a 2x1 array with the following original indices: that is, row 1 and 3, column 0 and 2.+--------+--------+
| (1, 0) | (1, 2) |
+--------+--------+
| (3, 0) | (3, 2) |
+--------+--------+
arr[[0, 3], :]
gives me the row 0 and 3, all columns, since :
implies everything in that axis:+--------+--------+--------+--------+
| (0, 0) | (0, 1) | (0, 2) | (0, 3) |
+--------+--------+--------+--------+
| (3, 0) | (3, 1) | (3, 2) | (3, 3) |
+--------+--------+--------+--------+
arr[3]
is identical to arr[3, :]
, this is just syntactic sugar.True
in the list means include the row of the same index as it, and vice versa. For example,
arr[:, [True, True, False, False]]
is equivalent to arr[:, [0, 1]]
.
Note that, perhaps obviously, the length of the boolean list must be the same as the dimension, so arr[:, [True, False, True]]
will raise an IndexError
.Upvotes: 1