Home >Backend Development >Python Tutorial >How to Determine if a Numpy Array Contains a Specific Row?
Finding if a Numpy Array Contains a Specific Row
When working with Numpy arrays, sometimes it becomes necessary to verify if a specific row exists within the array. Unlike standard Python lists, Numpy arrays provide unique nuances that require specialized approaches when performing such checks.
Numpy Array Differences
Unlike Python arrays, Numpy arrays exhibit different behavior when testing for row existence using the in operator:
<code class="python"># Python Array a = [[1, 2], [10, 20], [100, 200]] [1, 2] in a # True [1, 20] in a # False # Numpy Array a = np.array([[1, 2], [10, 20], [100, 200]]) np.array([1, 2]) in a # True np.array([1, 20]) in a # True (Unexpected)</code>
Efficient Methods
To efficiently check for row existence in Numpy arrays, consider the following methods:
<code class="python">[1, 2] in a.tolist() # True [1, 20] in a.tolist() # False</code>
<code class="python">any((a[:]==[1,2]).all(1)) # True any((a[:]==[1,20]).all(1)) # False</code>
<code class="python">any(([1, 2] == x).all() for x in a) # Stops on first occurrence</code>
<code class="python">any(np.equal(a, [1, 2]).all(1)) # True</code>
Performance Considerations
The performance of these methods varies based on the size and structure of the array. Here are some timings for a 300,000 x 3 array:
early hit: [9000, 9001, 9002] in 300,000 elements: view: 0.01002 seconds python list: 0.00305 seconds gen over numpy: 0.06470 seconds logic equal: 0.00909 seconds late hit: [899970, 899971, 899972] in 300,000 elements: view: 0.00936 seconds python list: 0.30604 seconds gen over numpy: 6.47660 seconds logic equal: 0.00965 seconds
Conclusion
For efficient row existence checks in Numpy arrays, it is recommended to use either the .tolist(), Numpy view, or Numpy logic function methods. The generator method should be avoided due to its significant performance overhead.
The above is the detailed content of How to Determine if a Numpy Array Contains a Specific Row?. For more information, please follow other related articles on the PHP Chinese website!