Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, looking to extract values from a masked array, then rebuild an array

Tags:

python

numpy

I’m currently writing something that involves a lot of noise I’m attempting to remove, but in order do this I initially used masks, but the way in which I’m analysing the data breaks using a mask.

The masking is done, I’m looking to extract the data that is not masked, run analysis on this, then rebuild the array with the original order.

array([[3, 0, 3],
       [6, 7, 2],
       [2, 5, 0],
       [2, 1, 4]])

Make Mask

array([[-, -, -],
       [6, 7, 2],
       [-, -, -],
       [2, 1, 4]])

Extract Values

array([[6, 7, 2],
       [2, 1, 4]])

Do analysis

Rebuild Array

array([[-, -, -],
       [6, 7, 2],
       [-, -, -],
       [2, 1, 4]])

I’m hoping for an efficient way of doing this as I’m dealing with 100 million data points. Any suggestions are appreciated.

like image 368
ArcAngel Avatar asked Jan 07 '23 04:01

ArcAngel


1 Answers

You could use

masked[~masked.mask] = analyzed.ravel()

to reassign the analyzed values to the masked array.


import numpy as np

arr = np.array([[3, 0, 3],
                [6, 7, 2],
                [2, 5, 0],
                [2, 1, 4]])

masked = np.ma.masked_array(arr, mask=False)
masked.mask[::2, None] = True

extracted = np.ma.compress_rows(masked)

analyzed = extracted*10

masked[~masked.mask] = analyzed.ravel()

print(masked)

yields

[[-- -- --]
 [60 70 20]
 [-- -- --]
 [20 10 40]]
like image 171
unutbu Avatar answered Feb 03 '23 02:02

unutbu