partition
データストアを分割する
構文
説明
例
データストアを指定した数の部分に分割
ファイルの大規模な集合のデータストアを作成します。この例では、サンプル ファイル airlinesmall.csv
の 10 個のコピーを使用します。表形式データの欠損フィールドを処理するには、名前と値のペア TreatAsMissing
と MissingValue
を指定します。
files = repmat({'airlinesmall.csv'},1,10); ds = tabularTextDatastore(files,... 'TreatAsMissing','NA','MissingValue',0);
データストアを 3 つの部分に分割し、最初の区画を返します。関数 partition
は、データストア ds
から最初の約 1/3 のデータを返します。
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
の Files
プロパティおよび分割されたデータストア subds
にあるファイルの数をチェックします。データストア ds
には 10 個のファイル、区画 subds
には最初の 4 つのファイルが格納されています。
length(ds.Files)
ans = 10
length(subds.Files)
ans = 4
データストアを既定の数の部分に分割
関数 mapreduce
の出力ファイルであるサンプル ファイル mapredout.mat
からデータストアを作成します。
ds = datastore('mapredout.mat');
ds
の既定の分割数を取得します。
n = numpartitions(ds);
データストアを既定の数に分割し、最初の分割部分に対応するデータストアを返します。
subds = partition(ds,n,1);
subds
内のデータを読み取ります。
while hasdata(subds) data = read(subds); end
データストアをファイル別に分割
イメージ ファイルを 3 つ含むデータストアを作成します。
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
データストアをファイル別に分割し、2 番目のファイルに対応する部分を返します。
subds = partition(ds,'Files',2)
subds = ImageDatastore with properties: Files: { ' ...\matlab\toolbox\matlab\imagesci\peppers.png' } ReadSize: 1 Labels: {} ReadFcn: @readDatastoreImage
subds
にはファイルが 1 つあります。
並列でのデータ分割
関数 mapreduce
の出力ファイルであるサンプル ファイル mapredout.mat
からデータストアを作成します。
ds = datastore('mapredout.mat');
並列プール内の 3 個のワーカーに対して、データストアを 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
の粒度の粗い分割を 4 個作成します。35 個のフレームをもつ、2 番目の分割を抽出します。
partds = partition(arrds,4,2); imshow(imtile(partds.readall()))
粒度の細かいサブセットを使用して、指定されたインデックスにおける 6 個の隣接しないフレームを arrds
から抽出します。
subds = subset(arrds,[67 79 82 69 89 33]); imshow(imtile(subds.readall()))
入力引数
ds
— 入力データストア
データストア
入力データストア。関数 datastore
を使用して、データから datastore オブジェクトを作成できます。
n
— 分割数
正の整数
分割数。正の整数として指定します。
指定した分割数が、データストア内のファイル数の因数でない場合、partition
は残りの観測値を既存の区画に、最初の区画から順番に配置します。
追加の観測値がある既存のパーティションの数は、データストア内のファイル数を区画数で除算したときの剰余と等しくなります。たとえば、データストア オブジェクトに 23 個のファイルがあり、3 つの部分に分割する場合、partition
により作成される最初の 2 つの区画には 8 個のファイル、最後の区画には 7 個のファイルが含まれます。
例: 3
データ型: double
index
— インデックス
正の整数
インデックス。正の整数として指定します。
例: 1
データ型: double
filename
— ファイル名
文字ベクトル | string スカラー
ファイル名。文字ベクトルまたは string スカラーとして指定します。
filename
の値は、データストアの Files
プロパティに含まれるファイル名と完全に一致しなければなりません。ファイル名を完全に一致させるには、ds.Files{N}
を使用して filename
を指定します。ここで N
は Files
プロパティ内のファイルのインデックスです。たとえば、ds.Files{3}
はデータストア ds
内の 3 番目のファイルを指定します。
例: 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 で導入
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)