# (2)python_numpy: numpy.ma.masked_invalid 与 numpy.ma.compress_rowcols 函数用法

Mask an array where invalid values occur (NaNs or infs).

### 2, numpy.ma.compress_rowcols(x, axis=None)

x：
axis：

example：

def np_masked_test():
arr1 = np.arange(0, 9, dtype=float).reshape(3, 3)
arr1[0][1] = np.NAN  # 无效值
arr1[1][0] = np.PINF  # 无效值
print('arr1:\n', '{arr1}'.format(arr1=arr1))
print('arr1_m:\n', '{arr1_m}'.format(arr1_m=arr1_m))

x = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
[1, 0, 0],
[0, 0, 0]])
print('x:\n', '{}'.format(x))
# ---------

# 1，remove rows and cols which are masked
print('supress_defalut:\n', '{supress_defalut}'.format(supress_defalut=supress_defalut))

# 2，remove rows which are masked
supress_rows = np.ma.compress_rowcols(arr1_m, 0)  # axis为0，则 rows are supressed
print('supress_rows:\n', '{supress_rows}'.format(supress_rows=supress_rows))
supress_rows1 = np.ma.compress_rows(arr1_m)  # 与axis为0相同，rows are supressed
print('supress_rows1:\n', '{supress_rows1}'.format(supress_rows1=supress_rows1))

# 3，remove cols which are masked
supress_cols = np.ma.compress_rowcols(arr1_m, 1)  # axis为1，则列被屏蔽
print('supress_cols:\n', '{supress_cols}'.format(supress_cols=supress_cols))
supress_cols1 = np.ma.compress_cols(arr1_m)  # 与axis为1相同，列被屏蔽
print('supress_cols1:\n', '{supress_cols1}'.format(supress_cols1=supress_cols1))

if __name__ == '__main__':


arr1:
[[ 0. nan  2.]
[inf  4.  5.]
[ 6.  7.  8.]]
arr1_m:
[[0.0 -- 2.0]
[-- 4.0 5.0]
[6.0 7.0 8.0]]
x:
[[-- 1 2]
[-- 4 5]
[6 7 8]]
supress_defalut:
[[8.]]
supress_rows:
[[6. 7. 8.]]
supress_rows1:
[[6. 7. 8.]]
supress_cols:
[[2.]
[5.]
[8.]]
supress_cols1:
[[2.]
[5.]
[8.]]

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

小旺的尾巴

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
08-18 349

03-23 5091
04-13 371
05-21 1122
01-04 1621
12-25 850
05-11 2420