Finding Instances of a Specific Row in a NumPy Array Efficiently
When working with NumPy arrays, one may encounter the need to determine if the array contains a specific row, but the standard contains method for ndarray raises questions. This article presents efficient and Pythonic solutions to this issue.
One approach involves converting the NumPy array into a Python list using .tolist() and performing membership checks on the list.
a = np.array([[1,2],[10,20],[100,200]]) [1,2] in a.tolist() # Returns True [1,20] in a.tolist() # Returns False
Another method is to use a view on the array and apply the .all(1) function to compare each row with the target row element-wise.
any((a[:]==[1,2]).all(1)) # Returns True any((a[:]==[1,20]).all(1)) # Returns False
Additionally, one can generate over the NumPy list for a potential performance boost. However, this approach can be inefficient if a result is not found early.
any(([1,2] == x).all() for x in a) # Stops on first occurrence
Lastly, NumPy's logical functions provide a concise way to perform comparisons.
any(np.equal(a,[1,2]).all(1)) # Returns True
Benchmark results indicate that the numpy routines maintain a consistent search speed regardless of hit or miss scenarios. The view, logic equal, and Python in operator approaches are comparable in terms of efficiency, while the generator over NumPy is not recommended for full array searches.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3