フィルターのクリア

Matlab error when evaluating a neural network

2 ビュー (過去 30 日間)
Killian Flynn
Killian Flynn 2022 年 12 月 29 日
回答済み: KSSV 2023 年 1 月 2 日
Hellow I am having an error when I am trying to evaluate a neural network. The code is as follows:
% Load the sample data
load fisheriris
% Extract the first two features as the predictors and the third feature as the response
X = meas(:,1:2);
y = meas(:,3);
% Split the data into training and test sets
rng(1); % For reproducibility
cv = cvpartition(y,'Holdout',0.3);
XTrain = X(training(cv),:);
YTrain = y(training(cv));
XTest = X(test(cv),:);
YTest = y(test(cv));
% Create a decision tree using entropy as the splitting criterion
tree = fitctree(XTrain,YTrain,'SplitCriterion','gdi');
% Evaluate the decision tree on the test set
YPred = predict(tree,XTest);
treeAccuracy = mean(YPred == YTest);
fprintf('Decision tree accuracy: %.2f%%\n',treeAccuracy*100)
% Create a feedforward neural network
net = patternnet(10);
% Train the neural network
net = train(net,XTrain',YTrain');
% Evaluate the neural network on the test set
YPred = net(XTest')';
YPred = tree.ClassNames(YPred);
nnAccuracy = mean(YPred == YTest);
fprintf('Neural network accuracy: %.2f%%\n',nnAccuracy*100)
Error:
Array indices must be positive integers or logical values.
Error in classreg.learning.internal.DisallowVectorOps/subsref (line 22)
[varargout{1:nargout}] = builtin('subsref',this,s);
Error in TreeVSAnn (line 33)
YPred = tree.ClassNames(YPred);
Any help to fix this would be greatly appreciated.
  1 件のコメント
Vinayak
Vinayak 2023 年 1 月 2 日
Hi Killian,
I am able to execute this code snippet error-free.
Could you please re-check and confirm.
Thanks.

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

回答 (1 件)

KSSV
KSSV 2023 年 1 月 2 日
These lines:
YPred = net(XTest')';
YPred = tree.ClassNames(YPred);
nnAccuracy = mean(YPred == YTest);
fprintf('Neural network accuracy: %.2f%%\n',nnAccuracy*100)
Should be:
YPred = net(XTest')';
nnAccuracy = mean(YPred == YTest);
fprintf('Neural network accuracy: %.2f%%\n',nnAccuracy*100)
You need not to index tree.ClassNames. You got your prediction already from the line:
YPred = net(XTest')';
But this is not the way to caclulate Accuracy.

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by