布尔输入场景下numpy.where与numpy.argwhere的区别及功能一致性疑问
Understanding the Difference Between
numpy.where and numpy.argwhere Great question! Let's unpack this clearly—while both functions can return coordinates of non-zero elements in a boolean array, they're not identical in functionality or output format, and each has its own distinct use cases.
First, let's break down the output difference using your example:
- When you run
res = np.where(((aa == 7) | (aa == 8))), you get a tuple of arrays where each array corresponds to indices for a single dimension. In your case,(array([0, 0]), array([2, 3]))means the matching elements sit at row indices[0,0]and column indices[2,3]. - When you run
res2 = np.argwhere(((aa == 7) | (aa == 8))), you get a 2D array where each row represents a full coordinate pair of a matching element:array([[0, 2], [0, 3]]). In fact, this is exactly equivalent to transposing the tuple fromnp.where:np.transpose(np.where(condition))will give you the same result asnp.argwhere(condition).
The key distinction comes in the core functionality of each function:
numpy.wherehas two distinct modes:- Single argument (boolean array): This is the mode you used, where it returns indices of non-zero elements.
- Three arguments (
condition, x, y): This is a powerful vectorized tool to choose elements fromxorybased on thecondition. For example,np.where(aa > 7, aa, 0)would replace all elements inaathat are ≤7 with 0, keeping the rest as-is. This is a completely separate use case thatnp.argwheredoesn't support.
numpy.argwhereis specialized solely for returning coordinate pairs (or tuples for higher dimensions) of non-zero elements in a boolean array. Its output format is often more convenient when you need to iterate over individual coordinates (e.g., looping through each matching element's position).
When to use which?
- Use
np.whereif:- You need to split indices by dimension (e.g., collect all row indices of matches separately from column indices)
- You want to perform element-wise selection between two arrays based on a condition
- Use
np.argwhereif:- You want an easy-to-iterate list of coordinate pairs for matching elements
- You prefer a 2D array format where each row represents a complete position
So to answer your original question: When only given a boolean array, both functions locate the same elements, but their output formats differ. And since np.where has an additional critical use case (the three-argument selection), they are not functionally identical overall.
内容的提问来源于stack exchange,提问作者f. c.




