簡體   English   中英

為什么這段代碼不返回一個元組數組?

[英]Why doesn't this code return an array of tuples?

在運行以下代碼時:

f=lambda m,n: (m,n)
np.fromfunction(f,(6,6),dtype=int)

我得到以下輸出:

(array([[0, 0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3, 3],
        [4, 4, 4, 4, 4, 4],
        [5, 5, 5, 5, 5, 5]]), array([[0, 1, 2, 3, 4, 5],
        [0, 1, 2, 3, 4, 5],
        [0, 1, 2, 3, 4, 5],
        [0, 1, 2, 3, 4, 5],
        [0, 1, 2, 3, 4, 5],
        [0, 1, 2, 3, 4, 5]]))

為什么我沒有得到一個數組/元組列表?

我的期望來自https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfunction.html

numpy.fromfunction(function, shape, **kwargs)
Construct an array by executing a function over each coordinate.

The resulting array therefore has a value fn(x, y, z) at coordinate (x, y, z).

我的函數 f(n,m) 返回 (n,m)。

f2=lambda m,n: m+10*n
np.fromfunction(f2,(6,6),dtype=int)

給出以下輸出

array([[ 0, 10, 20, 30, 40, 50],
       [ 1, 11, 21, 31, 41, 51],
       [ 2, 12, 22, 32, 42, 52],
       [ 3, 13, 23, 33, 43, 53],
       [ 4, 14, 24, 34, 44, 54],
       [ 5, 15, 25, 35, 45, 55]])

fromfunction文檔可能會令人困惑。 但是,如果您查看代碼,您會發現它只是生成indices並將它們整個傳遞給您的函數:

In [345]: f = lambda m,n: (m,n)                                                                
In [346]: np.fromfunction(f, (3,3), dtype=int)                                                 
Out[346]: 
(array([[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2]]), array([[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]]))
In [348]: m,n = np.indices((3,3))                                                              
In [349]: m,n                                                                                  
Out[349]: 
(array([[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2]]), array([[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]]))

有一個函數可以為您的函數提供元組:

In [351]: np.frompyfunc(f,2,1)(m,n)                                                            
Out[351]: 
array([[(0, 0), (0, 1), (0, 2)],
       [(1, 0), (1, 1), (1, 2)],
       [(2, 0), (2, 1), (2, 2)]], dtype=object)

np.vectorize做了類似的事情,但為此目的frompyfunc更簡單、更快。

這個列表理解同樣快:

In [352]: [(i,j) for i,j in zip(m.ravel(),n.ravel())]                                          
Out[352]: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

你的第二個功能:

In [414]: m,n = np.indices((3,3))                                                              
In [415]: m*10*n                                                                               
Out[415]: 
array([[ 0,  0,  0],
       [ 0, 10, 20],
       [ 0, 20, 40]])

實際代碼:

def fromfunction(function, shape, **kwargs):
    dtype = kwargs.pop('dtype', float)
    args = indices(shape, dtype=dtype)
    return function(*args, **kwargs)

====

f2=lambda m,n: m+10*n

適用於np.fromfunctionnp.frompyfunc

In [417]: f2=lambda m,n: m+10*n                                                                
In [418]: np.fromfunction(f2, (3,3), dtype=int)                                                
Out[418]: 
array([[ 0, 10, 20],
       [ 1, 11, 21],
       [ 2, 12, 22]])
In [420]: np.frompyfunc(f2,2,1)(m,n)                                                           
Out[420]: 
array([[0, 10, 20],
       [1, 11, 21],
       [2, 12, 22]], dtype=object)

===

這是將返回等效於嵌套元組的 3d 數組的函數:

In [426]: f3 = lambda m,n: np.stack((m,n), axis=2)                                             
In [427]: np.fromfunction(f3,(3,3),dtype=int)                                                  
Out[427]: 
array([[[0, 0],
        [0, 1],
        [0, 2]],

       [[1, 0],
        [1, 1],
        [1, 2]],

       [[2, 0],
        [2, 1],
        [2, 2]]])
In [428]: f3(m,n)                                                                              
Out[428]: 
array([[[0, 0],
        [0, 1],
        [0, 2]],

       [[1, 0],
        [1, 1],
        [1, 2]],

       [[2, 0],
        [2, 1],
        [2, 2]]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM