Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten an xarray dataset into a 1D numpy array?

Is there a simple way of flattening an xarray dataset into a single 1D numpy array?

For example, flattening the following test dataset:

xr.Dataset({
    'a' : xr.DataArray(
                   data=[10,11,12,13,14],
                   coords={'x':[0,1,2,3,4]},
                   dims={'x':5}
          ),
    'b' : xr.DataArray(data=1,coords={'y':0}),
    'c' : xr.DataArray(data=2,coords={'y':0}),
    'd' : xr.DataArray(data=3,coords={'y':0})
})

to

[10,11,12,13,14,1,2,3]

?

like image 706
user7821537 Avatar asked Oct 31 '17 12:10

user7821537


3 Answers

If you're OK with repeated values, you can use .to_array() and then flatten the values in NumPy, e.g.,

>>> ds.to_array().values.ravel()
array([10, 11, 12, 13, 14,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3,
        3,  3,  3])

If you don't want repeated values, then you'll need to write something yourself, e.g.,

>>> np.concatenate([v.values.ravel() for v in ds.data_vars.values()])
array([10, 11, 12, 13, 14,  1,  2,  3])

More generally, this sounds somewhat similar to a proposed interface for "stacking" data variables in 2D for machine learning applications: https://github.com/pydata/xarray/issues/1317

like image 107
shoyer Avatar answered Sep 21 '22 20:09

shoyer


As of July 2019, xarray now has the functions to_stacked_array and to_unstacked_dataset that perform this function.

like image 32
user7821537 Avatar answered Sep 21 '22 20:09

user7821537


Get Dataset from question:

ds = xr.Dataset({
'a' : xr.DataArray(
               data=[10,11,12,13,14],
               coords={'x':[0,1,2,3,4]},
               dims={'x':5}
      ),
'b' : xr.DataArray(data=1,coords={'y':0}),
'c' : xr.DataArray(data=2,coords={'y':0}),
'd' : xr.DataArray(data=3,coords={'y':0})
})

Get the list of data variables:

variables = ds.data_vars

Use the np.flatten() method to reduce arrays to 1D:

arrays = [ ds[i].values.flatten() for i in variables ] 

Then expand list of 1D arrays (as detailed in this answer):

arrays = [i for j in arrays for i in j  ]

Now convert this to an array as requested in Q (as currently a list):

array = np.array(arrays)
like image 34
tsherwen Avatar answered Sep 19 '22 20:09

tsherwen