How to convert from categorical to double ?

조회 수: 84 (최근 30일)
Pierre Lonfat
Pierre Lonfat 2018년 4월 12일
댓글: Pierre Lonfat 2018년 4월 15일
Dear all,
I have a many different vectors 200x1 of categorical 0.5 0.75 1 1.25 1.5. These are the results of my machine learning predictions.
I simply want to reconvert these into double to make some calculations, i.e. averages of the prediction etc etc.
Many thanks in advance for helping me with that,
Pierre
  댓글 수: 1
Pierre Lonfat
Pierre Lonfat 2018년 4월 13일
편집: Pierre Lonfat 2018년 4월 13일
For anyone having the same trouble here is my (slow code) solution:
CATEGORIES_MATCHING_MATRIX=[YOUR_CATEGORIES_IN_DOUBLE; double(unique(CATEGORIES_MATRIX_RESULTS)')];
for i=1:length(CATEGORIES_MATRIX_RESULTS);
for j=1:size(CATEGORIES_MATRIX_RESULTS,2)
CATEGORIES_MATRIX_RESULTS(i,j)=CATEGORIES_MATCHING_MATRIX(1,CATEGORIES_MATRIX_RESULTS(i,j));
end
end

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

채택된 답변

Peter Perkins
Peter Perkins 2018년 4월 13일
Presumably when you created your categorical arrays, you used the vector [0.5 0.75 1 1.25 1.5] to define the unique raw values. Once you convert to categorical, they are GONE - calling categorical is a data conversion. You can convert back to double using the double function, but as you have observed, what you get are the category numbers.
But the best way to get back to your original numeric values is to save that 1x5 vector, and subscript into it using your categorical array. That's all you need to do. No loops, just one line.
>> x = [1 2 3]
x =
1 2 3
>> c = categorical([1 2 3 2 1],x,{'a' 'b' 'c'})
c =
1×5 categorical array
a b c b a
>> x(c)
ans =
1 2 3 2 1
However: one might ask the question, if your categories have "numeric" names, and you need to use the corresponding values to do numeric computations, why use categorical at all?
  댓글 수: 1
Pierre Lonfat
Pierre Lonfat 2018년 4월 15일
Thank you very much Peter ! I used categorical for some machine learning issues. I wanted to be sure that my classification methods recognise my output variable as categorical, this is why I converted it at the beginning :) !
Thank you for your answer !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by