我不知道如何从xr.Dataset
对象中实际提取数据。我不知道如何访问单个值。如何像使用 DataArrays 一样从 Datasets 中提取值(点值、向量、数组等)?
np.random.seed(0)
DA_data = xr.DataArray(np.random.random((3,2,10,100)), dims=["targets","accuracy","metrics","attributes"], name="Data")
DA_data.coords["attributes"] = ["attr_%d"%_ for _ in range(100)]
# DA_data.coords
# Coordinates:
# * targets (targets) int64 0 1 2
# * accuracy (accuracy) int64 0 1
# * metrics (metrics) int64 0 1 2 3 4 5 6 7 8 9
# * attributes (attributes) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
# Indexing DataArray
#DA_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values
#array(0.6458941130666561)
float(DA_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values)
#0.6458941130666561
# Indexing Dataset
DS_data = DA_data.to_dataset()
# DS_data
# <xarray.Dataset>
# Dimensions: (accuracy: 2, attributes: 100, metrics: 10, targets: 3)
# Coordinates:
# * targets (targets) int64 0 1 2
# * accuracy (accuracy) int64 0 1
# * metrics (metrics) int64 0 1 2 3 4 5 6 7 8 9
# * attributes (attributes) <U7 'attr_0' 'attr_1' 'attr_2' 'attr_3' ...
# Data variables:
# Data (targets, accuracy, metrics, attributes) float64 0.5488 ...
DS_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values
# <bound method Mapping.values of <xarray.Dataset>
# Dimensions: ()
# Coordinates:
# targets int64 0
# accuracy int64 0
# metrics int64 0
# attributes <U7 'attr_5'
# Data variables:
# Data float64 0.6459>
float(DS_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values)
# ---------------------------------------------------------------------------
# TypeError Traceback (most recent call last)
# <ipython-input-408-e0c88e8541d8> in <module>()
# 38 # Data variables:
# 39 # Data float64 0.6459>
# ---> 40 float(DS_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values)
# TypeError: float() argument must be a string or a number, not 'method'