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

1 visualización (últimos 30 días)
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?

Respuesta aceptada

Mohammad Sami
Mohammad Sami el 10 de Sept. de 2021
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'}

Más respuestas (1)

Yagiz Dereboy
Yagiz Dereboy el 11 de Sept. de 2021
Thanks!!

Categorías

Más información sobre Image Data Workflows en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by