NumPy nonzero returns coordinate arrays rather than the values at those positions

An array can contain useful positional information even when many entries equal zero. NumPy nonzero identifies the positions whose entries are nonzero. Academia Master Trader presents this English lesson for readers in Iran, using constructed arrays to explain coordinate tuples, row major order and the difference between locating entries and retrieving their values.

A vector produces a tuple containing one index array

After import numpy as np, define v = np.array([0,7,0,-4,9]). The call np.nonzero(v) returns a tuple containing the index array [1,3,4]. NumPy counts positions from zero. Seven occupies position one, negative four occupies position three and nine occupies position four.

The negative value is included because nonzero does not mean positive. Both positive and negative entries qualify when they differ from zero. The returned numbers are indices, not copies of seven, negative four and nine. Keep the location result separate from the input values when explaining the operation.

NumPy coordinate selection

A matrix produces one coordinate array for each dimension

Define m = np.array([[0,5,0],[8,0,11]]). The call np.nonzero(m) returns ([0,1,1],[1,0,2]), with each displayed list representing an array inside the returned tuple. The first array contains row indices and the second contains column indices.

Read corresponding positions together. The first pair is row zero, column one, which locates five. The second pair is row one, column zero, which locates eight. The third pair is row one, column two, which locates eleven.

Do not read the first array as one complete coordinate and the second array as another. Nonzero groups indices by dimension. It does not return one row per located element. With a three dimensional input, the tuple has three coordinate arrays instead of two.

The reported positions follow row major order

For this matrix, the scan visits the first row before the second row. Within each row, it visits columns from left to right. The resulting coordinate sequence is therefore (0,1), (1,0), (1,2).

Assign locations = np.nonzero(m). The expression m[locations] returns [5,8,11]. This second operation retrieves the values at the reported coordinates. When the goal is simply to select nonzero values, m[m != 0] is a direct alternative.

Each coordinate array has the same length, equal to the number of qualifying entries. Here that length is three. Preserving this shared order lets NumPy combine the arrays into valid coordinates during indexing without forming every possible row and column combination.

A condition can define which positions to locate

The expression np.nonzero(m > 7) returns coordinate arrays [1,1] and [0,2]. The condition first creates a Boolean array. True entries identify the positions of eight and eleven, so those are the positions reported.

An input containing only zeros still produces a tuple with one empty index array per input dimension. For a zero matrix with shape (2,3), both coordinate arrays are empty. Their emptiness means no positions qualified; it does not change the number of input dimensions.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Pair coordinate entries by their shared position and distinguish indices from selected values.