Onehotencode function is giving more columns which are filled with zeros than the number of categories.

1 回表示 (過去 30 日間)
a = readtable("mushrooms.csv");
aa = categorical(table2array(a));
b = onehotencode(aa(:,1),2);
first column has 2 categories but function creates 23 columns. Which is also the total number of columns in variable a. Also other variables behave the same too and seemingly 2 spesific column in each onehotencoded variable has the true onehotencoded variables. Can you help me please?

採用された回答

Mohammad Sami
Mohammad Sami 2021 年 9 月 10 日
Because you are converting the entire matrix in to a categorical matrix, the categories for the entire matrix are common. If you want to have different categories for each column. you will have to convert each columm to caterogical separately.
a = repmat(1:10,10,1);
b = categorical(a)
b = 10×10 categorical array
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
categories(b(:,1))
ans = 10×1 cell array
{'1' } {'2' } {'3' } {'4' } {'5' } {'6' } {'7' } {'8' } {'9' } {'10'}
c = array2table(a);
c = varfun(@categorical,c)
c = 10×10 table
categorical_a1 categorical_a2 categorical_a3 categorical_a4 categorical_a5 categorical_a6 categorical_a7 categorical_a8 categorical_a9 categorical_a10 ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ _______________ 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
categories(c{:,1})
ans = 1×1 cell array
{'1'}

その他の回答 (1 件)

Yagiz Dereboy
Yagiz Dereboy 2021 年 9 月 11 日
Thanks!!

カテゴリ

Help Center および File ExchangeGet Started with Statistics and Machine Learning Toolbox についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by