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 CenterFile Exchange에서 Get 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