Relational comparisons are not allowed for categorical arrays that are not ordinal

10 ビュー (過去 30 日間)
The error is due to the difference in the type of the array. How can I change the type of the array so that the code works.Look at the workspace
Error using categorical/max (line 63)
Relational comparisons are not allowed for categorical arrays that are not ordinal.
Error in recall (line 4)
K = max(actual_label); %number of classes
Error in weightedVoting (line 14)
rec = recall(predict_label, Group_Test1); prec = precision(predict_label, Group_Test1);
%weightedVoting
load Group_Test
load predict_label
load predict_label2
%WEIGHTEDVOTING is a proposed ensemble method
k = max(Group_Test1); % determining number of classes [1-K]
heus = [];
rec = recall(predict_label, Group_Test1); prec = precision(predict_label, Group_Test1);
heus = [heus; 2*f1(prec,rec)-FPR(predict_label, Group_Test1)];
rec = recall(predict_label2, Group_Test1); prec = precision(predict_label2, Group_Test1);
heus = [heus; 2*f1(prec,rec)-FPR(predict_label2, Group_Test1)];
res = zeros(length(Group_Test1),k);
for i=1:length(Group_Test1)
res(i,predict_label(i)) = res(i,predict_label(i)) + heus(1);
res(i,predict_label2(i)) = res(i,predict_label2(i)) + heus(2);
end
[~, preds] = max(res,[],2);
%label.m
setup_folders;
sadtrdinfo = dir(sad1train); % Returns both folders and files
% sad = dir(pwd); % Returns both folders and files
sorted_cell_array_of_train_folder_names = setdiff({sadtrdinfo([sadtrdinfo.isdir]).name},{'..','.'}); % Select folder names
% cell_array_of_train_folder_names( strncmp( cell_array_of_train_folder_names, ".", 1 ) ) = []; % Remove '.' and '..'
%sorted_cell_array_of_train_folder_names = sort_nat( cell_array_of_train_folder_names );
% sorted_cell_array_of_train_folder_names = cell_array_of_train_folder_names; % if you don't have sort_nat
whos sorted_cell_array_of_train_folder_names
%----------------
np = numel(sorted_cell_array_of_train_folder_names); % number of subfolders
train_cats = categorical(sorted_cell_array_of_train_folder_names);
Group_Train1 = cell(np,1);
for i = 1 : np
SFN = sorted_cell_array_of_train_folder_names{i};% extract his name
traintifpattern = fullfile(sad1train, SFN, tiffwild);
tifListdinfo = dir(traintifpattern); % list all jpg files
tifList = fullfile({tifListdinfo.folder}, {tifListdinfo.name});
ms1 = numel(tifList); % ms = number of image files found
Group_Train1{i} = repmat(train_cats(i), ms1, 1);
end
Group_Train1 = vertcat(Group_Train1{:});
save('Group_Train','Group_Train1');
%---------------------
sadtedinfo = dir(sad1test); % Returns both folders and files
sorted_cell_array_of_test_folder_names = setdiff({sadtedinfo([sadtedinfo.isdir]).name},{'..','.'}); % Select folder names
whos sorted_cell_array_of_test_folder_names
%----------------
np = numel(sorted_cell_array_of_test_folder_names); %number of subfolders
test_cats = categorical(sorted_cell_array_of_test_folder_names);
Group_Test1 = cell(np, 1);
for i = 1 : np
SFN = sorted_cell_array_of_test_folder_names{i};% extract his name
testtifpattern = fullfile(sad1test, SFN, tiffwild);
tifListdinfo = dir(testtifpattern); % list all jpg files
tifList = fullfile({tifListdinfo.folder}, {tifListdinfo.name});
ms1 = numel(tifList); % ms = number of image files found
Group_Test1{i} = repmat(test_cats(i), ms1, 1);
end
Group_Test1 = vertcat(Group_Test1{:});
save('Group_Test','Group_Test1');
%HOG.M
setup_folders;
Folder = sad1train;
FileList = dir(fullfile(Folder, '**', tiffwild));
Feature = cell(1, numel(FileList)); % Pre-allocation
for iFile = 1:numel(FileList)
File = fullfile(FileList(iFile).folder, FileList(iFile).name);
Img = imread(File);
Img = imresize(Img, [128, 128]);
Feature{iFile} = extractHOGFeatures(Img,'CellSize',[2 2],'BlockSize',[2 2]);
end
% Maybe:
Feat1 = cat(1, Feature{:}); % Or cat(2, ...) ?!
clear Feature
save('featurs_T.mat', '-v7.3', 'Feat1');
%clear Feat1
Folder2 = sad1test;
FileList2 = dir(fullfile(Folder2, '**', tiffwild));
Feature2 = cell(1, numel(FileList2)); % Pre-allocation
for iFile2 = 1:numel(FileList2)
File2 = fullfile(FileList2(iFile2).folder, FileList2(iFile2).name);
Img2 = imread(File2);
%Img2 = imresize(Img2, [128, 128]);
Img2 = imresize(Img2, [128, 128]);
%Feature2{iFile2} = hog_feature_vector(Img2);
Feature2{iFile2} = extractHOGFeatures(Img2,'CellSize',[2 2],'BlockSize',[2 2]);
end
% Maybe:
Feat2 = cat(1, Feature2{:}); % Or cat(2, ...) ?!
clear Feature2
save('featurs_S.mat', '-v7.3', 'Feat2');
%clear Feat2
%maltisvm
function [preds] = multisvm(TrainingSet,Group_Train1,TestSet,Group_Test1)
%Models a given training set with a corresponding group vector and
%classifies a given test set using an SVM classifier according to a
%one vs. all relation.
%
%This code was written by Cody Neuburger cneuburg@fau.edu
%Florida Atlantic University, Florida USA...
%This code was adapted and cleaned from Anand Mishra's multisvm function
%found at http://www.mathworks.com/matlabcentral/fileexchange/33170-multi-class-support-vector-machine/
u=unique(Group_Train1);
numClasses=length(u);
%preds = zeros(length(TestSet(:,1)),1);
%build models
preds = categorical.empty();
for k=1:numClasses
%Vectorized statement that binarizes Group
%where 1 is the current class and 0 is all other classes
G1vAll=(Group_Train1==u(k));
models{k} = fitcsvm(TrainingSet,G1vAll,'KernelFunction','polynomial','polynomialorder',3,'Solver','ISDA','Verbose',0,'Standardize',true);
if ~models{k}.ConvergenceInfo.Converged
fprintf('Training did not converge for class "%s"\n', string(u(k)));
end
end
%classify test cases
for t=1:size(TestSet,1)
matched = false;
for k = numClasses:-1:1
% for k =1: numClasses
if(predict(models{k},TestSet(t,: )))
matched = true;
break;
end
end
if matched
preds(t,1) = u(k);
%result(t) = u(k);
else
preds(t,1) = 'No Match';
%--------------------------------
end
end
Accuracy = mean(Group_Test1==preds) * 100;
fprintf('Accuracy = %.2f\n', Accuracy);
fprintf('error rate = %.2f\n ', mean(preds ~= Group_Test1 ) * 100);
%confusionchart(result,Group_Test1);
end
%maltisvmcall
load featurs_T
load featurs_S
load Group_Train
load Group_Test
fprintf("HOG_SVM:\n")
HOG_SVM= multisvm(Feat1,Group_Train1,Feat2,Group_Test1);
predict_label= HOG_SVM;
save('predict_label.mat', 'predict_label');
  3 件のコメント
sun rise
sun rise 2022 年 6 月 1 日
I am a newbie to programming and want to change the code so that the arrays are of a type that works with weightedVoting.m I would be very grateful for your help. I've tried to change it but I get a heap of errors in the rest of the files like maltisvm.m , HOG_New.m
dpb
dpb 2022 年 6 月 1 日
Well, we don't have the data and you've not helped any by
Error in weightedVoting (line 14)
rec = recall(predict_label, Group_Test1); prec = precision(predict_label, Group_Test1);
has two code lines on one line so we don't eeven know which of the two is the cause...and
>> which -all recall
'recall' not found.
>>
shows recall isn't a MATLAB-supplied base function nor is it defined in the code supplied..
Start with
rec = recall(predict_label, Group_Test1);
prec = precision(predict_label, Group_Test1);
and re-run to at least see which is which.
And, whatever variable it is you're trying to turn into categorical we don't know nor how you tried that -- nothing of that sort shows up in the code you've posted.
If you don't understand the code, since it is from another source and shows very few comments/help, I'd suggest starting at the location from which you got the code and see about user community/support there.
This is really more about using another function that just happens to be written in MATLAB than about MATLAB itself.

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

採用された回答

Walter Roberson
Walter Roberson 2022 年 6 月 1 日
Without digging into the code itself, I can talk about the reason this happens.
Some clarification prediction methods take an input and return a vector of probability that the input belongs to each class. Determining the class is then typically a matter of taking max() of the probability vector and looking at the index of the maximum and assigning to that class.
But other classification prediction methods directly return the predicted class instead of the probability vector.
Generally speaking, when you do classification training feeding in categorical labels, then the prediction method is more likely to return a categorical class label instead of a vector of probabilities. And of course, taking max() of a list of categoricals usually does not work very well. What is the max() of Apple and Ball?
You need to go back through the code and examine whether prediction is returning a class label (no max needed) or probability (max needed.)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Analytics Toolbox についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by