[英]Numpy subsetting and assignment
假設您想要將 NumPy 數組fridge_items
項目子集化為評級高於 7 的美味事物行。
most_tasty_items = fridge_items[:,10] > 7)
您將返回一組 boolean 值。
如果你這樣做了:
fridge_items[most_tasty_items,:][:3,:]
當你索引到冰箱項目時,這里到底發生了什么。 我熟悉做 array[1,2] 並返回給定行和列的內容。
由於most_tasty_items
是 boolean 值的一維數組,我們如何使用[:3,:]
對其進行索引? 如果它只是一個一維數組,我們可以說 [:]。 不太明白這一點,以及為什么我們將:
作為[most_tasty_items,:]
的第二個參數
當您使用兩個括號對數據進行尋址時,您正在執行兩個操作,第一個括號 select 來自數據的新數組,第二個括號尋址新數組。
In [71]: np.random.seed(2020)
...: fridge = np.random.randint(11, size=(30, 5))
...: tasty = fridge_items[:,4] > 7
...: tastyfridge = fridge[tasty,:]
In [72]: tastyfridge[:2,:], fridge[tasty][:2,:]
Out[72]:
(array([[ 8, 10, 9, 3, 7],
[ 4, 7, 1, 4, 9]]),
array([[ 8, 10, 9, 3, 7],
[ 4, 7, 1, 4, 9]]))
In [73]:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.