![](/img/trans.png)
[英]In reshape function of Numpy, what is the significance of giving three parameters, where the first one is -1?
[英]What is the significance of .flat in numpy?
這兩個代碼有何不同? 或者根本沒有這樣的差異?
import numpy as np
a = np.array([1,2,4,5,5,6])
for item in a.flat:
print(item)
要么
import numpy as np
a = np.array([1,2,4,5,5,6])
for i in a:
print(i)
兩者給出了相同的結果
1
2
4
5
5
6
屬性.flat
將扁平化陣列。 https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.html
np.array([[1], [2], [3], [4], [5]]).flat
>[1, 2, 3, 4, 5]
np.array([[[[[1, 2, 3, 4, 5]]]]]).flat
>[1, 2, 3, 4, 5]
np.array([1, 2, 3, 4, 5]).flat
>[1, 2, 3, 4, 5]
因為您使用的是已經平坦的數組,所以兩段代碼的輸出都是相同的。
嘗試這個:
import numpy as np
a = np.array([[1,2,4],[5,5,6]])
for item in a.flat:
print(item)
>1
>2
>4
>5
>5
>6
import numpy as np
a = np.array([[1,2,4],[5,5,6]])
for i in a:
print(i)
>[1 2 4]
>[5 5 6]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.