Numpy > Masked array > マスクの部分を999999で置換する > numpy.ma.MaskedArray.filled() | fill_value引数指定の値で置換する
概要
- マスクされた要素('--')を持つ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]]
関連
Author And Source
この問題について(Numpy > Masked array > マスクの部分を999999で置換する > numpy.ma.MaskedArray.filled() | fill_value引数指定の値で置換する), 我々は、より多くの情報をここで見つけました https://qiita.com/7of9/items/59ba003333052386803c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .