How to convert from categorical to double ?

84 次查看(过去 30 天)
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