How to convert from categorical to double ?

84 vues (au cours des 30 derniers jours)
Pierre Lonfat
Pierre Lonfat le 12 Avr 2018
Commenté : Pierre Lonfat le 15 Avr 2018
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 commentaire
Pierre Lonfat
Pierre Lonfat le 13 Avr 2018
Modifié(e) : Pierre Lonfat le 13 Avr 2018
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

Connectez-vous pour commenter.

Réponse acceptée

Peter Perkins
Peter Perkins le 13 Avr 2018
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 commentaire
Pierre Lonfat
Pierre Lonfat le 15 Avr 2018
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 !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by