4

我不知道如何从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'
4

1 回答 1

6

这有点令人困惑,但在and上的.values工作方式不同:DatasetDataArray

  • DataArray.values返回一个 NumPy 数组。这种行为与熊猫一致。
  • Dataset.values()返回构成数据集的 DataArray 对象的列表 (Python 2) 或 ValuesView (Python 3)。Dataset这种行为与满足 Python 的Mapping接口是一致的。

要从 a 中提取值Dataset,您需要DataArray通过数据集的类字典接口提取 a ,例如float(DA_data['Data'])float(DA_data.values()[0])。您不能直接将 aDataset转换为 float 或 NumPy 数组,就像使用 Python dict 一样。

于 2016-07-21T19:20:59.290 回答