Perform operation on elements of numpy array using indexes list

user

I have numpy array and two python lists of indexes with positions to increase arrays elements by one. Do numpy has some methods to vectorize this operation without use of for loops?

My current slow implementation:

a = np.zeros([4,5])
xs = [1,1,1,3]
ys = [2,2,3,0]

for x,y in zip(xs,ys): # how to do it in numpy way (efficiently)?
    a[x,y] += 1

print(a)

Output:

[[0. 0. 0. 0. 0.]
 [0. 0. 2. 1. 0.]
 [0. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0.]]
Tomas Farias

np.add.at will do just that, just pass both indexes as a single 2D array/list:

a = np.zeros([4,5])
xs = [1, 1, 1, 3]
ys = [2, 2, 3, 0]

np.add.at(a, [xs, ys], 1) # in-place
print(a)

array([[0., 0., 0., 0., 0.],
       [0., 0., 2., 1., 0.],
       [0., 0., 0., 0., 0.],
       [1., 0., 0., 0., 0.]])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to perform some mathematical operation on some specific elements of a list using java 8?

From Dev

How to perform operation on elements of list which depend on previous elements?

From Dev

Sum elements of list using condition on indexes

From Dev

perform operation on select indices of a numpy array based on values in another array

From Dev

Replace elements in numpy array using list of old and new values

From Dev

Perform click operation in succession on dynamic list of selected elements in nightmare js

From Dev

Indexes of elements in NumPy array that satisfy conditions on the value and the index

From Dev

numpy, Select elements in rows by 1d indexes array

From Dev

numpy, Select elements in rows by 1d indexes array

From Dev

How to get the indexes of column and row in a 2-dimensional matrix(list) that have the most given elements in using numpy in python

From Dev

Find indexes of elements in list

From Dev

Find indexes of elements in list

From Java

Replace multiple values of 3d Numpy array by list of indexes

From Java

NumPy selecting specific column index per row by using a list of indexes

From Dev

Increase numpy array elements using array as index

From Dev

Accessing NumPy array elements not in a given index list

From Dev

Indexing a numpy array with a numpy array of indexes

From Dev

Finding the dimension indexes of elements of an array

From Dev

Using lapply to perform multiple operations on many elements of a list in R

From Dev

Using lapply to perform multiple operations on many elements of a list in R

From Dev

Order of indexes in a Numpy multidimensional array

From Dev

Merging numpy array elements using join() in python

From Dev

Is there a difference in the way we access elements of a list comprehension and the elements of a numpy array

From Dev

How do i access all elements of numpy array which indexes satisfy some condition?

From Dev

Replace 2D numpy array elements based on 2D indexes

From Dev

How to perform iterative 2D operation on 4D numpy array

From Dev

How to perform contain operation between a numpy array and a vector row-wise?

From Dev

How to perform iterative 2D operation on 4D numpy array

From Dev

Using a character operator to perform an operation

Related Related

  1. 1

    How to perform some mathematical operation on some specific elements of a list using java 8?

  2. 2

    How to perform operation on elements of list which depend on previous elements?

  3. 3

    Sum elements of list using condition on indexes

  4. 4

    perform operation on select indices of a numpy array based on values in another array

  5. 5

    Replace elements in numpy array using list of old and new values

  6. 6

    Perform click operation in succession on dynamic list of selected elements in nightmare js

  7. 7

    Indexes of elements in NumPy array that satisfy conditions on the value and the index

  8. 8

    numpy, Select elements in rows by 1d indexes array

  9. 9

    numpy, Select elements in rows by 1d indexes array

  10. 10

    How to get the indexes of column and row in a 2-dimensional matrix(list) that have the most given elements in using numpy in python

  11. 11

    Find indexes of elements in list

  12. 12

    Find indexes of elements in list

  13. 13

    Replace multiple values of 3d Numpy array by list of indexes

  14. 14

    NumPy selecting specific column index per row by using a list of indexes

  15. 15

    Increase numpy array elements using array as index

  16. 16

    Accessing NumPy array elements not in a given index list

  17. 17

    Indexing a numpy array with a numpy array of indexes

  18. 18

    Finding the dimension indexes of elements of an array

  19. 19

    Using lapply to perform multiple operations on many elements of a list in R

  20. 20

    Using lapply to perform multiple operations on many elements of a list in R

  21. 21

    Order of indexes in a Numpy multidimensional array

  22. 22

    Merging numpy array elements using join() in python

  23. 23

    Is there a difference in the way we access elements of a list comprehension and the elements of a numpy array

  24. 24

    How do i access all elements of numpy array which indexes satisfy some condition?

  25. 25

    Replace 2D numpy array elements based on 2D indexes

  26. 26

    How to perform iterative 2D operation on 4D numpy array

  27. 27

    How to perform contain operation between a numpy array and a vector row-wise?

  28. 28

    How to perform iterative 2D operation on 4D numpy array

  29. 29

    Using a character operator to perform an operation

HotTag

Archive