是有可能,在一个快速的方式,来创建(大)2D numpy的阵列numpy的阵列创建有图案
包含一个值,每行
n
倍(随机放置)。例如,用于n = 3
1 0 1 0 1 0 0 1 1 1 1 1 1 0 0 ...
相同1.,但大小
n
每行随机的地方的基团。例如1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 ...
,我可以列举所有的行,但我想知道如果有一种方法来创建一个使用np.fromfunction
或者一些更快捷的方式排列?
是有可能,在一个快速的方式,来创建(大)2D numpy的阵列numpy的阵列创建有图案
包含一个值,每行n
倍(随机放置)。例如,用于n = 3
1 0 1 0 1
0 0 1 1 1
1 1 1 0 0
...
相同1.,但大小n
每行随机的地方的基团。例如
1 1 1 0 0
0 0 1 1 1
1 1 1 0 0
...
,我可以列举所有的行,但我想知道如果有一种方法来创建一个使用np.fromfunction
或者一些更快捷的方式排列?
第一个问题的答案有一个简单的单行解决方案,我认为它非常高效。像np.random.shuffle或np.random.permutation这样的函数必须在底层做类似的事情,但是它们需要在行上使用python循环,如果行数非常短,这可能会成为问题。
第二个问题也有一个纯粹的numpy解决方案应该是相当有效的,虽然它有点不那么优雅。
import numpy as np
rows = 20
cols = 10
n = 3
#fixed number of ones per row in random places
print (np.argsort(np.random.rand(rows, cols)) < n).view(np.uint8)
#fixed number of ones per row in random contiguous place
data = np.zeros((rows, cols), np.uint8)
I = np.arange(rows*n)/n
J = (np.random.randint(0,cols-n+1, (rows,1))+np.arange(n)).flatten()
data[I, J] = 1
print data
编辑:这里是一个稍长,但更优雅,更高性能的解决方案,你的第二个问题:
import numpy as np
rows = 20
cols = 10
n = 3
def running_view(arr, window, axis=-1):
"""
return a running view of length 'window' over 'axis'
the returned array has an extra last dimension, which spans the window
"""
shape = list(arr.shape)
shape[axis] -= (window-1)
assert(shape[axis]>0)
return np.lib.index_tricks.as_strided(
arr,
shape + [window],
arr.strides + (arr.strides[axis],))
#fixed number of ones per row in random contiguous place
data = np.zeros((rows, cols), np.uint8)
I = np.arange(rows)
J = np.random.randint(0,cols-n+1, rows)
running_view(data, n)[I,J,:] = 1
print data
对于每行解决方案的固定数目,我也会得到'[1,2,1,0,0]' - 那么诀窍是否可以使用'> 0'掩码? – HTTPeter
好点;我使用除法的唯一原因是在阵列上单次传递获得所需的int结果;但事实上该解决方案只有在n
所有你需要导入numpy的一些功能第一:
from numpy.random import rand, randint
from numpy import array, argsort
案例1:
a = rand(10,5)
b=[]
for i in range(len(a)):
n=3 #number of 1's
b.append((argsort(a[i])>=(len(a[i])-n))*1)
b=array(b)
结果:
print b
array([[ 1, 0, 0, 1, 1],
[ 1, 0, 0, 1, 1],
[ 0, 1, 0, 1, 1],
[ 1, 0, 1, 0, 1],
[ 1, 0, 0, 1, 1],
[ 1, 1, 0, 0, 1],
[ 0, 1, 1, 1, 0],
[ 0, 1, 1, 0, 1],
[ 1, 0, 1, 0, 1],
[ 0, 1, 1, 1, 0]])
案例2:
a = rand(10,5)
b=[]
for i in range(len(a)):
n=3 #max number of 1's
n=randint(0,(n+1))
b.append((argsort(a[i])>=(len(a[i])-n))*1)
b=array(b)
结果:
print b
array([[ 0, 0, 1, 0, 0],
[ 0, 1, 0, 1, 0],
[ 1, 0, 1, 0, 1],
[ 0, 1, 1, 0, 0],
[ 1, 0, 1, 0, 0],
[ 1, 0, 0, 1, 1],
[ 0, 1, 1, 0, 1],
[ 1, 0, 1, 0, 0],
[ 1, 1, 0, 1, 0],
[ 1, 0, 1, 1, 0]])
我认为可以工作。为了得到结果,我生成随机浮点列表,并用“argsort”查看那些列表中的n个最大值,然后我将它们作为整数(布尔值1 - > int)进行过滤。
仅仅为了它的乐趣,我试图为你的第一个问题找到一个解决方案,即使我对Python很陌生。这里是我到目前为止有:
np.vstack([np.hstack(np.random.permutation([np.random.randint(0,2),
np.random.randint(0,2), np.random.randint(0,2), 0, 0, 0])),
np.hstack(np.random.permutation([np.random.randint(0,2),
np.random.randint(0,2), np.random.randint(0,2), 0, 0, 0])),
np.hstack(np.random.permutation([np.random.randint(0,2),
np.random.randint(0,2), np.random.randint(0,2), 0, 0, 0])),
np.hstack(np.random.permutation([np.random.randint(0,2),
np.random.randint(0,2), np.random.randint(0,2), 0, 0, 0])),
np.hstack(np.random.permutation([np.random.randint(0,2),
np.random.randint(0,2), np.random.randint(0,2), 0, 0, 0])),
np.hstack(np.random.permutation([np.random.randint(0,2),
np.random.randint(0,2), np.random.randint(0,2), 0, 0, 0]))])
array([[1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1]])
这不是最终的答案,但也许它可以帮助你找到使用随机数和排列的替代解决方案。
你想为行特定的概率分布有1个,2个或3那些? – EOL
这个问题似乎是无关紧要的,因为它没有显示出解决问题的尝试。 – 2014-02-08 05:03:50
@EOL:在一行中,不需要概率分布。 – HTTPeter