user785099 user785099 - 3 months ago 13
Python Question

calculate different statistics for multi-dimensional numpy array

There exists an

nd.array
referred to as
label1
, when printing it out, it has

[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 88 0]
[0 0 0 ..., 0 0 0]
...,
[0 0 1 ..., 0 0 0]
[0 0 0 ..., 0 2 0]
[0 0 0 ..., 0 0 0]]


Its shape is
(729,816)
. Are there any ways to know how many unique values are in this array? When running
print(np.where(label1==label1.max()))
, the result looks like this
(array([ 0, 0, 0, ..., 234, 234, 234]), array([450, 451, 452, ..., 433, 434, 435]))
, does that mean it has two arrays (or two lines) have those maximum values?

Answer
  1. To get the sorted unique values in your array, do numpy.unique(label1).
  2. When called on a two-dimensional array, numpy.where returns two arrays which are the row and column coordinates of all matching entries. So, if numpy.where returns (array([0]), array([1])) it means that one match was found at row 0, column 1.

EDIT: If you want to extract and print the row and column coordinates from numpy.where, you can do it like this:

rows, cols = np.where(label1==label1.max())
for row, col in zip(rows, cols):
  print row, col
Comments