TabularDatasetFactory 类
包含为 Azure 机器学习创建表格数据集的方法。
使用此类中的方法创建 TabularDataset Afrom_*
,例如,该方法from_delimited_files。
有关使用表格数据集的详细信息,请参阅笔记本 https://aka.ms/tabulardataset-samplenotebook。
构造函数
TabularDatasetFactory()
方法
from_delimited_files |
创建 TabularDataset 以表示分隔文件中的表格数据(e.g. 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 数据帧创建数据集。 |
register_pandas_dataframe |
从 pandas 数据帧创建数据集。 |
register_spark_dataframe |
注释 这是一种实验性方法,随时可能更改。 有关详细信息,请参阅 https://aka.ms/azuremlexperimental。 从 spark 数据帧创建数据集。 |
from_delimited_files
创建 TabularDataset 以表示分隔文件中的表格数据(e.g. 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
必需
|
|
validate
必需
|
用于验证是否可以从返回的数据集加载数据的布尔值。 默认为 True。 验证要求可从当前计算访问数据源。 若要禁用验证,还需要将“infer_column_types”设置为 False。 |
include_path
必需
|
将路径信息保留为数据集中的列的布尔值。 默认为 False。 当读取多个文件时,这非常有用,并且想要知道特定记录源自哪个文件,或者要在文件路径中保留有用的信息。 |
infer_column_types
必需
|
用于推断列数据类型的布尔值。 默认为 True。 类型推理要求可从当前计算访问数据源。 当前类型推理只会拉取前 200 行。 如果数据包含多种类型的值,最好通过 set_column_types 参数提供所需的类型作为替代。 有关set_column_types的代码示例,请查看“备注”部分。 |
set_column_types
必需
|
用于设置列数据类型的字典,其中键为列名和值 DataType。 |
separator
必需
|
用于拆分列的分隔符。 |
header
必需
|
控制从文件读取时如何提升列标题。 对于具有相同标头的所有文件,默认值为 True。 文件将读取为没有标头 When header=False。 可以使用枚举值 PromoteHeadersBehavior指定更多选项。 |
partition_format
必需
|
指定路径的分区格式。 默认为 None。 每个路径的分区信息将基于指定的格式提取到列中。 设置部件“{column_name}”的格式将创建字符串列,“{column_name:yyyy/MM/dd/HH/mm/ss}”创建日期时间列,其中“yy”、“MM”、“dd”、“HH”、“mm”和“ss”用于提取日期时间类型的年、月、日、小时、分钟和秒。 格式应从第一个分区键的位置开始,直到文件路径的末尾。 例如,给定路径 “../Accounts/2019/01/01/data.csv',其中分区按部门名称和时间,partition_format='/{Department}/{PartitionDate:yyyy/MM/dd}/data.csv' 创建一个字符串列“Department”,其值为“Accounts”,而日期/时间列“PartitionDate”的值为“2019-01-01”。 |
support_multi_line
必需
|
默认情况下(support_multi_line=False),所有换行符(包括带引号的字段值中的换行符)都将解释为记录分隔符。 以这种方式读取数据更快并且更适合在多个 CPU 核心上并行执行。 但是,它可能会导致无声地生成更多字段值未对齐的记录。 当已知带分隔符的文件包含带引号的换行符时,应将其设置为 True。 将此 csv 文件作为示例,根据support_multi_line以不同的方式读取数据。 A、B、C A1、B1、C1 A2、“B 2”、C2
|
empty_as_string
必需
|
指定是否应将空字段值加载为空字符串。 默认值 (False) 将空字段值读取为 null。 将此值作为 True 传递会将空字段值读取为空字符串。 如果值转换为数字或日期时间,则这不起作用,因为空值将转换为 null。 |
encoding
必需
|
指定文件编码。 支持的编码为“utf8”、“iso88591”、“latin1”、“ascii”、“utf16”、“utf32”、“utf8bom”和“windows1252” |
返回
类型 | 说明 |
---|---|
返回 TabularDataset 对象。 |
注解
from_delimited_files 创建类的对象,该对象 TabularDataset 定义将数据从分隔文件加载到表格表示形式的作。
若要使 Azure 机器学习可以访问数据, 由路径 指定的分隔文件必须位于 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
必需
|
|
validate
必需
|
用于验证是否可以从返回的数据集加载数据的布尔值。 默认为 True。 验证要求可从当前计算访问数据源。 |
include_path
必需
|
将路径信息保留为数据集中的列的布尔值。 默认为 False。 当读取多个文件时,这非常有用,并且想要知道特定记录源自哪个文件,或者要在文件路径中保留有用的信息。 |
set_column_types
必需
|
用于设置列数据类型的字典,其中键是列名称和值 DataType |
partition_format
必需
|
指定路径的分区格式。 默认为 None。 每个路径的分区信息将基于指定的格式提取到列中。 设置部件“{column_name}”的格式将创建字符串列,“{column_name:yyyy/MM/dd/HH/mm/ss}”创建日期时间列,其中“yy”、“MM”、“dd”、“HH”、“mm”和“ss”用于提取日期时间类型的年、月、日、小时、分钟和秒。 格式应从第一个分区键的位置开始,直到文件路径的末尾。 例如,给定路径 “../Accounts/2019/01/01/data.jsonl',其中分区按部门名称和时间划分, partition_format=“/{Department}/{PartitionDate:yyyy/MM/dd}/data.jsonl”创建一个字符串列“Department”,其值为“Accounts”,日期/时间列“PartitionDate”的值为“2019-01-01”。 |
invalid_lines
必需
|
如何处理无效 JSON 的行。 支持的值为“error”和“drop”。 |
encoding
必需
|
指定文件编码。 支持的编码为“utf8”、“iso88591”、“latin1”、“ascii”、“utf16”、“utf32”、“utf8bom”和“windows1252” |
返回
类型 | 说明 |
---|---|
返回 TabularDataset 对象。 |
注解
from_json_lines_files 创建类的对象,该对象 TabularDataset 定义将数据从 JSON Lines 文件加载到表格表示形式的作。
若要使 Azure 机器学习可以访问数据, 由路径 指定的 JSON Lines 文件必须位于 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
列数据类型是从 JSON Lines 文件中保存的数据类型读取的。 提供 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
必需
|
|
validate
必需
|
用于验证是否可以从返回的数据集加载数据的布尔值。 默认为 True。 验证要求可从当前计算访问数据源。 |
include_path
必需
|
将路径信息保留为数据集中的列的布尔值。 默认为 False。 当读取多个文件时,这非常有用,并且想要知道特定记录源自哪个文件,或者要在文件路径中保留有用的信息。 |
set_column_types
必需
|
用于设置列数据类型的字典,其中键为列名和值 DataType。 |
partition_format
必需
|
指定路径的分区格式。 默认为 None。 每个路径的分区信息将基于指定的格式提取到列中。 设置部件“{column_name}”的格式将创建字符串列,“{column_name:yyyy/MM/dd/HH/mm/ss}”创建日期时间列,其中“yy”、“MM”、“dd”、“HH”、“mm”和“ss”用于提取日期时间类型的年、月、日、小时、分钟和秒。 格式应从第一个分区键的位置开始,直到文件路径的末尾。 例如,给定路径 “../Accounts/2019/01/data.parquet,其中分区按部门名称和时间划分,partition_format=“/{Department}/{PartitionDate:yyyy/MM/dd}/data.parquet”创建一个字符串列“Department”,其值为“Accounts”,日期/时间列“PartitionDate”的值为“2019-01-01”。 |
返回
类型 | 说明 |
---|---|
返回 TabularDataset 对象。 |
注解
from_parquet_files 创建类的对象,该对象 TabularDataset 定义将数据从 Parquet 文件加载到表格表示形式的作。
若要使 Azure 机器学习可以访问数据, 由路径 指定的 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
必需
|
SQL 类型的数据存储和查询。 |
validate
必需
|
用于验证是否可以从返回的数据集加载数据的布尔值。 默认为 True。 验证要求可从当前计算访问数据源。 |
set_column_types
必需
|
用于设置列数据类型的字典,其中键为列名和值 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 数据帧创建数据集。
static register_dask_dataframe(dataframe, target, name, description=None, tags=None, show_progress=True)
参数
名称 | 说明 |
---|---|
dataframe
必需
|
<xref:dask.dataframe.core.DataFrame>
必需,需要上传 dask 数据帧。 |
target
必需
|
必需,数据帧 parquet 数据将上传到的数据存储路径。 将在目标路径下生成 guid 文件夹,以避免冲突。 |
name
必需
|
必需,即已注册数据集的名称。 |
description
必需
|
可选。 数据集的文本说明。 默认为 None。 |
tags
必需
|
可选。 用于为数据集提供键值标记的字典。 默认为 None。 |
show_progress
必需
|
可选,指示是否在控制台中显示上传的进度。 默认值为 True。 |
返回
类型 | 说明 |
---|---|
已注册的数据集。 |
register_pandas_dataframe
从 pandas 数据帧创建数据集。
static register_pandas_dataframe(dataframe, target, name, description=None, tags=None, show_progress=True, row_group_size=None, make_target_path_unique=True)
参数
名称 | 说明 |
---|---|
dataframe
必需
|
需要,在要上传的内存数据帧中。 |
target
必需
|
必需,数据帧 parquet 数据将上传到的数据存储路径。 将在目标路径下生成 guid 文件夹,以避免冲突。 |
name
必需
|
必需,即已注册数据集的名称。 |
description
必需
|
可选。 数据集的文本说明。 默认为 None。 |
tags
必需
|
可选。 用于为数据集提供键值标记的字典。 默认为 None。 |
show_progress
必需
|
可选,指示是否在控制台中显示上传的进度。 默认值为 True。 |
row_group_size
必需
|
可选。 写入 parquet 文件时要使用的行组的最大大小。 默认为 None。 |
make_target_path_unique
必需
|
可选,指示是否应在目标中创建唯一子文件夹。 默认值为 True。 |
返回
类型 | 说明 |
---|---|
已注册的数据集。 |
register_spark_dataframe
注释
这是一种实验性方法,随时可能更改。 有关详细信息,请参阅 https://aka.ms/azuremlexperimental。
从 spark 数据帧创建数据集。
static register_spark_dataframe(dataframe, target, name, description=None, tags=None, show_progress=True)
参数
名称 | 说明 |
---|---|
dataframe
必需
|
需要,在要上传的内存数据帧中。 |
target
必需
|
必需,数据帧 parquet 数据将上传到的数据存储路径。 将在目标路径下生成 guid 文件夹,以避免冲突。 |
name
必需
|
必需,即已注册数据集的名称。 |
description
必需
|
可选。 数据集的文本说明。 默认为 None。 |
tags
必需
|
可选。 用于为数据集提供键值标记的字典。 默认为 None。 |
show_progress
必需
|
可选,指示是否在控制台中显示上传的进度。 默认值为 True。 |
返回
类型 | 说明 |
---|---|
已注册的数据集。 |