I have a situation that seems trivial but I can't figure it out. I have a dataset in Matlab that has categorical values. For example:
Outlook,Temperature,Humidity,Windy,Play sunny,hot,high,false,no sunny,hot,high,true,no overcast,hot,high,false,yes rainy,mild,high,false,yes rainy,cool,normal,false,yes rainy,cool,normal,true,no overcast,cool,normal,true,yes
In order to use
ClassificationTree (or other algorithm in machine learning), I need to convert the dataset to a matrix. I use (assuming that data contains the dataset):
which assigns numbers to the values. Let's say
sunny=3 and so on. I train the ClassificationTree and it works fine. But here is my problem. If I want to predict on test data:
I need to know the numbers that
double() assigned to each of the categories. i.e.
double() on the test set does not work because there is no guarantee that the numbers assigned by
double() are the same to those assigned to the training dataset.
I have found a really twisted way of doing it. For example, for
d = zeros(size(test)); ls = getlevels(training.Outlook); n = size(ls,2); for i = 1:n d(test.Outlook == ls(i),1) = i; end
This will assign the numbers correctly because I check against each of the values for
Outlook that I found in the training dataset. This is far from elegant, and there has to be something better.
Any help would be appreciated.