简体   繁体   English

如何将xarray数据集展平为1D numpy数组?

[英]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? 有没有一种简单的方法可以将xarray数据集展平为单个1D numpy数组?

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]

?

If you're OK with repeated values, you can use .to_array() and then flatten the values in NumPy, eg, 如果您对重复值没有问题,可以使用.to_array()然后在NumPy中展平值,例如,

>>> 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, eg, 如果你不想重复值,那么你需要自己写一些东西,例如,

>>> 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 更一般地说,这听起来有点类似于为机器学习应用程序在2D中“堆叠”数据变量的建议接口: https//github.com/pydata/xarray/issues/1317

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: 使用np.flatten()方法将数组减少到1D:

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

Then expand list of 1D arrays (as detailed in this answer ): 然后展开1D数组列表( 详见本答复 ):

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

Now convert this to an array as requested in Q (as currently a list): 现在将其转换为Q中请求的数组(目前是列表):

array = np.array(arrays)

截至2019年7月,xarray现在具有执行此功能的to_stacked_arrayto_unstacked_dataset函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM