このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。
t-SNE の使用による高次元データの可視化
この例では、関数 tsne
を使用して、手書き数字のイメージから構成されている MNIST データ [1] を可視化する方法を示します。イメージはグレースケールの 28 x 28 ピクセルです。各イメージには、イメージが表す数字である 0 ~ 9 のラベルが関連付けられています。tsne
は、PCA を使用してデータの次元数を元の 784 から 50 に削減してから、t-SNE の Barnes-Hut アルゴリズムを使用して 2 または 3 に削減します。
データの取得
はじめに、イメージとラベルのデータを以下から取得します。
http://yann.lecun.com/exdb/mnist/
ファイルを解凍します。この例では t10k-images
データを使用します。
imageFileName = 't10k-images.idx3-ubyte'; labelFileName = 't10k-labels.idx1-ubyte';
ファイルを処理してワークスペースに読み込みます。この処理関数のコードは、この例の終わりで示します。
[X,L] = processMNISTdata(imageFileName,labelFileName);
Read MNIST image data... Number of images in the dataset: 10000 ... Each image is of 28 by 28 pixels... The image data is read to a matrix of dimensions: 10000 by 784... End of reading image data. Read MNIST label data... Number of labels in the dataset: 10000 ... The label data is read to a matrix of dimensions: 10000 by 1... End of reading label data.
データの次元数を 2 に削減
t-SNE を使用して、データ クラスターの 2 次元類似物を取得します。PCA を使用して、初期次元を 50 に削減します。この比較的大きいデータセットで時間を節約するため、t-SNE アルゴリズムの Barnes-Hut バリアントを使用します。
rng default % for reproducibility Y = tsne(X,'Algorithm','barneshut','NumPCAComponents',50);
正しいラベルで色を付けて、結果を表示します。
figure numGroups = length(unique(L)); clr = hsv(numGroups); gscatter(Y(:,1),Y(:,2),L,clr)
t-SNE は、真のラベルに密接に対応する相対的な類似度のみに基づいて点のクラスターを作成します。
データの次元数を 3 に削減
t-SNE は、データの次元数を 3 に削減することもできます。tsne
の名前と値のペア 'NumDimensions'
を 3
に設定します。
rng default % for fair comparison Y3 = tsne(X,'Algorithm','barneshut','NumPCAComponents',50,'NumDimensions',3); figure scatter3(Y3(:,1),Y3(:,2),Y3(:,3),15,clr(L+1,:),'filled'); view(-93,14)
以下は、データをワークスペースに読み込む関数のコードです。
function [X,L] = processMNISTdata(imageFileName,labelFileName) [fileID,errmsg] = fopen(imageFileName,'r','b'); if fileID < 0 error(errmsg); end %% % First read the magic number. This number is 2051 for image data, and % 2049 for label data magicNum = fread(fileID,1,'int32',0,'b'); if magicNum == 2051 fprintf('\nRead MNIST image data...\n') end %% % Then read the number of images, number of rows, and number of columns numImages = fread(fileID,1,'int32',0,'b'); fprintf('Number of images in the dataset: %6d ...\n',numImages); numRows = fread(fileID,1,'int32',0,'b'); numCols = fread(fileID,1,'int32',0,'b'); fprintf('Each image is of %2d by %2d pixels...\n',numRows,numCols); %% % Read the image data X = fread(fileID,inf,'unsigned char'); %% % Reshape the data to array X X = reshape(X,numCols,numRows,numImages); X = permute(X,[2 1 3]); %% % Then flatten each image data into a 1 by (numRows*numCols) vector, and % store all the image data into a numImages by (numRows*numCols) array. X = reshape(X,numRows*numCols,numImages)'; fprintf(['The image data is read to a matrix of dimensions: %6d by %4d...\n',... 'End of reading image data.\n'],size(X,1),size(X,2)); %% % Close the file fclose(fileID); %% % Similarly, read the label data. [fileID,errmsg] = fopen(labelFileName,'r','b'); if fileID < 0 error(errmsg); end magicNum = fread(fileID,1,'int32',0,'b'); if magicNum == 2049 fprintf('\nRead MNIST label data...\n') end numItems = fread(fileID,1,'int32',0,'b'); fprintf('Number of labels in the dataset: %6d ...\n',numItems); L = fread(fileID,inf,'unsigned char'); fprintf(['The label data is read to a matrix of dimensions: %6d by %2d...\n',... 'End of reading label data.\n'],size(L,1),size(L,2)); fclose(fileID);
参考文献
[1] 元の NIST データセットから派生して作成された MNIST データセットの著作権は Yann LeCun (Courant Institute, NYU) と Corinna Cortes (Google Labs, New York) が有しています。MNIST データセットは、Creative Commons Attribution-Share Alike 3.0 ライセンス (https://creativecommons.org/licenses/by-sa/3.0/) の条件下で使用可能になります。