Error with reshape of number of elements

조회 수: 1 (최근 30일)
Fed
Fed 2019년 1월 25일
댓글: Fed 2019년 2월 1일
Hello,
I'm working with multi-class classification by using function fitcecoc.
Featureste is a 1063x22 matrix while Wte is a 1063x1 vector.
Running the code shown below, a message error is given, saying: 'To RESHAPE the number of elements must not change'
Error is given at the following line:
reshape(max(PosteriorRegion,[ ],2),size(x1Grid,1),size(x1Grid,2)))
How can I fix it? Thank you in advance
Mdl = fitcecoc(Featureste,Wte,'Learners',t,'FitPosterior',1,...
'ClassNames',{'1','2','3'},...
'Verbose',2)
%Cross-validate Mdl using 10-fold cross-validation.
CVMdl = crossval(Mdl);
%CVMdl is a ClassificationPartitionedECOC cross-validated ECOC classifier.
%Estimate the classification error.
loss = kfoldLoss(CVMdl);
%Define a grid of values in the observed predictor space.
%Predict the posterior probabilities for each instance in the grid.
xMax = max(Featureste);
xMin = min(Featureste);
x1Pts = linspace(xMin(1),xMax(1));
x2Pts = linspace(xMin(2),xMax(2));
[x1Grid,x2Grid] = meshgrid(x1Pts,x2Pts);
[~,~,~,PosteriorRegion] = predict(Mdl,[x1Grid(:),x2Grid(:)]);
%For each coordinate on the grid
%plot the maximum class posterior probability among all classes.
figure
contourf(x1Grid,x2Grid,...
reshape(max(PosteriorRegion,[],2),size(x1Grid,1),size(x1Grid,2)))
hold on
gh = gscatter(Featureste(:,1),Featureste(:,2),Wte,'krk','*xd',8)
gh(2).LineWidth = 2;
gh(3).LineWidth = 2;
hold off

채택된 답변

Cris LaPierre
Cris LaPierre 2019년 1월 25일
Here's my guess. You create x1Pts and x2Pts without specifying the number of elements you want. When you do that, you get a vector with length 100. See this comment from the documentation page:
  • y = linspace(x1,x2) returns a row vector of 100 evenly spaced points between x1 and x2.
Therefore, x1Grid and x2Grid and 100x100.
I have no idea what the size of PosteriorRange is, but max(PosteriorRegion,[],2) must NOT be a 10000 x 1 (size(x1Grid,1) = size(x1Grid,2) = 100).
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2019년 1월 28일
A countour plot must be given a matrix for input Z. You can't create a contour with a vector. Without knowing anything about your data, I can't tell you how you should reshape your data to see what you want to see.
Consider adding a row of NaN to make your vector 1064 so you can reshape it to a matrix.
Fed
Fed 2019년 2월 1일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Support Vector Machine Classification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by