Main Content

partition

データストアを分割する

説明

subds = partition(ds,n,index) は、n で指定された個数にデータストア ds を分割し、インデックス index に対応する区画を返します。

subds = partition(ds,'Files',index) は、データストアをファイル別に分割し、Files プロパティのインデックス index が示すファイルに対応する区画を返します。

subds = partition(ds,'Files',filename) は、データストアをファイル別に分割し、filename で指定されたファイルに対応する区画を返します。

すべて折りたたむ

ファイルの大規模な集合のデータストアを作成します。この例では、サンプル ファイル airlinesmall.csv の 10 個のコピーを使用します。表形式データの欠損フィールドを処理するには、名前と値のペア TreatAsMissingMissingValue を指定します。

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 プロパティには、データストア内のファイルのリストが格納されます。データストア dsFiles プロパティおよび分割されたデータストア 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()))

Figure contains an axes object. The axes object contains an object of type image.

粒度の細かいサブセットを使用して、指定されたインデックスにおける 6 個の隣接しないフレームを arrds から抽出します。

subds = subset(arrds,[67 79 82 69 89 33]);
imshow(imtile(subds.readall()))

Figure contains an axes object. The axes object contains an object of type image.

入力引数

すべて折りたたむ

入力データストア。関数 datastore を使用して、データから datastore オブジェクトを作成できます。

分割数。正の整数として指定します。

指定した分割数が、データストア内のファイル数の因数でない場合、partition は残りの観測値を既存の区画に、最初の区画から順番に配置します。

追加の観測値がある既存のパーティションの数は、データストア内のファイル数を区画数で除算したときの剰余と等しくなります。たとえば、データストア オブジェクトに 23 個のファイルがあり、3 つの部分に分割する場合、partition により作成される最初の 2 つの区画には 8 個のファイル、最後の区画には 7 個のファイルが含まれます。

例: 3

データ型: double

インデックス。正の整数として指定します。

例: 1

データ型: double

ファイル名。文字ベクトルまたは string スカラーとして指定します。

filename の値は、データストアの Files プロパティに含まれるファイル名と完全に一致しなければなりません。ファイル名を完全に一致させるには、ds.Files{N} を使用して filename を指定します。ここで NFiles プロパティ内のファイルのインデックスです。たとえば、ds.Files{3} はデータストア ds 内の 3 番目のファイルを指定します。

例: ds.Files{3}

例: 'file1.csv'

例: '../dir/data/file1.csv'

例: 'hdfs://myserver:7867/data/file1.txt'

データ型: char

出力引数

すべて折りたたむ

出力データストア。出力データストアは、入力データストア ds と同じ種類です。

拡張機能

バージョン履歴

R2015a で導入

参考

| |

トピック