具有多个条件的 numpy where 函数
在 numpy 中,where 函数允许根据条件过滤数组。但是,当尝试使用 & 和 | 等逻辑运算符应用多个条件时,可能会出现意外结果。
请考虑以下代码:
import numpy as np dists = np.arange(0, 100, 0.5) r = 50 dr = 10 # Attempt to select distances within a range result = dists[(np.where(dists >= r)) and (np.where(dists <p>此代码尝试选择 r 和 r 之间的距离r博士。但是,它只选择满足第二个条件的距离,dists </p><p><strong>失败原因:</strong></p><p>numpy where 函数返回以下元素的索引:满足条件,而不是布尔数组。使用逻辑运算符组合多个 where 语句时,输出是满足各自条件的索引列表。对这些列表执行 and 运算会产生第二组索引,从而有效地忽略第一个条件。</p><p><strong>正确方法:</strong></p>
- 元素-明智比较:
要应用多个条件,请直接使用逐元素比较:
dists[(dists >= r) & (dists
- 布尔数组:
或者,为每个条件创建布尔数组并对它们执行逻辑运算:
condition1 = dists >= r condition2 = dists
- 花式索引:
花哨的索引还允许条件过滤:
result = dists[(condition1) & (condition2)]
在某些情况下,将条件简化为单个标准可能会更有利,如下例所示:
result = dists[abs(dists - r - dr/2.) <p>通过了解了 where 函数的行为,程序员可以在 numpy 中根据多个条件有效地过滤数组。</p>
以上是如何使用多个条件过滤 Numpy 数组:为什么 `np.where()` 失败以及如何获得正确的结果?的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

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

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

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

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具