2014-02-07 72 views
0

是有可能,在一個快速的方式,來創建(大)2D numpy的陣列numpy的陣列創建有圖案

  1. 包含一個值,每行n倍(隨機放置)。例如,用於n = 3

    1 0 1 0 1 
    0 0 1 1 1 
    1 1 1 0 0 
    ... 
    
  2. 相同1.,但大小n每行隨機的地方的基團。例如

    1 1 1 0 0 
    0 0 1 1 1 
    1 1 1 0 0 
    ... 
    
當然

,我可以列舉所有的行,但我想知道如果有一種方法來創建一個使用np.fromfunction或者一些更快捷的方式排列?

+1

你想爲行特定的概率分佈有1個,2個或3那些? – EOL

+1

這個問題似乎是無關緊要的,因爲它沒有顯示出解決問題的嘗試。 – 2014-02-08 05:03:50

+0

@EOL:在一行中,不需要概率分佈。 – HTTPeter

回答

1

第一個問題的答案有一個簡單的單行解決方案,我認爲它非常高效。像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 
+0

對於每行解決方案的固定數目,我也會得到'[1,2,1,0,0]' - 那麼訣竅是否可以使用'> 0'掩碼? – HTTPeter

+0

好點;我使用除法的唯一原因是在陣列上單次傳遞獲得所需的int結果;但事實上該解決方案只有在n

0

所有你需要導入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)進行過濾。

0

僅僅爲了它的樂趣,我試圖爲你的第一個問題找到一個解決方案,即使我對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]]) 

這不是最終的答案,但也許它可以幫助你找到使用隨機數和排列的替代解決方案。