在Python阵列内的随机数 [英] Random numbers within Array in Python

查看:149
本文介绍了在Python阵列内的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Python。在阅读,请提及关于如何改善我的Python code任何其他建议。

问:如何生成Python中的数8xn维数组包含随机数? 的约束是,此数组中的每一列必须包含8无需更换吸引了来自设置整数[1,8] 。更具体地说,当N = 10,我想是这样的。

  [6. 2. 3. 4. 5. 7. 5. 7. 8. 4]
 [1. 4. 5. 5. 4. 4. 8 5 7。5.]
 [7. 3. 8. 8. 3. 8 7。3 6. 7.]
 [3. 6. 7. 1. 5. 6. 2. 1. 5. 1.]
 [8 1。4 3 8 2. 3. 4. 3. 3.]
 [5 8 1 7 1 3 6 8。1。6]
 [4. 5. 2. 6. 2. 1. 1. 6 4 2]
 [2 7 6。2. 6. 7. 4. 2。2 8]

要做到这一点我用下面的办法:

 进口numpy.random
进口numpy的
高清rand_M(N):
    M = numpy.zeros(形状=(8,N))
    对于在范围I(0,N):
        M [:,我] = numpy.random.choice(8,大小= 8,更换= FALSE)+ 1
    回报中号

在实践中N值将〜1E7。上述算法是O(n)在时间和大约需要0.38秒当N = 1E3。因此,当N = 1E7是1小时〜时间(即3800秒)。必须有一个更有效的方式。

定时功能

 从timeit进口计时器
T =定时器(拉姆达:rand_M(1000))
打印(t.timeit(5))
0.3863314103162543


解决方案

创建特定形状的随机排列,然后沿排序,你想保留的限制,从而给我们一个量化的,非常有效的解决方案轴。这将在此基础上 智能答案 以<一个href=\"http://stackoverflow.com/questions/29156823/matlab-randomly-permuting-columns-differently\"><$c$c>MATLAB随机置换列不同 。这里的执行 -

样运行 -

 在[122]:N = 10在[123]:np.argsort(np.random.rand(8,N),轴= 0)+1
出[123]:
阵列([[7,3,5,1,1,5,2,4,1,4]
       [8,4,3,2,2,8,5,5,6,2],
       [1,2,4,6,5,4,4,3,4,7],
       [5,6,2,5,8,2,7,8,5,8],
       [2,8,6,3,4,7,1,1,2,6]
       [6,7,7,8,6,6,3,2,7,3]
       [4,1,1,4,3,3,8,6,8,1],
       [3,5,8,7,7,1,6,7,3,5],DTYPE = int64类型)

运行测试 -

 在[124]:高清sortbased_rand8(N):
     ...:返回np.argsort(np.random.rand(8,N),轴= 0)+1
     ...:
     ...:DEF rand_M(N):
     ...:M = np.zeros(形状=(8,N))
     ......因为我在范围(0,N):
     ...:M [:,我] = np.random.choice(8,大小= 8,更换= FALSE)+ 1
     ...:返回中号
     ...:在[125]:N = 5000在[126]:%timeit sortbased_rand8(N)
100圈,最好的3:每循环1.95毫秒在[127]:%timeit rand_M(N)
1循环,最好的3:每循环233毫秒

因此​​,等待一个 120X 加速比!

I'm new to Python. While reading, please mention any other suggestions regarding ways to improve my Python code.

Question: How do I generate a 8xN dimensional array in Python containing random numbers? The constraint is that each column of this array must contain 8 draws without replacement from the integer set [1,8]. More specifically, when N = 10, I want something like this.

[[ 6.  2.  3.  4.  7.  5.  5.  7.  8.  4.]
 [ 1.  4.  5.  5.  4.  4.  8.  5.  7.  5.]
 [ 7.  3.  8.  8.  3.  8.  7.  3.  6.  7.]
 [ 3.  6.  7.  1.  5.  6.  2.  1.  5.  1.]
 [ 8.  1.  4.  3.  8.  2.  3.  4.  3.  3.]
 [ 5.  8.  1.  7.  1.  3.  6.  8.  1.  6.]
 [ 4.  5.  2.  6.  2.  1.  1.  6.  4.  2.]
 [ 2.  7.  6.  2.  6.  7.  4.  2.  2.  8.]]

To do this I use the following approach:

import numpy.random
import numpy
def rand_M(N):
    M = numpy.zeros(shape = (8, N))
    for i in range (0, N):
        M[:, i] = numpy.random.choice(8, size = 8, replace = False) + 1 
    return M

In practice N will be ~1e7. The algorithm above is O(n) in time and it takes roughly .38 secs when N=1e3. The time therefore when N = 1e7 is ~1hr (i.e. 3800 secs). There has to be a much more efficient way.

Timing the function

from timeit import Timer 
t = Timer(lambda: rand_M(1000))
print(t.timeit(5))
0.3863314103162543

解决方案

Create a random array of specified shape and then sort along the axis where you want to keep the limits, thus giving us a vectorized and very efficient solution. This would be based on this smart answer to MATLAB randomly permuting columns differently. Here's the implementation -

Sample run -

In [122]: N = 10

In [123]: np.argsort(np.random.rand(8,N),axis=0)+1
Out[123]: 
array([[7, 3, 5, 1, 1, 5, 2, 4, 1, 4],
       [8, 4, 3, 2, 2, 8, 5, 5, 6, 2],
       [1, 2, 4, 6, 5, 4, 4, 3, 4, 7],
       [5, 6, 2, 5, 8, 2, 7, 8, 5, 8],
       [2, 8, 6, 3, 4, 7, 1, 1, 2, 6],
       [6, 7, 7, 8, 6, 6, 3, 2, 7, 3],
       [4, 1, 1, 4, 3, 3, 8, 6, 8, 1],
       [3, 5, 8, 7, 7, 1, 6, 7, 3, 5]], dtype=int64)

Runtime tests -

In [124]: def sortbased_rand8(N):
     ...:     return np.argsort(np.random.rand(8,N),axis=0)+1
     ...: 
     ...: def rand_M(N):
     ...:     M = np.zeros(shape = (8, N))
     ...:     for i in range (0, N):
     ...:         M[:, i] = np.random.choice(8, size = 8, replace = False) + 1 
     ...:     return M
     ...: 

In [125]: N = 5000

In [126]: %timeit sortbased_rand8(N)
100 loops, best of 3: 1.95 ms per loop

In [127]: %timeit rand_M(N)
1 loops, best of 3: 233 ms per loop

Thus, awaits a 120x speedup!

这篇关于在Python阵列内的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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