Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

python - numpy.where , numpy.take and indices on multidimensional arrays

I've got 2-d array that looks like this:

my_array
array([[6, 1, 4],
       [4, 8, 4],
       [6, 3, 5]])

After using np.where I've got a tuple with two arrays for both dimensions

indices = np.where(my_array > 4)


(array([0, 1, 2, 2], dtype=int64), 
array([0, 1, 0, 2], dtype=int64))

my question are

  1. is there a method to turn these arrays into iterable set of paired indices without iterating through them with a for loop?

    what I would like to get is a list(?) of tuples with pairs of indices that I could directly use over array to get the objects one at a time.

    So in this case

    paired_indices = [ (0,0), (1,1), (2,0), (2,2) ]


    my_array[paired_indices[0]]
    6

    my_array[paired_indices[1]]
    8
  1. How can I use numpy.take(my_array, indices) to get the proper elements when the arrays are multidimensional? It works fine on 1-dimensional array, but I couldn't figure so far how to deal with more dimensions.

    when I use:

    a = np.take(my_array, indices)

the result is:

    array([[6, 1, 4, 4],
           [6, 1, 6, 4]])
  1. how was the result of
    a = np.take(my_array, indices)

calculated into below, and how could you interpret this result?

    array([[6, 1, 4, 4],
           [6, 1, 6, 4]])
question from:https://stackoverflow.com/questions/65888374/numpy-where-numpy-take-and-indices-on-multidimensional-arrays

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
In [13]: arr = np.array([[6, 1, 4],
    ...:        [4, 8, 4],
    ...:        [6, 3, 5]])
In [14]: idx = np.nonzero(arr>4)
In [15]: idx
Out[15]: (array([0, 1, 2, 2]), array([0, 1, 0, 2]))

This tuple can be used directly to index the array, the result being a 1d array of the >4 values:

In [16]: arr[idx]
Out[16]: array([6, 8, 6, 5])

it can also be used to modify values

In [17]: arr1 = arr.copy()
In [18]: arr1[idx] = 10
In [19]: arr1
Out[19]: 
array([[10,  1,  4],
       [ 4, 10,  4],
       [10,  3, 10]])

You can get an array index pairs with:

In [20]: np.argwhere(arr>4)
Out[20]: 
array([[0, 0],
       [1, 1],
       [2, 0],
       [2, 2]])

argwhere just applies transpose to the where:

In [21]: np.transpose(idx)
Out[21]: 
array([[0, 0],
       [1, 1],
       [2, 0],
       [2, 2]])

arr[idx] is the equivalent of:

In [22]: arr[idx[0],idx[1]]
Out[22]: array([6, 8, 6, 5])

np.take is a way of indexing along just one axis at at time, so isn't very useful in this context. The indices in idx are meant to be used together, not individually.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...