Main Content

事後分類確率のプロット

この例では、単純ベイズ分類モデルによって予測した事後分類確率を可視化する方法を示します。

フィッシャーのアヤメのデータセットを読み込みます。

loadfisheririsX = meas(:,1:2); Y = species; labels = unique(Y);

Xは、150 本のアヤメについて 2 つの花弁の測定値が含まれている数値行列です。Yは、対応するアヤメの種類が含まれている文字ベクトルの cell 配列です。

散布図を使用してデータを可視化します。アヤメの種類によって変数をグループ化します。

figure; gscatter(X(:,1), X(:,2), species,'rgb','osd'); xlabel('Sepal length'); ylabel('Sepal width');

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent setosa, versicolor, virginica.

単純ベイズ分類器を学習させます。

mdl = fitcnb(X,Y);

mdlは学習させたClassificationNaiveBayes分類器です。

データの一定境界内で空間全体に広がる点のグリッドを作成します。X(:,1)のデータは 4.3 ~ 7.9 の範囲にあります。X(:,2)のデータは 2 ~ 4.4 の範囲にあります。

[xx1, xx2] = meshgrid(4:.01:8,2:.01:4.5); XGrid = [xx1(:) xx2(:)];

mdlを使用して、アヤメの種類とXGrid内の各観測値の事後クラス確率を予測します。

[predictedspecies,Posterior,~] = predict(mdl,XGrid);

種類ごとに事後確率分布をプロットします。

sz = size(xx1); s = max(Posterior,[],2); figure holdonsurf(xx1,xx2,reshape(Posterior(:,1),sz),'EdgeColor','none') surf(xx1,xx2,reshape(Posterior(:,2),sz),'EdgeColor','none') surf(xx1,xx2,reshape(Posterior(:,3),sz),'EdgeColor','none') xlabel('Sepal length'); ylabel('Sepal width'); colorbar view(2) holdoff

Figure contains an axes object. The axes object contains 3 objects of type surface.

観測値が決定面に近いほど、データが特定の種類に属する可能性が小さくなります。

分類確率分布を個別にプロットします。

figure('Units','Normalized','Position',[0.25,0.55,0.4,0.35]); holdonsurf(xx1,xx2,reshape(Posterior(:,1),sz),'FaceColor',“红色”,'EdgeColor','none') surf(xx1,xx2,reshape(Posterior(:,2),sz),'FaceColor','blue','EdgeColor','none') surf(xx1,xx2,reshape(Posterior(:,3),sz),'FaceColor','green','EdgeColor','none') xlabel('Sepal length'); ylabel('Sepal width'); zlabel('Probability'); legend(labels) title('Classification Probability') alpha(0.2) view(3) holdoff

Figure contains an axes object. The axes object with title Classification Probability contains 3 objects of type surface. These objects represent setosa, versicolor, virginica.

参考

関数

オブジェクト

関連するトピック