convert categorical to numeric

조회 수: 561 (최근 30일)
Fischer Zheng
Fischer Zheng . 2016년 1월 19일
댓글: the cyclist . 2022년 7월 25일
I have a categorical array and I want to convert it back to the numerical matrix. What is the syntax?
  댓글 수: 3
the cyclist
the cyclist 2022년 7월 25일
@AMEN BARGEES, if you look at other solutions, besides the accepted one, you will see that others suggested double(), followed by comments about how it did not actually solve the problem that was posed.

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2016년 1월 19일
편집: the cyclist 님. 2016년 1월 19일
If you have the Statistics and Machine Learning Toolbox, you could use the grp2idx command:
c = categorical({'Male','Female','Female','Male','Female'})
n = grp2idx(c)
That will simply encode the categories as numerical variables (which is handy for some other software packages). But that does not really change the fact that "1", "2" etc are still really just categories.
If you have categories that somehow embed numbers inside of them, that you want to convert to truly numerical (e.g. ordinal or interval) data, you'll need to be more specific about what your input is.
  댓글 수: 7
the cyclist
the cyclist 2017년 8월 23일
I suggest you open a new question. You will get the attention of more people with a new, unanswered question rather than a comment on an answered question.
In that new question, I suggest that you include a small example of your data, or upload the entire array in a MAT file. You have not given enough information here to help you.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Matthew Parkan
Matthew Parkan 2018년 3월 19일
Juste use the unique() function (which does not require any toolbox).
For example:
c = categorical({'Red','Blue','Red','Red','Blue','Blue','Green'});
[GN, ~, G] = unique(c)
Will return:
GN =
1×3 categorical array
Blue Green Red
G =
  댓글 수: 1
the cyclist
the cyclist 2018년 3월 19일
My comment on Xingyu Li's answer applies here as well. It works well if arbitrary numeric values are OK as output, but will not convert categorical '12' to numeric 12.

댓글을 달려면 로그인하십시오.

Peter Perkins
Peter Perkins 2018년 3월 23일

Calling categorical is a data conversion, so

   c = categorical([12 12 13])

completely throws away the numeric values. In general, there is no way to get them back unless you have saved them, any more than you can get back the original values from int8([1.1 2.2 3.3]). Calling categorical is a data conversion.

That being said, you can certainly save the unique numeric values, and then index into those using the categorical array:

   n = uniqueNumericValues(c)

You can also call double on a categorical, but what you will get back are the category numbers, not the original numeric values.

But here's the question: if you need to convert back to the original numbers, and you are not using meaningful category names when converting from those numbers, why use categorical to begin with? There may be things you haven't mentioned.

  댓글 수: 4
Matthew Anderson
Matthew Anderson 2020년 4월 13일
a = categorical(["2" "3" "3"])
double(a) % returns [1 2 2] - maybe desired for some reason
double(string(a)) % returns [2 3 3] - maybe desired for some reason
categorical(double(string(a)) % returns the same thing as a

댓글을 달려면 로그인하십시오.

Milan Andrejevic
Milan Andrejevic 2018년 4월 29일
It's an intuitive functionality that should exist. There are so many instances one needs to treat certain variables as categorical when using some modelling functions, and as continuous for other analyses, or simply be able to index the array comparing it to a number. This is so easy to do in other programming languages.

Xingyu Li
Xingyu Li 2017년 12월 15일
  댓글 수: 1
the cyclist
the cyclist 2017년 12월 15일
편집: the cyclist 님. 2018년 3월 19일
This is a great solution for the use case of assigning arbitrary numeric values to general categorical variables, e.g.
c = categorical({'Male','Female','Female','Male','Female'})
But this will not solve this poster's particular use case of
c = categorical([12 12 13]);
and wanting numeric [12 12 13] as the output.

댓글을 달려면 로그인하십시오.

nathan blanc
nathan blanc 2021년 1월 16일
I converted the categorical data into a char and then used str2num. worked for me :)
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 1월 16일
In most cases it is better to use str2double() rather than str2num(). str2num() invokes the full power of eval(), which can lead to problems.

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기


아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by