0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Numpy > Masked array > マスクの部分を999999で置換する > numpy.ma.MaskedArray.filled() | fill_value引数指定の値で置換する

Last updated at Posted at 2020-02-10

概要

  • マスクされた要素('--')を持つarrayがある
  • 後続の処理のためにマスクを数値に置換したい

参考

実装1

    import numpy as np
    import numpy.ma as ma
     
    # checked with PEP8
     
    MASKED_VALUE = -32768
    SIZE_X = 3
    SIZE_Y = 3
     
    # 1. data preparation
    ncep = np.array([[3, 1, 4], [1, 5, 9], [2, 6, 5]])
    oisst_unmask = np.array([[2, 7, 1], [8, 2, 8], [1, 8, 2]])
    masks = np.array([[1, 0, 1], [1, 0, 1], [1, 0, 1]])
    oisst_masked = ma.masked_where(masks, oisst_unmask)
    print(oisst_masked)
    oisst_unmasked = oisst_masked.filled()
    print(oisst_unmasked)

下記のように置換できた。999999という値になる。

[[-- 7 --]
 [-- 2 --]
 [-- 8 --]]
[[999999      7 999999]
 [999999      2 999999]
 [999999      8 999999]]

実装2

fill_value指定の例。

import numpy as np
import numpy.ma as ma

# checked with PEP8

MASKED_VALUE = -32768
SIZE_X = 3
SIZE_Y = 3

# 1. data preparation
ncep = np.array([[3, 1, 4], [1, 5, 9], [2, 6, 5]])
oisst_unmask = np.array([[2, 7, 1], [8, 2, 8], [1, 8, 2]])
masks = np.array([[1, 0, 1], [1, 0, 1], [1, 0, 1]])
oisst_masked = ma.masked_where(masks, oisst_unmask)
print(oisst_masked)
oisst_unmasked = oisst_masked.filled(fill_value=MASKED_VALUE)
print(oisst_unmasked)
[[-- 7 --]
 [-- 2 --]
 [-- 8 --]]
[[-32768      7 -32768]
 [-32768      2 -32768]
 [-32768      8 -32768]]

関連

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?