Determine sum of numpy array while excluding certain values

The Dude

I would like to determine the sum of a two dimensional numpy array. However, elements with a certain value I want to exclude from this summation. What is the most efficient way to do this?

For example, here I initialize a two dimensional numpy array of 1s and replace several of them by 2:

import numpy

data_set = numpy.ones((10, 10))

data_set[4][4] = 2
data_set[5][5] = 2
data_set[6][6] = 2

How can I sum over the elements in my two dimensional array while excluding all of the 2s? Note that with the 10 by 10 array the correct answer should be 97 as I replaced three elements with the value 2.

I know I can do this with nested for loops. For example:

elements = []
for idx_x in range(data_set.shape[0]):
  for idx_y in range(data_set.shape[1]):
    if data_set[idx_x][idx_y] != 2:
      elements.append(data_set[idx_x][idx_y])

data_set_sum = numpy.sum(elements)

However on my actual data (which is very large) this is too slow. What is the correct way of doing this?

Korem

Use numpy's capability of indexing with boolean arrays. In the below example data_set!=2 evaluates to a boolean array which is True whenever the element is not 2 (and has the correct shape). So data_set[data_set!=2] is a fast and convenient way to get an array which doesn't contain a certain value. Of course, the boolean expression can be more complex.

In [1]: import numpy as np
In [2]: data_set = np.ones((10, 10))
In [4]: data_set[4,4] = 2
In [5]: data_set[5,5] = 2
In [6]: data_set[6,6] = 2
In [7]: data_set[data_set != 2].sum()
Out[7]: 97.0
In [8]: data_set != 2
Out[8]: 
array([[ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True],
       ...
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True]], dtype=bool)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From PHP

PHP Sum multidimensional array values where certain index is the same

From Java

How to count values in a certain range in a Numpy array?

From Java

How do I get all the values from a NumPy array excluding a certain index?

From Dev

Sum slices of consecutive values in a NumPy array

From Dev

Changing specified values on numpy array in a certain dimension

From Dev

Excluding multiple values from the array

From Dev

Python numpy array sum over certain indices

From Dev

Comparing dictionaries while excluding certain keys

From Dev

Loop through array of objects and return sum of certain values

From Dev

How can you Create a copy of a worksheet by iterating through another while excluding certain rows in a array.?

From Dev

How do I sum values in array from certain indexes in loop

From Dev

How do I average a column of values while excluding rows that contain a certain value from another column? PowerBi

From Dev

Map numpy array and sum values on positions in another array

From Dev

Expanding numpy array while updating the values

From Dev

Pandas: use groupby to sum while aggregating certain values

From Dev

Python numpy 2D array sum over certain indices

From Dev

Regex - Match certain patterns while excluding others?

From Dev

JavaScript; calculate a mean, excluding certain values

From Dev

Array sum for certain keys in while loop

From Dev

I want to sum up certain values in an array of objects with Reduce

From Dev

Numpy array with the given values and the sum equal 1

From Dev

Sum of all subtractions while excluding cells with text

From Dev

Concatenate columns inside ArrayFormula, excluding certain values

From Dev

Sum values from numpy array if condition on value in another array is met

From Dev

Generate a Point object randomly excluding certain values

From Dev

Oracle regex_replace is not excluding certain values

From Dev

Filter Numpy array for negative values excluding equal values but with opposite sign

From Dev

LINQ sum of column values excluding weekends

From Dev

2d numpy array sum excluding one index location

Related Related

  1. 1

    PHP Sum multidimensional array values where certain index is the same

  2. 2

    How to count values in a certain range in a Numpy array?

  3. 3

    How do I get all the values from a NumPy array excluding a certain index?

  4. 4

    Sum slices of consecutive values in a NumPy array

  5. 5

    Changing specified values on numpy array in a certain dimension

  6. 6

    Excluding multiple values from the array

  7. 7

    Python numpy array sum over certain indices

  8. 8

    Comparing dictionaries while excluding certain keys

  9. 9

    Loop through array of objects and return sum of certain values

  10. 10

    How can you Create a copy of a worksheet by iterating through another while excluding certain rows in a array.?

  11. 11

    How do I sum values in array from certain indexes in loop

  12. 12

    How do I average a column of values while excluding rows that contain a certain value from another column? PowerBi

  13. 13

    Map numpy array and sum values on positions in another array

  14. 14

    Expanding numpy array while updating the values

  15. 15

    Pandas: use groupby to sum while aggregating certain values

  16. 16

    Python numpy 2D array sum over certain indices

  17. 17

    Regex - Match certain patterns while excluding others?

  18. 18

    JavaScript; calculate a mean, excluding certain values

  19. 19

    Array sum for certain keys in while loop

  20. 20

    I want to sum up certain values in an array of objects with Reduce

  21. 21

    Numpy array with the given values and the sum equal 1

  22. 22

    Sum of all subtractions while excluding cells with text

  23. 23

    Concatenate columns inside ArrayFormula, excluding certain values

  24. 24

    Sum values from numpy array if condition on value in another array is met

  25. 25

    Generate a Point object randomly excluding certain values

  26. 26

    Oracle regex_replace is not excluding certain values

  27. 27

    Filter Numpy array for negative values excluding equal values but with opposite sign

  28. 28

    LINQ sum of column values excluding weekends

  29. 29

    2d numpy array sum excluding one index location

HotTag

Archive