partition
划分数据存储
语法
说明
示例
将数据存储划分到特定数量的部分中
为大型文件集合创建数据存储。对于此示例,请使用示例文件 airlinesmall.csv
的十个副本。要处理表格数据中的缺失字段,请指定名称-值对组 TreatAsMissing
和 MissingValue
。
files = repmat({'airlinesmall.csv'},1,10); ds = tabularTextDatastore(files,... 'TreatAsMissing','NA','MissingValue',0);
将数据存储划分为三个部分并返回第一个分区。partition
函数返回数据存储 ds
中大约三分之一的数据。
subds = partition(ds,3,1)
subds = TabularTextDatastore with properties: Files: { ' ...\matlab\toolbox\matlab\demos\airlinesmall.csv'; ' ...\matlab\toolbox\matlab\demos\airlinesmall.csv'; ' ...\matlab\toolbox\matlab\demos\airlinesmall.csv' ... and 1 more } FileEncoding: 'UTF-8' AlternateFileSystemRoots: {} ReadVariableNames: true VariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more} Text Format Properties: NumHeaderLines: 0 Delimiter: ',' RowDelimiter: '\r\n' TreatAsMissing: 'NA' MissingValue: 0 Advanced Text Format Properties: TextscanFormats: {'%f', '%f', '%f' ... and 26 more} TextType: 'char' ExponentCharacters: 'eEdD' CommentStyle: '' Whitespace: ' \b\t' MultipleDelimitersAsOne: false Properties that control the table returned by preview, read, readall: SelectedVariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more} SelectedFormats: {'%f', '%f', '%f' ... and 26 more} ReadSize: 20000 rows
数据存储的 Files
属性包含该数据存储中所包含文件的列表。检查数据存储 ds
和分区后的数据存储 subds
的 Files
属性中的文件数。数据存储 ds
包含十个文件,分区 subds
包含前四个文件。
length(ds.Files)
ans = 10
length(subds.Files)
ans = 4
将数据存储划分到默认数量的部分中
根据示例文件 mapredout.mat
(mapreduce
函数的输出文件)创建一个数据存储。
ds = datastore('mapredout.mat');
为 ds
获取默认数量的分区。
n = numpartitions(ds);
将数据存储划分到默认数量的分区中并返回与第一个分区对应的数据存储。
subds = partition(ds,n,1);
读取 subds
中的数据。
while hasdata(subds) data = read(subds); end
按文件划分数据存储
创建一个包含三个图像文件的数据存储。
ds = imageDatastore({'street1.jpg','peppers.png','corn.tif'})
ds = ImageDatastore with properties: Files: { ' ...\matlab\toolbox\matlab\demos\street1.jpg'; ' ...\matlab\toolbox\matlab\imagesci\peppers.png'; ' ...\matlab\toolbox\matlab\imagesci\corn.tif' } ReadSize: 1 Labels: {} ReadFcn: @readDatastoreImage
按文件划分数据存储并返回与第二个文件对应的部分。
subds = partition(ds,'Files',2)
subds = ImageDatastore with properties: Files: { ' ...\matlab\toolbox\matlab\imagesci\peppers.png' } ReadSize: 1 Labels: {} ReadFcn: @readDatastoreImage
subds
包含一个文件。
并行划分数据
根据示例文件 mapredout.mat
(mapreduce
函数的输出文件)创建一个数据存储。
ds = datastore('mapredout.mat');
将数据存储划分为并行池中的三个工作进程的 3 个部分。
numWorkers = 3; p = parpool('local',numWorkers); n = numpartitions(ds,p); parfor ii=1:n subds = partition(ds,n,ii); while hasdata(subds) data = read(subds); end end
比较数据粒度
比较粗粒度分区和细粒度子集。
读取视频文件 xylophone.mp4
中的所有帧,并构造一个 ArrayDatastore
对象对其进行迭代。生成的对象有 141 个帧。
v = VideoReader("xylophone.mp4"); allFrames = read(v); arrds = arrayDatastore(allFrames,IterationDimension=4,OutputType="cell",ReadSize=4);
要提取一组特定的相邻帧,请创建四个粗粒度分区 arrds
。提取第二个分区,它有 35 个帧。
partds = partition(arrds,4,2); imshow(imtile(partds.readall()))
使用细粒度子集在指定索引处从 arrds
中提取六个不相邻的帧。
subds = subset(arrds,[67 79 82 69 89 33]); imshow(imtile(subds.readall()))
输入参数
ds
— 输入数据存储
数据存储
输入数据存储。可以使用 datastore
函数根据您的数据创建数据存储对象。
n
— 分区数
正整数
分区数量,指定为正整数。
如果您指定的分区数不是数据存储中文件数的数字因子,则 partition
将从第一个分区开始,将剩余的每个观测值放置在现有分区中。
包含一个额外观测值的现有分区数等于数据存储中的文件数除以分区数所得的余数。例如,如果您的数据存储对象包含 23 个文件,并且您要将其划分为 3 个部分,则 partition
创建的前两个分区将各包含 8 个文件,最后一个分区将包含 7 个文件。
示例: 3
数据类型: double
index
— 索引
正整数
索引,指定为正整数。
示例: 1
数据类型: double
filename
— 文件名
字符向量 | 字符串标量
文件名,指定为字符向量或字符串标量。
filename
的值必须与数据存储的 Files
属性包含的文件名完全匹配。要确保文件名完全匹配,请使用 ds.Files{N}
指定 filename
,其中 N
是 Files
属性中的文件的索引。例如,ds.Files{3}
指定数据存储 ds
中的第三个文件。
示例: ds.Files{3}
示例: 'file1.csv'
示例: '../dir/data/file1.csv'
示例: 'hdfs://myserver:7867/data/file1.txt'
数据类型: char
输出参量
subds
— 输出数据存储
数据存储
输出数据存储。输出数据存储的类型与输入数据存储 ds
的类型相同。
扩展功能
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
用法说明和限制:
在基于线程的环境中,您只能对以下数据存储使用
partition
:ImageDatastore
对象使用
combine
或transform
从ImageDatastore
对象创建的CombinedDatastore
、SequentialDatastore
或TransformedDatastore
对象
如果您有 Parallel Computing Toolbox™,则可以对其他数据存储使用
partition
。为此,请使用基于进程的并行池运行该函数,而不是使用backgroundPool
或ThreadPool
(使用ProcessPool
或ClusterPool
)。
有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2015a 中推出
另请参阅
datastore
| numpartitions
| subset
主题
- Partition a Datastore in Parallel (Parallel Computing Toolbox)
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)