onehotdecode
説明
は、A
= onehotdecode(B
,classes
,featureDim
)B
の各確率ベクトルを classes
で指定されたラベルのうちの最も確率の高いクラス ラベルに復号化します。featureDim
で確率ベクトルが定義されている次元を指定します。関数は、ベクトル内の最も大きい値の位置を classes
の対応する位置にあるクラス ラベルと一致させることで、確率ベクトルをクラス ラベルに復号化します。A
の各確率ベクトルは、その確率ベクトル内の最も大きい値に対応する classes
の値に置き換えられます。
例
one-hot 符号化と復号化のラベル
ラベルのセットを確率ベクトルに符号化し、それらを復号化してラベルに戻します。
色の種類を指定する categorical ラベルのベクトルを作成します。
colorsOriginal = ["red","blue","red","green","yellow","blue"]; colorsOriginal = categorical(colorsOriginal)
colorsOriginal = 1x6 categorical
red blue red green yellow blue
categorical ベクトルのクラスを調べます。
classes = categories(colorsOriginal)
classes = 4x1 cell
{'blue' }
{'green' }
{'red' }
{'yellow'}
関数 onehotencode
を使用して、ラベルを確率ベクトルに one-hot 符号化します。各行がクラスに対応し、各列が確率ベクトルに対応するように、最初の次元にラベルを符号化します。
colorsEncoded = onehotencode(colorsOriginal,1)
colorsEncoded = 4×6
0 1 0 0 0 1
0 0 0 1 0 0
1 0 1 0 0 0
0 0 0 0 1 0
関数 onehotdecode
を使用して、確率ベクトルを復号化します。
colorsDecoded = onehotdecode(colorsEncoded,classes,1)
colorsDecoded = 1x6 categorical
red blue red green yellow blue
復号化されたラベルは元のラベルと一致します。
One-Hot によるダミー変数の復号化
ダミー変数を作成し、元のデータに復号化します。
色の種類を指定して、カテゴリカル データの列ベクトルを作成します。
colorsOriginal = ["red";"blue";"red";"green";"yellow";"blue"]; colorsOriginal = categorical(colorsOriginal)
colorsOriginal = 6x1 categorical
red
blue
red
green
yellow
blue
categorical ベクトルのクラスを調べます。
classes = categories(colorsOriginal);
関数 dummyvar
を使用して、色の種類のそれぞれにダミー変数を作成します。
dummyColors = dummyvar(colorsOriginal)
dummyColors = 6×4
0 0 1 0
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 0 0 0
関数 onehotdecode
を使用して、2 番目の次元のダミー変数を復号化します。
colorsDecoded = onehotdecode(dummyColors,classes,2)
colorsDecoded = 6x1 categorical
red
blue
red
green
yellow
blue
復号化された変数は、元の色の種類と一致します。
最も確率の高いクラスへの確率ベクトルの復号化
確率ベクトルのセットを各観測値に対する最も確率の高いクラスに復号化します。
10 個のランダムな確率ベクトルのセットを作成します。ベクトルは、観測値が 5 つのクラスのうちのいずれかに属する確率を表します。
numObs = 10; numClasses = 5; prob = rand(numObs,numClasses); tot = sum(prob,2); prob = prob./tot
prob = 10×5
0.2938 0.0568 0.2365 0.2546 0.1582
0.3895 0.4174 0.0154 0.0137 0.1641
0.0427 0.3217 0.2854 0.0931 0.2573
0.2878 0.1529 0.2943 0.0145 0.2505
0.2640 0.3341 0.2834 0.0405 0.0780
0.0422 0.0614 0.3280 0.3564 0.2120
0.1078 0.1632 0.2876 0.2689 0.1725
0.1940 0.3249 0.1392 0.1125 0.2293
0.2356 0.1949 0.1613 0.2338 0.1745
0.3345 0.3326 0.0593 0.0119 0.2616
5 つのクラスのセットを定義します。
classes = ["Red","Yellow","Green","Blue","Purple"];
関数 onehotdecode
を使用して、最も確率の高いクラスに確率を復号化します。確率ベクトルは 2 番目の次元 (各列が一意のクラスに対応) に符号化されているため、符号化された確率を含む次元を 2
と指定します。最も確率の高いクラスを string ベクトルとして取得します。
result = onehotdecode(prob,classes,2,"string")
result = 10x1 string
"Red"
"Yellow"
"Yellow"
"Green"
"Yellow"
"Blue"
"Green"
"Yellow"
"Red"
"Red"
One-Hot による分類スコアの復号化
予測クラス スコアを予測ラベルに復号化します。
fisheriris
データ セットを読み込みます。150 本のアヤメについて 4 つの花弁の測定値が含まれる数値行列 X
を作成します。対応するアヤメの種類が含まれる categorical ラベルのベクトル S
を作成します。
load fisheriris
X = meas;
S = categorical(species);
関数 onehotencode
を使用して、ラベルを確率ベクトルに one-hot 符号化します。確率ベクトルを 2 番目の次元に符号化します。
Y = onehotencode(S,2);
単純な線形分類器の近似係数を計算します。
B = X\Y
B = 4×3
0.0834 0.2117 -0.1481
0.2533 -0.3059 0.1412
-0.2270 0.1888 0.0181
-0.0635 -0.5749 0.5873
近似係数からクラス スコアを予測し、スコアが範囲 [0,1] に収まっていることを確認します。
scores = X*B; scores = min(1,max(0,scores));
関数 onehotdecode
を使用して、予測クラス スコアを予測ラベルに復号化します。その後、混同チャートを作成して、真のラベル S
を予測ラベル label
と比較します。
label = onehotdecode(scores,categories(S),2); confusionchart(S,label)
入力引数
B
— 確率ベクトル
数値配列
復号化する確率ベクトル。数値行列として指定します。
B
の値は 0
から 1
までの間でなければなりません。B
の確率ベクトルに NaN
値が含まれている場合、関数はその観測値を NaN
でない最も確率の高いクラスに復号化します。観測値に NaN
値しか含まれていない場合、関数はその観測値を classes
の最初のクラス ラベルに復号化します。
データ型: single
| double
classes
— クラス
cell 配列 | string ベクトル | 数値ベクトル | 文字配列
クラス。文字ベクトルの cell 配列、string ベクトル、数値ベクトル、または 2 次元文字配列として指定します。
データ型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| string
| cell
| char
typename
— 復号化されたラベルのデータ型
'categorical'
(既定値) | 文字ベクトル | string スカラー
復号化されたラベルのデータ型。文字ベクトルまたは string スカラーとして指定します。
typename
の有効な値は、'categorical'
、'string'
、および 'single'
や 'int64'
などの数値型です。数値型を指定する場合、classes
は数値ベクトルでなければなりません。
例: 'double'
データ型: char
| string
出力引数
A
— 復号化されたクラス ラベル
categorical 配列 (既定値) | string 配列 | 数値配列
復号化されたクラス ラベル。categorical 配列、string 配列、または数値配列として返されます。
バージョン履歴
R2021b で導入
参考
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)