how to convert categorical data (76*1) of labels into target data of 3 classes (76*3) for nrptool?

3 visualizaciones (últimos 30 días)
I have assigned labels to my database like "traindatabase.Labels" which gives 76*1 categorical data (metal pipe-19, plastic box-28, steel box-29). I need to classify into 3 classes and my target data should be 76*3 which is in the form of
1 1 1....1(19) 0 0 0... (remaining)
0 0 0..0(19) 1 1 1...1(28) 0 0...0(remaining)
0 0 0......0(47) 1 1 1...1(29)
how to make target like this? please help me
path1='D:\matprog\matfiles2\trainfiles'; % training path
path2='D:\matprog\matfiles2\testfiles'; % testing path
traindb=imageDatastore(path1,'IncludeSubfolders',true,'LabelSource','foldernames');
testdb=imageDatastore(path2,'IncludeSubfolders',true,'LabelSource','foldernames');
%% Training
img=readimage(traindb,1);
[pixelCounts GLs] = imhist(img); % GL-gray levels
% Get the number of pixels in the histogram.
numberOfPixels = sum(pixelCounts);
% Get the mean gray lavel.
meanGL = sum(GLs .* pixelCounts) / numberOfPixels
% Get the variance, which is the second central moment.
varianceGL = sum((GLs - meanGL) .^ 2 .* pixelCounts) / (numberOfPixels-1)
% Get the standard deviation.
sd = sqrt(varianceGL);
% Get the skew.
skew = sum((GLs - meanGL) .^ 3 .* pixelCounts) / ((numberOfPixels - 1) * sd^3)
% Get the kurtosis.
kurtosis = sum((GLs - meanGL) .^ 4 .* pixelCounts) / ((numberOfPixels - 1) * sd^4)
% Get the entropy.
ent = entropy(img)
FeatureVector = [meanGL varianceGL sd skew kurtosis ent];
featuresize=length(FeatureVector)
totaltrainimages=numel(traindb.Files);
trainingfeatures=zeros(totaltrainimages,featuresize,'single');
for i=1:totaltrainimages % train all the images in training folder
img=readimage(traindb,i);
[pixelCounts GLs] = imhist(img); % GL-gray levels
% Get the number of pixels in the histogram.
numberOfPixels = sum(pixelCounts);
% Get the mean gray lavel.
meanGL = sum(GLs .* pixelCounts) / numberOfPixels;
% Get the variance, which is the second central moment.
varianceGL = sum((GLs - meanGL) .^ 2 .* pixelCounts) / (numberOfPixels-1);
% Get the standard deviation.
sd = sqrt(varianceGL);
% Get the skew.
skew = sum((GLs - meanGL) .^ 3 .* pixelCounts) / ((numberOfPixels - 1) * sd^3);
% Get the kurtosis.
kurtosis = sum((GLs - meanGL) .^ 4 .* pixelCounts) / ((numberOfPixels - 1) * sd^4);
% Get the entropy.
ent = entropy(img);
FeatureVector = [meanGL varianceGL sd skew kurtosis ent];
featuresize=length(FeatureVector);
trainingfeatures(i, 1 : featuresize) = FeatureVector;
end
traininglabels=traindb.Labels; % assign labels for training

Respuestas (1)

Vinayak
Vinayak el 15 de Feb. de 2024
Hi Santhosh
As you mentioned the labels are a categorical data of 76x1. You can easily convert them into double matrix(target matrix) using the `grp2idx` function from the Statistics and Machine Learning Toolbox.
It can also work with any custom dataset:
% Create a random categorical dataset for testing
categories = {'metal pipe', 'plastic box', 'steel box'};
numSamples = 5; %Testing
labels = categorical(randi([1, 3], numSamples, 1), 1:3, categories);
% Convert categorical labels to numerical array
labelIndices = grp2idx(labels)
labelIndices = 5x1
1 2 2 3 2
% Generate one-hot encoded target matrix
targetMatrix = full(ind2vec(labelIndices'))'
targetMatrix = 5x3
1 0 0 0 1 0 0 1 0 0 0 1 0 1 0
Refer to the documentation on `grp2idx` and `ind2vec` for more information.

Categorías

Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by