python:进行大规模检查的有效方法

标签 python numpy

我有一个包含很多行的数组 test,每行有 3 个数字。我需要为所有 3 个数字均为正数的每一行获取 True,否则为 False。目前我在用

check = np.all((test[:] > 0), axis=1)

测试 看起来像这样

[[ 0.76717743 -0.14548865  0.10444938]
 [-0.349094    0.86043484 -0.03470421]
 [ 0.61281474 -0.5589774  -0.46888367]
 [ 0.106147   -0.15964074 -0.72297835]
 [-0.47496423  0.53561226 -0.56997515]
 [ 0.11404722 -0.19772103 -0.70308939]
 [ 0.05176676  0.68243443  0.10374478]
 [ 0.41054414 -0.37773413 -0.65230608]
 [-0.51045939  0.81814742 -0.34113701]
 [-0.30434684  0.229953   -0.6717718 ]
 [ 0.12726272 -0.2157578  -0.6968093 ]
 [-0.56914774  0.80530545 -0.27157462]
 [-0.10736854  0.76317726  0.08648359]
 [-0.30585015  0.85400552 -0.02213381]
 [ 0.81378956 -0.20719654  0.04194195]
 [-0.57842424  0.70255033 -0.39798076]
 [ 0.56074816 -0.54319019 -0.49967448]
 [ 0.72422859 -0.58427248 -0.37145212]
 [ 0.86456736 -0.45311164 -0.08229539]
 [ 0.86428591 -0.43964496 -0.0693436 ]
 [ 0.47937801 -0.51390972 -0.53733707]
 [ 0.85551894 -0.39396805 -0.01711862]
 [ 0.4737934  -0.54178454 -0.50940207]
 [ 0.15694062 -0.34735834 -0.61456285]]

我想加快速度。我能做什么?

最佳答案

使用numexpr:

import numexpr as ne

t0, t1, t2 = test[:,0], test[:,1], test[:,2]

check = ne.evaluate('(t0 > 0) & (t1 > 0) & (t2 > 0)')

关于python:进行大规模检查的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42234672/

相关文章:

python - 尝试在给定的条件下查找名称

python - 在 np.select 中使用字符串条件时出现问题

python - Pandas 使用 numpy 百分位数重采样?

numpy - 如何在 tensorflow 中打印完整(未截断)的张量?

python - 如何计算列表中数组的特定元素的出现次数?

python - 在 Django 中运行 Managepy Shell 时的设置问题

Python相同的代码但不同的继承

python - 在 Python 中通过 `subprocess.run` 调用 g++ 导致可执行文件中的 "exec format error"

python - MySQL LOAD DATA LOCAL INFILE Python

python - 如何使用 OpenCV 在 Python 中查找图像的平均颜色?