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


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

先決條件:numpy

此numpy內置函數可抑製包含二維數組中包含掩碼值的整個列。

Syntax: numpy.ma.compress_cols(arr)

Parameters: 

arr:[array_like, MaskedArray]

  1. This parameter holds the array to operate on.
  2. The array must be a 2D array.
  3. If no array elements are masked, arr is interpreted as a MaskedArray with mask set to nomask.
     

返回:Returns the compressed array.



下麵是上述函數的實現。

範例1:

Python3

# importing numpy as geek 
import numpy as geek 
  
# defining an array with mask 
arr = geek.ma.array(geek.arange(6).reshape(2, 3), 
                    mask=[[1, 0, 0], [0, 0, 0]]) 
  
# applying mask to array elements 
gfg = geek.ma.compress_cols(arr) 
  
print(gfg)

輸出:

[[1 2]
 [4 5]]

範例2:

Python3

# importing numpy as geek 
import numpy as geek 
  
# defining array 
arr = geek.ma.array(geek.arange(9).reshape(3, 3), mask=[ 
                    [1, 0, 0], [1, 0, 0], [0, 0, 0]]) 
  
# applying mask to array elements 
gfg = geek.ma.compress_cols(arr) 
  
print(gfg)

輸出:

[[1 2]
 [4 5]
 [7 8]]




相關用法


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