查找 Numpy 数组是否包含特定行
使用 Numpy 数组时,有时需要验证特定行是否存在数组内。与标准 Python 列表不同,Numpy 数组提供了独特的细微差别,在执行此类检查时需要专门的方法。
Numpy 数组差异
与 Python 数组不同,Numpy 数组在以下情况下表现出不同的行为:使用 in 运算符测试行是否存在:
<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>
高效方法
要有效检查 Numpy 数组中的行是否存在,请考虑以下方法:
- .tolist() 转换: 将 Numpy 数组转换为列表,然后在列表上使用 in 运算符:
<code class="python">[1, 2] in a.tolist() # True [1, 20] in a.tolist() # False</code>
- Numpy 视图: 使用数组的视图快速检查行是否存在:
<code class="python">any((a[:]==[1,2]).all(1)) # True any((a[:]==[1,20]).all(1)) # False</code>
- Numpy 上的生成器: 在每行上生成数组并与目标行进行比较:
<code class="python">any(([1, 2] == x).all() for x in a) # Stops on first occurrence</code>
- Numpy 逻辑函数: 利用 Numpy 逻辑函数来执行比较:
<code class="python">any(np.equal(a, [1, 2]).all(1)) # True</code>
性能注意事项
这些方法的性能因数组的大小和结构而异。以下是 300,000 x 3 数组的一些计时:
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
结论
为了在 Numpy 数组中进行有效的行存在检查,建议使用 . tolist()、Numpy 视图或 Numpy 逻辑函数方法。应避免使用生成器方法,因为它的性能开销很大。
以上是如何确定 Numpy 数组是否包含特定行?的详细内容。更多信息请关注PHP中文网其他相关文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver CS6
视觉化网页开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。