Python中的 numpy.ma.compress_rowcols()函数
numpy.ma.compress_rowcols()函数抑制二维数组中包含屏蔽值的行和列。
抑制行为通过轴参数选择:
- 如果axis为None,则行和列都被抑制。
- 如果轴为 0,则仅抑制行。
- 如果轴为 1 或 -1,则仅抑制列。
Syntax : numpy.ma.compress_rowcols(arr, axis = None)
Parameters :
arr : [array_like, MaskedArray] This parameter holds the array to operate on.The array must be a 2D array. If no array elements are masked, arr is interpreted as a MaskedArray with mask set to nomask.
axis : [int, optional] Axis along which to perform the operation. Default is None.
Return : Return the compressed array.
代码#1:
Python3
# Python program explaining
# numpy.ma.compress_rowcols() function
# importing numpy as geek
import numpy as geek
arr = geek.ma.array(geek.arange(6).reshape(2, 3),
mask=[[1, 0, 0], [0, 0, 0]])
gfg = geek.ma.compress_rowcols(arr)
print(gfg)
Python3
# Python program explaining
# numpy.ma.compress_rowcols() function
# importing numpy as geek
import numpy as geek
arr = geek.ma.array(geek.arange(6).reshape(2, 3),
mask=[[1, 0, 0], [0, 0, 0]])
gfg = geek.ma.compress_rowcols(arr, 1)
print(gfg)
输出:
[[4 5]]
代码#2:
蟒蛇3
# Python program explaining
# numpy.ma.compress_rowcols() function
# importing numpy as geek
import numpy as geek
arr = geek.ma.array(geek.arange(6).reshape(2, 3),
mask=[[1, 0, 0], [0, 0, 0]])
gfg = geek.ma.compress_rowcols(arr, 1)
print(gfg)
输出:
[[1 2]
[4 5]]