Pythonic方式稀疏地随机填充数组?

时间:2013-10-30 15:57:40

标签: python arrays random

问题: 用10 1 1,20 2,30 3随机填充一个10 x 10的零序列。

我实际上不需要使用数组,而只需要坐标值来确定值。从阵列的角度来看,这更容易想到。

我已经为此写了几个解决方案,但它们似乎都是非直线和非pythonic。我希望有人可以给我一些见解。我的方法一直使用0-99的线性数组,随机选择(np.random.choice)10个值,从数组中删除它们,然后选择20个随机值。之后,我将线性位置转换为(y,x)坐标。

import numpy as np

dim = 10
grid = np.arange(dim**2)

n1 = 10
n2 = 20
n3 = 30

def populate(grid, n, dim):
    pos = np.random.choice(grid, size=n, replace=False)
    yx = np.zeros((n,2))
    for i in xrange(n):
        delPos = np.where(grid==pos[i])
        grid = np.delete(grid, delPos)
        yx[i,:] = [np.floor(pos[i]/dim), pos[i]%dim]
    return(yx, grid)

pos1, grid = populate(grid, n1, dim)
pos2, grid = populate(grid, n2, dim)
pos3, grid = populate(grid, n3, dim)

附加 假设当我填充1时,我希望它们都在“数组”的一半上。我可以使用我的方法(从网格[dim ** 2/2:]中抽样),但我还没有弄清楚如何对其他建议做同样的事情。

3 个答案:

答案 0 :(得分:3)

要生成数组,您可以使用numpy.random.choice

np.random.choice([0, 1, 2, 3], size=(10,10), p=[.4, .1, .2, .3])

然后你可以转换为坐标。请注意,numpy.random.choice会使用概率p生成随机样本,因此您无法保证在p中获得完全比例。

<强>附加

如果您希望在阵列的特定一侧拥有所有1,则可以生成两个随机数组,然后生成hstack个数组。诀窍是略微修改每一方的每个数字的概率。

In [1]: import numpy as np
In [2]: rem = .1/3 # amount to de- / increase the probability for non-1s
In [3]: A = np.random.choice([0, 1, 2, 3], size=(5, 10),
                              p=[.4-rem, .2, .2-rem, .3-rem])
In [4]: B = np.random.choice([0, 2, 3], size=(5, 10), p=[.4+rem, .2+rem, .3+rem])
In [5]: M = np.hstack( (A, B) )
In [6]: M
Out[1]: 
array([[1, 1, 3, 0, 3, 0, 0, 1, 1, 0, 2, 2, 0, 2, 0, 2, 3, 3, 2, 0],
       [0, 3, 3, 3, 3, 0, 1, 3, 1, 3, 0, 2, 3, 0, 0, 0, 3, 3, 2, 3],
       [1, 0, 0, 0, 1, 0, 3, 1, 2, 2, 0, 3, 0, 3, 3, 0, 0, 3, 0, 0],
       [3, 2, 3, 0, 3, 0, 1, 2, 3, 2, 0, 0, 0, 0, 3, 2, 0, 0, 0, 3],
       [3, 3, 0, 3, 3, 3, 1, 3, 0, 3, 0, 2, 0, 2, 0, 0, 0, 3, 3, 3]])

在这里,因为我将所有放在左侧的1,所以我将1的概率加倍并且平均降低每个数字的概率。创建另一面时也适用相同的逻辑。

答案 1 :(得分:2)

您可以创建一个列出所有坐标的列表shuffle,并列出其中的前60个坐标(10 + 20 + 30):

>>> import random
>>> coordinates = [(i, j) for i in xrange(10) for j in xrange(10)]
>>> random.shuffle(coordinates)
>>> coordinates[:60]
[(9, 5), (6, 9), (1, 5), ..., (0, 2), (5, 9), (2, 6)]

然后,您可以使用前10个插入10个值,接下来的20个用于20个值,剩余的用于30个值。

答案 2 :(得分:0)

不确定这是不是“pythonic”,但这是我想出的一些使用Simeon答案的部分。

import random

dim = 10
n1 = 10
n2 = 20
n3 = 30

coords = [[i,j] for i in xrange(dim) for j in xrange(dim)]

def setCoords(coords, n):
    pos = []
    for i in xrange(n):
        random.shuffle(coords)
        pos.append(coords.pop())
    return(coords, pos)

coordsTmp, pos1 = setCoords(coords[dim**2/2:], n1)
coords = coords[:dim**2/2] + coordsTmp
coords, pos2 = setCoords(coords, n2)
coords, pos3 = setCoords(coords, n3)