當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.ma.choose()用法及代碼示例

numpy.ma.choose()函數使用索引數組從一組選擇中構造一個新數組。給定一個整數數組和一組n個選擇數組,此方法將創建一個合並每個選擇數組的新數組。如果arr中的arr值是i,則新數組將具有choices [i]包含在同一位置的值。

用法: numpy.ma.choose(arr, choices, out = None, mode = ‘raise’)

參數:
arr:[int的整數數組]此數組必須包含[0,n-1]中的整數,其中n是選擇的數目。
choices:[數組的順序]選擇數組。索引數組和所有選擇都應廣播為相同形狀。
out:[數組,可選]如果提供,結果將插入到此數組中。它應該具有適當的形狀和dtype。
mode:[{‘raise’,‘wrap’,‘clip’},可選]指定out-of-bounds索引的行為。 ‘raise’:引發錯誤。 ‘wrap’:環繞。 ‘clip’:剪切到範圍。

Return :一個合並每個選擇數組的新數組。

代碼1:



# Python program explaining 
# numpy.ma.choose() function 
    
# importing numpy as geek    
# and numpy.ma module as ma   
import numpy as geek   
import numpy.ma as ma 
   
choice = geek.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) 
arr = geek.array([2, 1, 0]) 
  
gfg = geek.ma.choose(arr, choice) 
  
print (gfg)

輸出:

[3 2 1]


代碼2:

# Python program explaining 
# numpy.ma.choose() function 
    
# importing numpy as geek    
# and numpy.ma module as ma   
import numpy as geek   
import numpy.ma as ma 
   
choice = geek.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) 
arr = geek.array([0, 1, 2]) 
  
gfg = geek.ma.choose(arr, choice) 
  
print (gfg)

輸出:

[1 2 3]



相關用法


注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 numpy.ma.choose() function – Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。