mindspore/docs/api/api_python/dataset/mindspore.dataset.Schema.rst

90 lines
2.7 KiB
ReStructuredText
Raw Normal View History

2021-11-27 16:09:05 +08:00
mindspore.dataset.Schema
=========================
.. py:class:: mindspore.dataset.Schema(schema_file=None)
2021-11-23 15:00:48 +08:00
代表一个解析和存储数据列属性的类。
2021-11-27 16:09:05 +08:00
**参数:**
2021-12-04 20:36:47 +08:00
- **schema_file** (str): schema文件的路径默认值为None
2021-11-27 16:09:05 +08:00
**返回:**
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
schema对象关于数据集的行列配置的策略信息。
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
**异常:**
2021-11-23 15:00:48 +08:00
2021-12-02 20:06:28 +08:00
**RuntimeError** 模式文件加载失败。
2021-11-27 16:09:05 +08:00
**样例:**
>>> from mindspore import dtype as mstype
>>>
>>> # 创建模式指定列名、mindspore.dtype和列shape。
>>> schema = ds.Schema()
>>> schema.add_column(name='col1', de_type=mstype.int64, shape=[2])
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
.. py:method::add_column(name, de_type, shape=None)
2021-11-23 15:00:48 +08:00
向schema中添加新列。
2021-11-27 16:09:05 +08:00
**参数:**
- **name** (str): 列的新名称。
- **de_type** (str): 列的数据类型。
- **shape** (list[int], optional): 列shape默认值为None[-1]表示rank 1的未知shape
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
**异常:**
**ValueError** 列类型未知。
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
.. py:method::from_json(json_obj)
2021-11-23 15:00:48 +08:00
从JSON对象获取schema文件。
2021-11-27 16:09:05 +08:00
**参数:**
2021-12-04 20:36:47 +08:00
- **json_obj** (dictionary): 解析的JSON对象。
2021-11-27 16:09:05 +08:00
**异常:**
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
- **RuntimeError** 对象中存在未知的项。
- **RuntimeError** 对象中缺少数据集类型。
- **RuntimeError** 对象中缺少列。
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
.. py:method::parse_columns(columns)
2021-11-23 15:00:48 +08:00
解析传入的数据列的属性并将其添加到自身的schema中。
2021-11-27 16:09:05 +08:00
**参数:**
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
- **columns** (Union[dict, list[dict], tuple[dict]]): 数据集属性信息从schema文件解码。
2021-11-23 15:00:48 +08:00
2021-12-02 20:06:28 +08:00
- **list**[dict]'name'和'type'必须为key值'shape'可选。
- **dict**columns.keys()作为名称columns.values()是dict其中包含'type''shape'可选。
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
**异常:**
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
- **RuntimeError:** 解析列失败。
- **RuntimeError:** 列name字段缺失。
- **RuntimeError:** 列type字段缺失。
**样例:**
>>> schema = Schema()
>>> columns1 = [{'name': 'image', 'type': 'int8', 'shape': [3, 3]},
>>> {'name': 'label', 'type': 'int8', 'shape': [1]}]
>>> schema.parse_columns(columns1)
>>> columns2 = {'image': {'shape': [3, 3], 'type': 'int8'}, 'label': {'shape': [1], 'type': 'int8'}}
>>> schema.parse_columns(columns2)
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
.. py:method::to_json()
2021-11-23 15:00:48 +08:00
获取schema的JSON字符串。
2021-11-27 16:09:05 +08:00
**返回:**
str模式的JSON字符串。
2021-11-23 15:00:48 +08:00