make categorical array out of 4D array
Show older comments
hi,
When I want to use the cross-entropy solver in matlab, I have to use the classificationlayer at the end of my layers and therefore the responses have to be a categorical array. My response array has the size of [1 1 500 7000] where the last two indexes are the the scale of the sample(500) as well as the number of observations (7000).
For using this as a categorical response, I know that is has to be in the form of nx1, as noted in the documantation. However, I don´t how to extract my response array to a categorical one. Any Ideas?
Thanks in advance!
6 Comments
Walter Roberson
on 4 Mar 2022
You need some other information that is number of samples x 1 categorical array. You do not extract it from the [1 1 500 7000] -- not unless one of the columns is the category information. If one of the columns is the category information, you need to remove that from the information sent to training.
Kuno Bruswachtl
on 7 Mar 2022
Walter Roberson
on 7 Mar 2022
Normally what you have is a number of related readings (for example, temperature, pressure, concentration) that together define a "sample", together with information about what class (category) that particular sample belongs to.
It is plausible that you might have the same number of samples for each category, so the information about category could be implicit.
If you had 7000 categories and 500 samples per category, then you would normally have more than one reading per sample --- 7000 by 500 x N, or (7000 * 500) x N . MATLAB would need either the (7000 * 500) x N form or the N x (7000*500) form [depending which neural network you are using; some use the data transpose of the other.) And for the class information you would take
reshape(repmat(categorical(1:7000), 500, 1),[],1) %OR
reshape(repmat(categorical(1:7000), 1, 500),[],1)
depending on how the data was arranged.
Is it correct that you only have one reading for each sample? That would be rather uncommon for deep learning, since with only one reading you cannot do much more than population statistics.
It would be much more common if it was something like 500 different readings per sample, and (for example) 7 classes with 1000 samples each.
Kuno Bruswachtl
on 10 Mar 2022
Walter Roberson
on 10 Mar 2022
You would create either a 500 x 7000 or 7000 x 500 array (different toolbox functions need different ways.)
You would also want a vector of length 7000 indicating which class each sample belongs to. Either you already have that information somewhere, or else it is implicit, such as knowing that (for example) you might have 7 classes with 1000 samples each. You could then use
C = reshape(repmat(categorical(1:NumberOfCategories), 7000/NumberOfCategories, 1),[],1) %OR
C = reshape(repmat(categorical(1:NumberOfCategories), 1, 7000/NumberOfCategories),[],1)
depending how the data is arranged.
Kuno Bruswachtl
on 10 Mar 2022
Answers (0)
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!