如何有效地操作大型numpy数组 [英] How to efficiently operate a large numpy array

查看:116
本文介绍了如何有效地操作大型numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段基于大型numpy数组的代码,然后操作另一个数组.由于这是一个非常大的阵列,请您让我知道是否有一种有效的方法可以实现我的目标? (我认为应该通过直接在数组上而不是通过for循环来实现有效的方法.)

I have a segment of codes which is based on a large numpy array and then to operate another array. Because this is a very large array, could you please let me know whether there is an efficient way to achieve my goal? (I think the efficient way should be achieved by directly operating on the array but not through the for-loop).

谢谢,请在下面找到我的代码:

Thanks in advance, please below find my codes:

N = 1000000000
rand = np.random.rand(N)
beta = np.zeros(N)
for i in range(0, N):
    if rand[i] < 0.5:
        beta[i] = 2.0*rand[i]
    else:
        beta[i] = 1.0/(2.0*(1.0-rand[i]))

推荐答案

通过使用Python执行处理,您在这里基本上失去了numpy的效率. numpy的想法是处理 bulk 中的项目,因为它在C ++中具有执行实际处理工作的高效算法.您可以更多地将numpy的Python结尾视为接口".

You are here basically losing the efficiency of numpy, by performing the processing in Python. The idea of numpy is process the items in bulk, since it has efficiency algorithms in C++ behind the curtains that do the actual processing. You can see the Python end of numpy more as an "interface".

现在要回答您的问题,我们基本上可以首先将0到2之间的随机数数组乘以2来构建它:

Now to answer your question, we can basically first construct an array of random numbers between 0 and 2, by multiplying it with 2 already:

rand = 2.0 * np.random.rand(N)

接下来,我们可以使用 np.where(..) [numpy-doc] 就像一个条件选择器:我们在这里传递了三个数组":第一个是布尔数组,用于对条件"的真实性进行编码,第二个是在相关条件为true时要填充的值的数组,第三个值是在条件为false时要插入的值的数组,因此我们可以这样写:

next we can use np.where(..) [numpy-doc] that acts like a conditional selector: we here pass it three "arrays": the first one is an array of booleans that encodes the truthiness of the "condition", the second is the array of values to fill in in case the related condition is true, and the third value is the array of values to plug in in case the condition is false, so we can then write it like:

N = 1000000000
rand = 2 * np.random.rand(N)
beta = np.where(rand < 1.0, rand, 1.0 / (2.0 - rand))

这篇关于如何有效地操作大型numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆