Numpy裡的fromfunction 的問題

時間 2021-06-08 23:37:45

1樓:腦殼彩虹風暴幻者

這個方法確實把很多邏輯都濃縮了 :)

舉乙個官方給的例子:

np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)

1.function: lamba i,j: i == j

表示傳入兩個shape相同的array1, array2, 判斷array1[id1, id_2]和array2[id1, id_2]上的元素是否相等,輸入判斷結果為乙個(3, 3)的array

2.(3, 3)

表示定義兩個array, 每個array的shape都為(3, 3)

第乙個array是沿著第一軸方向,按照np.arange(3)產生元素且元素dtype為int

第二個array是沿著第二軸方向,按照np.arange(3)產生元素且元素dtype為int

官方給的解答太抽象了,希望以上解答有助於理解~

2樓:nicekwell

np.fromfunction(func, )

對於這個函式,很多人都認為fromfunction是把後面傳入的shape的各個座標迭代進func,然而並不是這樣的。

比如9x9乘法表:

deffunc(i

,j):return(i

+1)*

(j+1

)np.fromfunction

(func,(

9,9))

看起來像是依次把 i, j 從0開始依次迭代,用函式計算出每個座標的值。如果真的是這樣的話我們應當可以在函式中加入判斷的:

deffunc(i

,j):if

i>=j:

return(i

+1)*

(j+1

)else

:return

0然而實際這樣做的話會報錯誤:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.

any() or a.all()

什麼原因呢?那是因為傳入的 i 和 j 並不是數值,而是陣列!

具體是怎麼回事呢?我直接說結論吧:

1、並沒有各個座標迭代多次呼叫函式,實際上只呼叫了一次函式

2、傳入的引數是各個維度的座標陣列。例如shape是9x9的陣列,傳入的第乙個引數是:

[[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[ 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[ 2. 2. 2. 2. 2. 2. 2. 2. 2.]

[ 3. 3. 3. 3. 3. 3. 3. 3. 3.]

[ 4. 4. 4. 4. 4. 4. 4. 4. 4.]

[ 5. 5. 5. 5. 5. 5. 5. 5. 5.]

[ 6. 6. 6. 6. 6. 6. 6. 6. 6.]

[ 7. 7. 7. 7. 7. 7. 7. 7. 7.]

[ 8. 8. 8. 8. 8. 8. 8. 8. 8.]]

第二個引數是:

[[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]

[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]]

並沒有多次呼叫函式,而是通過一次呼叫,直接傳入的是各個維的座標陣列。

3、傳入各個維的座標陣列之後,剩下的就是陣列計算了。

比如 9*9 乘法口訣表,用 (i+1)*(j+1) 可以得到乙個9*9的乘法口訣表陣列。但注意這個口訣表是通過陣列計算得到的,而不是座標迭代。

3樓:

將乙個範圍內的座標代入前面定義的函式中,

fromfunction 的第二個引數既定義座標的範圍,也定義輸出array的shape

比如 (2, 3, 4) 相當於x, y, z,座標的範圍是 2 即(0,1),3 即(0,1,2), 4 即(0,1,2,3)

同時輸出的array的shape 也是(2,3,4)將座標代入函式,執行後,輸出

嗯,就是這樣

4樓:

numpy.fromfunction(function, shape, **kwargs)

Docstring:

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)``.

Parameters

function : callable

The function is called with N parameters, where N is the rank of`shape`. Each parameter represents the coordinates of the array varying along a specific axis. For example, if `shape` were ``(2, 2)``, then the parameters in turn be (0, 0), (0, 1),(1, 0),(1, 1).

shape : (N, ) tuple of ints

Shape of the output array, which also determines the shape of the coordinate array passed to `function`.

dtype : data-type, optional

Data-type of the coordinate arrays passed to `function`. By default, `dtype` is float.

fromfunction : any

The result of the call to `function` is passed back directly. Therefore the shape of `fromfunction` is completely determined by `function`. If `function` return a scalar value, the shape of `fromfunction` would match the `shape` parameter.

無恥的分割線

好吧!其實我是複製的幫助文件。可恥地匿名。

Numpy 的輸入與輸出整理 Datawhale

Cola 珂珂珂 為時不晚 我數學從四年級開始沒及格過 天無絕人之路,理科不好文科可以努力,你要是真的門門不及格,那你該想想自己的原因,是天生智力不夠,還是後天不努力,付出總比不付出得到的會更值得。 xx xx 小學和初中學習不好意味著1 基礎差 2 學習習慣和自控力不好 3 學習方法問題。這些要在...

python的numpy的淺拷貝的作用是什麼?

泰克尼客 NumPy的淺拷貝用來讓不同的ndarray物件共享資料區,一是為了減少記憶體的消耗,二是為了減少拷貝次數,提公升效能。要知道,很多時候都會使用NumPy來操作大型的矩陣,拷貝的消耗是巨大的。另外,a b這種操作根本就不是淺拷貝,Python裡的賦值操作傳遞的是引用,這句話的意思是讓 a和...

python的numpy向量化語句為什麼會比for快?

雲中客 Numpy運算是向量化的,進行是CPU指令集層面的並行運算,for迴圈是序列運算,效率肯定差多了。想象一下,乙個大矩陣做運算,for迴圈是一次提取兩個值,放入記憶體運算後返回結果,再提取2個再運算返回結果,如果迴圈。Numpy是一次性把所有值都提入記憶體同時運算同時返回結果,效率肯定不一樣。...