make categorical array out of 4D array

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 件のコメント

Walter Roberson
Walter Roberson 2022 年 3 月 4 日
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
Kuno Bruswachtl 2022 年 3 月 7 日
well first of all thank you, for your reply.
I have to be honest; I don´t understand much more now.
My information is in of these columns. Basically every observation of the 7000, is a category, where 500 is the size of it. My main problem is still, that I do not know how to transfer the 7000 observations to a category array, so I am able to use the cross-entropy solver. Would you be able to help me out? thank you soo much, again
Walter Roberson
Walter Roberson 2022 年 3 月 7 日
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
Kuno Bruswachtl 2022 年 3 月 10 日
ok first of all I have to thank you again for you taking your time to answer my question.
Sorry for the late reply...
I think I used the wrong vocabulary in the whole context with sample and stuff. So basically 500 is the size of a Matrix(500x1), form which I have 7000 observations. So I have 7000 different 500x1 matrices and I want to put them into categorical array, to be able to use the crossentropy solver... Can I now still use the way you described?
Thank you so much for your help
Walter Roberson
Walter Roberson 2022 年 3 月 10 日
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
Kuno Bruswachtl 2022 年 3 月 10 日
I think I got it now!
I really have to thank you Walter for your help and time. It means a lot to me, you made my day!

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

リリース

R2021a

質問済み:

2022 年 3 月 4 日

コメント済み:

2022 年 3 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by