TabularDatasetFactory 类

包含用于为 Azure 机器学习创建表格数据集的方法。

TabularDataset 是使用此类中的 from_* 方法创建的,例如方法 from_delimited_files

有关使用表格数据集的详细信息,请参阅笔记本 https://aka.ms/tabulardataset-samplenotebook

继承
builtins.object
TabularDatasetFactory

构造函数

TabularDatasetFactory()

方法

from_delimited_files

创建一个 TabularDataset,用于表示分隔文件(例如 CSV 和 TSV)中的表格数据。

from_json_lines_files

创建一个 TabularDataset,用于表示 JSON 行文件 (http://jsonlines.org/) 中的表格数据。

from_parquet_files

创建一个 TabularDataset,用于表示 Parquet 文件中的表格数据。

from_sql_query

创建一个 TabularDataset,用于表示 SQL 数据库中的表格数据。

register_dask_dataframe

注意

这是一个实验性的方法,可能会在任何时候更改。 有关详细信息,请参阅 https://aka.ms/azuremlexperimental

从 Dask DataFrame 创建数据集。

register_pandas_dataframe

从 Pandas DataFrame 创建数据集。

register_spark_dataframe

注意

这是一个实验性的方法,可能会在任何时候更改。 有关详细信息,请参阅 https://aka.ms/azuremlexperimental

从 Spark DataFrame 创建数据集。

from_delimited_files

创建一个 TabularDataset,用于表示分隔文件(例如 CSV 和 TSV)中的表格数据。

static from_delimited_files(path, validate=True, include_path=False, infer_column_types=True, set_column_types=None, separator=',', header=True, partition_format=None, support_multi_line=False, empty_as_string=False, encoding='utf8')

参数

path
Union[str, list[str], DataPath, list[DataPath], (Datastore, str), list[(Datastore, str)]]
必需

源文件的路径,可以是单个值或 url 字符串列表, (http[s]|abfs[s]|wasb[s]) 、 DataPath 对象或元组 Datastore 和相对路径。 请注意,路径列表不能同时包含 URL 和数据存储。

validate
bool
必需

一个布尔值,用于验证是否可以从返回的数据集加载数据。 默认为 True。 验证要求可从当前计算访问数据源。 若要禁用验证,还需将“infer_column_types”设置为 False。

include_path
bool
必需

一个布尔值,用于将路径信息保留为数据集中的列。 默认为 False。 在读取多个文件,并且想要知道特定记录源自哪个文件,或者想要在文件路径中保留有用的信息时,这非常有用。

infer_column_types
bool
必需

一个布尔值,用于推断列数据类型。 默认为 True。 类型推理要求可从当前计算访问数据源。 类型推理当前仅提取前 200 行。 如果数据包含多个类型的值,最好通过 set_column_types 参数提供所需的类型作为替代。 有关 set_column_types 的代码示例,请查看“注解”部分。

set_column_types
dict[str, DataType]
必需

用于设置列数据类型的字典,其中键是列名称,值为 DataType

separator
str
必需

用于拆分列的分隔符。

header
boolPromoteHeadersBehavior
必需

控制在读取文件时如何提升列标题。 对于具有相同标头的所有文件,默认值为 True。 如果标头为 False,文件将读取为不带标头。 可以使用 PromoteHeadersBehavior 的枚举值指定更多选项。

partition_format
str
必需

指定路径的分区格式。 默认值为“None”。 将会基于指定的格式将每个路径的分区信息提取到列中。 格式部分“{column_name}”创建字符串列,“{column_name:yyyy/MM/dd/HH/mm/ss}”创建日期/时间列,其中“yyyy”、“MM”、“dd”、“HH”、“mm”和“ss”分别用于提取日期/时间类型的年、月、日、小时、分钟和秒。 格式应从文件路径结束之前的第一个分区键位置开始。 例如,给定路径“../Accounts/2019/01/01/data.csv”,其中分区是按部门名称和时间划分的;partition_format='/{Department}/{PartitionDate:yyyy/MM/dd}/data.csv' 创建值为“Accounts”的字符串列“Department”和值为“2019-01-01”的日期/时间列“PartitionDate”。

support_multi_line
bool
必需

默认情况下 (support_multi_line=False),所有换行符(包括带引号的字段值中的那些)都将被解释为记录中断。 以这种方式读取数据更快并且更适合在多个 CPU 核心上并行执行。 但是,它可能会导致无声地生成更多字段值未对齐的记录。 在已知带分隔符的文件包含带引号的换行符的情况下,应将此项设置为 True。

如示例所示,根据 support_multi_line 以不同的方式读取数据。

A,B,C A1,B1,C1 A2,"B 2",C2


   from azureml.core import Dataset, Datastore
   from azureml.data.datapath import DataPath

   # default behavior: support_multi_line=False
   dataset = Dataset.Tabular.from_delimited_files(path=datastore_path)
   print(dataset.to_pandas_dataframe())
   #      A   B     C
   #  0  A1  B1    C1
   #  1  A2   B  None
   #  2  2"  C2  None

   # to handle quoted line breaks
   dataset = Dataset.Tabular.from_delimited_files(path=datastore_path,
                                                  support_multi_line=True)
   print(dataset.to_pandas_dataframe())
   #      A       B   C
   #  0  A1      B1  C1
   #  1  A2  B\r\n2  C2
empty_as_string
bool
必需

指定是否应将空字段值作为空字符串加载。 默认值 (False) 会将空字段值读取为 Null。 将此值作为 True 传递会将空字段值作为空字符串读取。 如果将值转换为数值或日期/时间,那么这将无效,因为空值将被转换为 Null。

encoding
str
必需

指定文件编码。 支持的编码如下:“utf8”、“iso88591”、“latin1”、“ascii”、“utf16”、“utf32”、“utf8bom”和“windows1252”

返回

返回一个 TabularDataset 对象。

返回类型

注解

from_delimited_files 创建 TabularDataset 类的对象,该类定义将数据从分隔文件加载到表格表示形式的操作。

要使数据可供 Azure 机器学习访问, path 指定的带分隔符的文件必须位于 Blob、ADLS Gen1 和 ADLS Gen2 的公共 Web URL 或 URL 中 Datastore 或后面。 如果用户的 AAD 令牌直接调用以下函数之一,则将在笔记本或本地 python 程序中使用:FileDataset.mount FileDataset.download FileDataset.to_path TabularDataset.to_pandas_dataframe TabularDataset.to_dask_dataframe TabularDataset.to_spark_dataframe TabularDataset.to_parquet_files TabularDataset.to_csv_files计算目标的标识将在 Experiment.submit 提交的作业中使用以进行数据访问身份验证。 了解详细信息:https://aka.ms/data-access

列数据类型默认情况下是从分隔文件中的数据推断出来的。 提供 set_column_types 将替代返回的 TabularDataset 中指定列的数据类型。


   from azureml.core import Dataset, Datastore

   # create tabular dataset from a single file in datastore
   datastore = Datastore.get(workspace, 'workspaceblobstore')
   tabular_dataset_1 = Dataset.Tabular.from_delimited_files(path=(datastore,'weather/2018/11.csv'))

   # create tabular dataset from a single directory in datastore
   datastore = Datastore.get(workspace, 'workspaceblobstore')
   tabular_dataset_2 = Dataset.Tabular.from_delimited_files(path=(datastore,'weather/'))

   # create tabular dataset from all csv files in the directory
   tabular_dataset_3 = Dataset.Tabular.from_delimited_files(path=(datastore,'weather/**/*.csv'))

   # create tabular dataset from multiple paths
   data_paths = [(datastore, 'weather/2018/11.csv'), (datastore, 'weather/2018/12.csv')]
   tabular_dataset_4 = Dataset.Tabular.from_delimited_files(path=data_paths)

   # create tabular dataset from url
   tabular_dataset_5 = Dataset.Tabular.from_delimited_files(path='https://url/weather/2018/12.csv')

   # use `set_column_types` to set column data types
   from azureml.data import DataType
   data_types = {
       'ID': DataType.to_string(),
       'Date': DataType.to_datetime('%d/%m/%Y %I:%M:%S %p'),
       'Count': DataType.to_long(),
       'Latitude': DataType.to_float(),
       'Found': DataType.to_bool()
   }
   web_path = [
       'https://url/weather/2018/11.csv',
       'https://url/weather/2018/12.csv'
   ]
   tabular = Dataset.Tabular.from_delimited_files(path=web_path, set_column_types=data_types)

from_json_lines_files

创建一个 TabularDataset,用于表示 JSON 行文件 (http://jsonlines.org/) 中的表格数据。

static from_json_lines_files(path, validate=True, include_path=False, set_column_types=None, partition_format=None, invalid_lines='error', encoding='utf8')

参数

path
Union[str, list[str], DataPath, list[DataPath], (Datastore, str), list[(Datastore, str)]]
必需

源文件的路径,可以是单个值或 url 字符串列表, (http[s]|abfs[s]|wasb[s]) 、 DataPath 对象或元组 Datastore 和相对路径。 请注意,路径列表不能同时包含 URL 和数据存储。

validate
bool
必需

一个布尔值,用于验证是否可以从返回的数据集加载数据。 默认为 True。 验证要求可从当前计算访问数据源。

include_path
bool
必需

一个布尔值,用于将路径信息保留为数据集中的列。 默认为 False。 在读取多个文件,并且想要知道特定记录源自哪个文件,或者想要在文件路径中保留有用的信息时,这非常有用。

set_column_types
dict[str, DataType]
必需

用于设置列数据类型的字典,其中键是列名称,值为 DataType

partition_format
str
必需

指定路径的分区格式。 默认值为“None”。 将会基于指定的格式将每个路径的分区信息提取到列中。 格式部分“{column_name}”创建字符串列,“{column_name:yyyy/MM/dd/HH/mm/ss}”创建日期/时间列,其中“yyyy”、“MM”、“dd”、“HH”、“mm”和“ss”分别用于提取日期/时间类型的年、月、日、小时、分钟和秒。 格式应从文件路径结束之前的第一个分区键位置开始。 例如,给定路径“../Accounts/2019/01/01/data.jsonl”,其中分区是按部门名称和时间划分的;partition_format='/{Department}/{PartitionDate:yyyy/MM/dd}/data.jsonl' 创建值为“Accounts”的字符串列“Department”和值为“2019-01-01”的日期/时间列“PartitionDate”。

invalid_lines
str
必需

如何处理无效 JSON 的行。 支持的值为“error”和“drop”。

encoding
str
必需

指定文件编码。 支持的编码如下:“utf8”、“iso88591”、“latin1”、“ascii”、“utf16”、“utf32”、“utf8bom”和“windows1252”

返回

返回一个 TabularDataset 对象。

返回类型

注解

from_json_lines_files 创建 TabularDataset 类的对象,该类定义将数据从 JSON 行文件加载到表格表示形式的操作。

要使数据可供 Azure 机器学习访问, path 指定的 JSON 行文件必须位于 Datastore Blob、ADLS Gen1 和 ADLS Gen2 的公共 Web URL 或 URL 中或后面。 如果用户的 AAD 令牌直接调用以下函数之一,则将在笔记本或本地 python 程序中使用:FileDataset.mount FileDataset.download FileDataset.to_path TabularDataset.to_pandas_dataframe TabularDataset.to_dask_dataframe TabularDataset.to_spark_dataframe TabularDataset.to_parquet_files TabularDataset.to_csv_files计算目标的标识将在 Experiment.submit 提交的作业中使用以进行数据访问身份验证。 了解详细信息:https://aka.ms/data-access

列数据类型是从保存在 JSON 行文件中的数据类型读取的。 提供 set_column_types 将替代返回的 TabularDataset 中指定列的数据类型。


   from azureml.core import Dataset, Datastore

   # create tabular dataset from a single file in datastore
   datastore = Datastore.get(workspace, 'workspaceblobstore')
   tabular_dataset_1 = Dataset.Tabular.from_json_lines_files(path=(datastore,'weather/2018/11.jsonl'))

   # create tabular dataset from a single directory in datastore
   datastore = Datastore.get(workspace, 'workspaceblobstore')
   tabular_dataset_2 = Dataset.Tabular.from_json_lines_files(path=(datastore,'weather/'))

   # create tabular dataset from all jsonl files in the directory
   tabular_dataset_3 = Dataset.Tabular.from_json_lines_files(path=(datastore,'weather/**/*.jsonl'))

   # create tabular dataset from multiple paths
   data_paths = [(datastore, 'weather/2018/11.jsonl'), (datastore, 'weather/2018/12.jsonl')]
   tabular_dataset_4 = Dataset.Tabular.from_json_lines_files(path=data_paths)

   # create tabular dataset from url
   tabular_dataset_5 = Dataset.Tabular.from_json_lines_files(path='https://url/weather/2018/12.jsonl')

   # use `set_column_types` to set column data types
   from azureml.data import DataType
   data_types = {
       'ID': DataType.to_string(),
       'Date': DataType.to_datetime('%d/%m/%Y %I:%M:%S %p'),
       'Count': DataType.to_long(),
       'Latitude': DataType.to_float(),
       'Found': DataType.to_bool()
   }
   web_path = [
       'https://url/weather/2018/11.jsonl',
       'https://url/weather/2018/12.jsonl'
   ]
   tabular = Dataset.Tabular.from_json_lines_files(path=web_path, set_column_types=data_types)

from_parquet_files

创建一个 TabularDataset,用于表示 Parquet 文件中的表格数据。

static from_parquet_files(path, validate=True, include_path=False, set_column_types=None, partition_format=None)

参数

path
Union[str, list[str], DataPath, list[DataPath], (Datastore, str), list[(Datastore, str)]]
必需

源文件的路径,可以是单个值或 url 字符串列表, (http[s]|abfs[s]|wasb[s]) 、 DataPath 对象或元组 Datastore 和相对路径。 请注意,路径列表不能同时包含 URL 和数据存储。

validate
bool
必需

一个布尔值,用于验证是否可以从返回的数据集加载数据。 默认为 True。 验证要求可从当前计算访问数据源。

include_path
bool
必需

一个布尔值,用于将路径信息保留为数据集中的列。 默认为 False。 在读取多个文件,并且想要知道特定记录源自哪个文件,或者想要在文件路径中保留有用的信息时,这非常有用。

set_column_types
dict[str, DataType]
必需

用于设置列数据类型的字典,其中键是列名称,值为 DataType

partition_format
str
必需

指定路径的分区格式。 默认值为“None”。 将会基于指定的格式将每个路径的分区信息提取到列中。 格式部分“{column_name}”创建字符串列,“{column_name:yyyy/MM/dd/HH/mm/ss}”创建日期/时间列,其中“yyyy”、“MM”、“dd”、“HH”、“mm”和“ss”分别用于提取日期/时间类型的年、月、日、小时、分钟和秒。 格式应从文件路径结束之前的第一个分区键位置开始。 例如,给定路径“../Accounts/2019/01/01/data.parquet”,其中分区是按部门名称和时间划分的;partition_format='/{Department}/{PartitionDate:yyyy/MM/dd}/data.parquet' 创建值为“Accounts”的字符串列“Department”和值为“2019-01-01”的日期/时间列“PartitionDate”。

返回

返回一个 TabularDataset 对象。

返回类型

注解

from_parquet_files 创建 TabularDataset 类的对象,该类定义将数据从 Parquet 文件加载到表格表示形式的操作。

要使数据可供 Azure 机器学习访问, path 指定的 Parquet 文件必须位于 Blob、ADLS Gen1 和 ADLS Gen2 的公共 Web URL 或 URL 中 Datastore 或后面。 如果用户的 AAD 令牌直接调用以下函数之一,则将在笔记本或本地 python 程序中使用:FileDataset.mount FileDataset.download FileDataset.to_path TabularDataset.to_pandas_dataframe TabularDataset.to_dask_dataframe TabularDataset.to_spark_dataframe TabularDataset.to_parquet_files TabularDataset.to_csv_files计算目标的标识将在 Experiment.submit 提交的作业中使用以进行数据访问身份验证。 了解详细信息:https://aka.ms/data-access

列数据类型是从保存在 Parquet 文件中的数据类型读取的。 提供 set_column_types 将替代返回的 TabularDataset 中指定列的数据类型。


   # create tabular dataset from a single file in datastore
   datastore = Datastore.get(workspace, 'workspaceblobstore')
   tabular_dataset_1 = Dataset.Tabular.from_parquet_files(path=(datastore,'weather/2018/11.parquet'))

   # create tabular dataset from a single directory in datastore
   datastore = Datastore.get(workspace, 'workspaceblobstore')
   tabular_dataset_2 = Dataset.Tabular.from_parquet_files(path=(datastore,'weather/'))

   # create tabular dataset from all parquet files in the directory
   tabular_dataset_3 = Dataset.Tabular.from_parquet_files(path=(datastore,'weather/**/*.parquet'))

   # create tabular dataset from multiple paths
   data_paths = [(datastore, 'weather/2018/11.parquet'), (datastore, 'weather/2018/12.parquet')]
   tabular_dataset_4 = Dataset.Tabular.from_parquet_files(path=data_paths)

   # create tabular dataset from url
   tabular_dataset_5 = Dataset.Tabular.from_parquet_files(path='https://url/weather/2018/12.parquet')

   # use `set_column_types` to set column data types
   from azureml.data import DataType
   data_types = {
       'ID': DataType.to_string(),
       'Date': DataType.to_datetime('%d/%m/%Y %I:%M:%S %p'),
       'Count': DataType.to_long(),
       'Latitude': DataType.to_float(),
       'Found': DataType.to_bool()
   }
   web_path = [
       'https://url/weather/2018/11.parquet',
       'https://url/weather/2018/12.parquet'
   ]
   tabular = Dataset.Tabular.from_parquet_files(path=web_path, set_column_types=data_types)

from_sql_query

创建一个 TabularDataset,用于表示 SQL 数据库中的表格数据。

static from_sql_query(query, validate=True, set_column_types=None, query_timeout=30)

参数

query
Union[DataPath, (Datastore, str)]
必需

SQL 类型的数据存储和查询。

validate
bool
必需

一个布尔值,用于验证是否可以从返回的数据集加载数据。 默认为 True。 验证要求可从当前计算访问数据源。

set_column_types
dict[str, DataType]
必需

用于设置列数据类型的字典,其中键是列名称,值为 DataType

query_timeout
必需

设置在终止尝试执行命令并生成错误之前的等待时间(以秒为单位)。 默认为 30 秒。

返回

返回一个 TabularDataset 对象。

返回类型

注解

from_sql_query 创建 TabularDataset 类的对象,该类定义将数据从 SQL 数据库加载到表格表示形式的操作。 目前,我们仅支持 MSSQLDataSource。

对于 Azure 机器学习可访问的数据,由 query 指定的 SQL 数据库必须位于 Datastore 中,并且数据存储类型必须为 SQL 类型。

列数据类型是从 SQL 查询结果中的数据类型读取的。 提供 set_column_types 将替代返回的 TabularDataset 中指定列的数据类型。


   from azureml.core import Dataset, Datastore
   from azureml.data.datapath import DataPath

   # create tabular dataset from a SQL database in datastore
   datastore = Datastore.get(workspace, 'mssql')
   query = DataPath(datastore, 'SELECT * FROM my_table')
   tabular = Dataset.Tabular.from_sql_query(query, query_timeout=10)
   df = tabular.to_pandas_dataframe()

   # use `set_column_types` to set column data types
   from azureml.data import DataType
   data_types = {
       'ID': DataType.to_string(),
       'Date': DataType.to_datetime('%d/%m/%Y %I:%M:%S %p'),
       'Count': DataType.to_long(),
       'Latitude': DataType.to_float(),
       'Found': DataType.to_bool()
   }
   tabular = Dataset.Tabular.from_sql_query(query, set_column_types=data_types)

register_dask_dataframe

注意

这是一个实验性的方法,可能会在任何时候更改。 有关详细信息,请参阅 https://aka.ms/azuremlexperimental

从 Dask DataFrame 创建数据集。

static register_dask_dataframe(dataframe, target, name, description=None, tags=None, show_progress=True)

参数

dataframe
<xref:dask.dataframe.core.DataFrame>
必需

必需,要上传的 Dask DataFrame。

target
Union[DataPath, Datastore, tuple(Datastore, str)]
必需

必需,DataFrame Parquet 数据将上传到的数据存储路径。 将在目标路径下生成 guid 文件夹,以避免冲突。

name
str
必需

必需,已注册的数据集的名称。

description
str
必需

可选。 数据集的文本说明。 默认值为“None”。

tags
dict[str, str]
必需

可选。 提供数据集的键值标记的字典。 默认值为“None”。

show_progress
bool
必需

可选,指示是否在控制台中显示上传进度。 默认值为 True。

返回

已注册的数据集。

返回类型

register_pandas_dataframe

从 Pandas DataFrame 创建数据集。

static register_pandas_dataframe(dataframe, target, name, description=None, tags=None, show_progress=True, row_group_size=None, make_target_path_unique=True)

参数

dataframe
DataFrame
必需

必需,在要上传的内存 DataFrame 中。

target
Union[DataPath, Datastore, tuple(Datastore, str)]
必需

必需,DataFrame Parquet 数据将上传到的数据存储路径。 将在目标路径下生成 guid 文件夹,以避免冲突。

name
str
必需

必需,已注册的数据集的名称。

description
int
必需

可选。 数据集的文本说明。 默认值为“None”。

tags
dict[str, str]
必需

可选。 提供数据集的键值标记的字典。 默认值为“None”。

show_progress
bool
必需

可选,指示是否在控制台中显示上传进度。 默认值为 True。

row_group_size
必需

可选。 写入 parquet 文件时要使用的行组的最大大小。 默认值为“None”。

make_target_path_unique
必需

可选,指示是否应在目标中创建唯一的子文件夹。 默认值为 True。

返回

已注册的数据集。

返回类型

register_spark_dataframe

注意

这是一个实验性的方法,可能会在任何时候更改。 有关详细信息,请参阅 https://aka.ms/azuremlexperimental

从 Spark DataFrame 创建数据集。

static register_spark_dataframe(dataframe, target, name, description=None, tags=None, show_progress=True)

参数

dataframe
DataFrame
必需

必需,在要上传的内存 DataFrame 中。

target
Union[DataPath, Datastore, tuple(Datastore, str)]
必需

必需,DataFrame Parquet 数据将上传到的数据存储路径。 将在目标路径下生成 guid 文件夹,以避免冲突。

name
str
必需

必需,已注册的数据集的名称。

description
str
必需

可选。 数据集的文本说明。 默认值为“None”。

tags
dict[str, str]
必需

可选。 提供数据集的键值标记的字典。 默认值为“None”。

show_progress
bool
必需

可选,指示是否在控制台中显示上传进度。 默认值为 True。

返回

已注册的数据集。

返回类型