May-02-2019, 01:19 AM
In case of
You can read about advanced indexing [here](https://docs.scipy.org/doc/numpy/referen...exing.html).
Adv. indexing always returns a copy of the data, so using
is triggered because you pass an array of integers to
From official docs:
You can try the following examples:
Extract_trial1
, when you invoke A[index, :]
it triggers advanced indexing of Numpy.You can read about advanced indexing [here](https://docs.scipy.org/doc/numpy/referen...exing.html).
Adv. indexing always returns a copy of the data, so using
np.copy
is redundant here. Advanced indexingis triggered because you pass an array of integers to
A[...]
. From official docs:
Output:Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool).
You can inspect this by printing shape of the index variable (it is randomly changed between runs):index = np.where(A[:,0] == 1) print(np.array(index).shape)Lets look at the advanced indexing broadcasting formula:
result[i_1, ..., i_M] == x[ind_1[i_1, ..., i_M], ind_2[i_1, ..., i_M], ..., ind_N[i_1, ..., i_M]]
ind_1
is your index variable, (ind_2
= ':' in your case, that is simple indexing); ind_1
has shape (1, small random integer)
, so result shape will be (1, small_random_integer, 10)
. This is what you are having regarding Extract_trial1
.You can try the following examples:
A[[1,2,3], :] => shape = (3, 10) A[[[1,2,3],], :] => shape (1, 3, 10) A[[[[1,2,3],]], :] => shape (1, 1, 3, 10)To fix this behavior you need to pass
1d
array of indices to A[...]
, i.e. A[index[0], :]
.