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

时间:2017-02-14 19:33:42

标签: python numpy

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

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

test看起来像这样

[[ 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]]

我想加快速度。我该怎么办?

2 个答案:

答案 0 :(得分:4)

使用numexpr

import numexpr as ne

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

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

答案 1 :(得分:0)

这是一个选项

(test > 0).all(1)

考虑test

test = np.random.randn(1000000, 3) 

时间测试

enter image description here