2017-04-11 59 views

回答

4

看來你np.broadcast_to後:

# build an array where l_vals[0,0,0,i,0] = i 
l_vals = np.arange(10).reshape(1, 1, 1, -1, 1) 

# duplicate that array, without copies, in the other axes 
l_grid = np.broadcast_to(l_vals, (10, 10, 10, 10, 10)) 

值得一提的是,broadcast_to返回一個只讀數組,因爲元素實際上共享內存位置。如果你想創建後寫入到這一點,那麼你可以調用np.copy,或使用tile代替:

l_grid = np.tile(l_vals, (10, 10, 10, 1, 10)) 

你也只是具有扁平您的循環:

MyArray = np.zeros([10,10,10,10,10]) 
for l in range(10): 
    MyArray[:,:,:,l,:] = l 
+1

值得一提的是'np.broadcast_to'創建了一個只讀數組。 – Divakar

2

嘗試np.fromfunction,你的情況這將是類似的東西: MyArray = np.fromfunction(lambda i, j, k, l, m : l, (10, 10, 10, 10, 10))

+0

小的浪費,但做這項工作 – Eric

2

你可以這樣做一個循環:

import numpy as np 
MyArray = np.zeros([10,10,10,10,10]) 

for l in range(10): 
    MyArray[:, :, :, l, :] = l 

很明顯,你也可以做到這一點作爲一個列表理解。

+0

對我來說,這看起來似乎不像列表理解的形式... – Eric

+0

是的,對不起,似乎我的手指在鍵盤上速度比我的大腦可以跟上:) –

3

下面是與initialization的方法 -

n = 10 
a = np.empty((n,n,n,n,n),dtype=int) 
a[...] = np.arange(n)[:,None] 

這裏還有一個NumPy strides基礎的方法 -

r = np.arange(n) 
s = r.strides[0] 
shp = (n,n,n,n,n) 
out = np.lib.index_tricks.as_strided(r, shape=shp, strides=(0,0,0,s,0)) 

運行測試

途徑 -

# @Eric's soln1 
def broadcast_to_based(n): # Creates a read-only array 
    l_vals = np.arange(n).reshape(1, 1, 1, -1, 1) 
    return np.broadcast_to(l_vals, (n, n, n, n, n)) 

# @Eric's soln2  
def tile_based(n): 
    l_vals = np.arange(n).reshape(1, 1, 1, -1, 1) 
    return np.tile(l_vals, (n, n, n, 1, n)) 

# @kmichael08's soln   
def fromfunc_based(n): 
    return np.fromfunction(lambda i, j, k, l, m : l, (n, n, n, n, n)) 

# @Tw UxTLi51Nus's soln 
def loop_based(n): 
    MyArray = np.zeros([n,n,n,n,n],dtype=int) 
    for l in range(n): 
     MyArray[:, :, :, l, :] = l 
    return MyArray 

# Proposed-1 in this post  
def initialization_based(n): 
    a = np.empty((n,n,n,n,n),dtype=int) 
    a[...] = np.arange(n)[:,None] 
    return a 

# Proposed-2 in this post  
def strided_based(n): 
    r = np.arange(n) 
    s = r.strides[0] 
    shp = (n,n,n,n,n) 
    return np.lib.index_tricks.as_strided(r, shape=shp, strides=(0,0,0,s,0)) 

計時 -

In [153]: n = 10 
    ...: %timeit broadcast_to_based(n) 
    ...: %timeit tile_based(n) 
    ...: %timeit fromfunc_based(n) 
    ...: %timeit loop_based(n) 
    ...: %timeit initialization_based(n) 
    ...: %timeit strided_based(n) 
    ...: 
100000 loops, best of 3: 4.1 µs per loop 
1000 loops, best of 3: 236 µs per loop 
1000 loops, best of 3: 645 µs per loop 
10000 loops, best of 3: 180 µs per loop 
10000 loops, best of 3: 89.1 µs per loop 
100000 loops, best of 3: 5.44 µs per loop 

In [154]: n = 20 
    ...: %timeit broadcast_to_based(n) 
    ...: %timeit tile_based(n) 
    ...: %timeit fromfunc_based(n) 
    ...: %timeit loop_based(n) 
    ...: %timeit initialization_based(n) 
    ...: %timeit strided_based(n) 
    ...: 
100000 loops, best of 3: 4.05 µs per loop 
100 loops, best of 3: 8.16 ms per loop 
10 loops, best of 3: 24.1 ms per loop 
100 loops, best of 3: 6.07 ms per loop 
100 loops, best of 3: 2.31 ms per loop 
100000 loops, best of 3: 5.48 µs per loop 
+0

第一個':'可能更好拼寫'...' – Eric

+0

@Eric Yup,這就是那裏的想法。謝謝,編輯。 – Divakar