Error using svmclassify The number of columns in TEST and training data must be equal. Error in multisvm classes = svmclassif​y(svmStruc​t,tst);

1 回表示 (過去 30 日間)
when i try to classify i got the above error in multisvm this is the code that am using
function [itrfin] = multisvm( T,C,test )
itrind=size(test,1);
itrfin=[];
Cb=C;
Tb=T;
for tempind=1:itrind
tst=test(tempind,:);
C=Cb;
T=Tb;
u=unique(C);
N=length(u);
c4=[];
c3=[];
j=1;
k=1;
if(N>2)
itr=1;
classes=0;
cond=max(C)-min(C);
while((classes~=1)&&(itr<=length(u))&& size(C,2)>1 && cond>0)
c1=(C==u(itr));
newClass=c1;
svmStruct = svmtrain(T,newClass);
classes = svmclassify(svmStruct,tst);
for i=1:size(newClass,2)
if newClass(1,i)==0;
c3(k,:)=T(i,:);
k=k+1;
end
end
T=c3;
c3=[];
k=1;
for i=1:size(newClass,2)
if newClass(1,i)==0;
c4(1,j)=C(1,i);
j=j+1;
end
end
C=c4;
c4=[];
j=1;
cond=max(C)-min(C);
if classes~=1
itr=itr+1;
end
end
end
valt=Cb==u(itr);
val=unique(val);
itrfin(tempind,:)=val;
end
end

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 3 月 11 日
tst=test(tempind,:)
has multiple columns
itr=1;
[...]
c1=(C==u(itr));
newClass=c1;
itr is a scalar, so u(itr) is a scalar, so c1 is a scalar so newClass is a scalar
svmStruct = svmtrain(T,newClass);
You are training on T as if everything is in the same class. If you get different results for newClass = false versus newClass = true then out would be due to round-off error in the calculations. svm does not care what the one class is labeled with; it only cares that you are telling it that all of the data is in the same class.
"The number of columns in TEST and training data must be equal."
size(T,2) is not the same as size(test,2) . You did not happen to show us the sizes.
Be careful with whether your rows represent different attributes within one observation, or whether your rows represent the same attribute for each different observation. Different classification routines have different rules about whether they expect row-oriented or column-oriented values.
  2 件のコメント
Melaku Eneayehu
Melaku Eneayehu 2018 年 3 月 12 日
This is the code that am trying to classify i used GLCM feature extraction my train_feat is 157*8 and label is 157*1 vector matrix, what happen even when i try the training image for testing always result return 1 and i gt the same result benign or the result in class 1 this is the code i used for testing, why result always return 1? the classification code is the above(multisvm)
function browseButtonPushed(app, event)
% global im im2
[path,user_cance]=imgetfile();
app.UIFigure.Visible = 'off';
app.UIFigure.Visible = 'on';
if user_cance
msgbox(sprintf('error'),'error','error');
return;
end
im=imread(path);
im2=im;
imshow(im2,'Parent',app.UIAxes)
[rows, columns, numberOfColorChannels] = size(im2);
if numberOfColorChannels >1
% % It's not really gray scale like we expected - it's color.
% % Convert it to gray scale by taking only the green channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
uiwait(msgbox('Image is Not GrayScale Conversion is needed','Attention','warn'));
else
uiwait(msgbox('Image is GrayScale Conversion is not needed','Attention','modal'));
end
end
function classifyButtonPushed(app, event)
img=getimage(app.UIAxes);
glcms = graycomatrix(img);
% Derive Statistics from GLCM
stats = graycoprops(glcms,'Contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
Mean = mean2(img);
Standard_Deviation = std2(img);
Entropy = entropy(img);
RMS = mean2(rms(img));
%Skewness = skewness(img)
feat_disease = [Contrast,Correlation,Energy,Homogeneity, Mean, Standard_Deviation, Entropy, RMS];
test= feat_disease;
% Load All The Features
load('Training_Data.mat');
% Put the test features into variable 'test'
result = multisvm(Train_Feat,Train_Label,test);
%disp(result);
% Visualize Results
if result == 1
helpdlg(' Begnin ');
disp(' Begnin ');
elseif result == 2
helpdlg('Normal ');
disp('Normal');
elseif result == 0
helpdlg(' Malignat ');
disp(' Malignat ');
end
end
Melaku Eneayehu
Melaku Eneayehu 2018 年 3 月 12 日
and here is the code am using to prepare Train_Feat feature matrix but i prepared the Train_Lable manually through excel because i know the label of each image first 54 class 0 second 49 for class 1 last 54 image class 2 totally 157 rows (always result is return class 1)and converted to .mat file.
clc
close all
clear all
setDir = fullfile('./Training');
imgSets = imageSet(setDir, 'recursive');
h = waitbar(0,'Please wait...');
ki=1;
for i=1:length(imgSets)
for j=1:imgSets(i).Count
img=imresize(imread(cell2mat(imgSets(i).ImageLocation(j))),[512 512]);
[a b c]=size(img);
if c==3
img=rgb2gray(img);
end
feat=graycoprops(img,{'contrast','Correlation','Energy','homogeneity'});
Mean = mean2(img);
Standard_Deviation = std2(img);
Entropy = entropy(img);
RMS = mean2(rms(img));
feature=[feat.Contrast feat.Correlation feat.Energy feat.Homogeneity Mean Standard_Deviation Entropy RMS];
dataset(ki, :) = [feature ki];
ki=ki+1
waitbar(ki/150)
end
end
close(h);
save('Training_Feat.mat','dataset')

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


thejas A V
thejas A V 2019 年 3 月 28 日
use equal number of dataset and corresponding label pls refer to attachment

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by