Unable to run the code given in the link: https://in.mathworks.com/help/stats/code-generation-for-prediction-of-machine-learning-model-using-matlab-coder-app.html
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have been trying to run the code given the following link: https://in.mathworks.com/help/stats/code-generation-for-prediction-of-machine-learning-model-using-matlab-coder-app.html. Currently I am using Matlab2021a version. Do I need to install 2021b to run this code or am I missing something else?
15 个评论
Doli Hazarika
2022-3-2
While running the following line of code:
Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...
'Learners',learner,'NumLearningCycles',13);
it is showing an error stating:
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
syntax error. To construct matrices, use brackets instead of parentheses.
Walter Roberson
2022-3-2
There is a possibility that it is complaining about a previous line not being complete.
Doli Hazarika
2022-3-2
load ionosphere
rng('default') % For reproducibility
learner = templateKNN('NumNeighbors',2);
Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...
'Learners',learner,'NumLearningCycles',13);
this are the lines of code that I am running. It shows the following error:
File: fitcensemble.m Line: 1 Column: 33
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
syntax error. To construct matrices, use brackets instead of parentheses.
Error in Classification (line 4)
Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...
Walter Roberson
2022-3-2
编辑:Walter Roberson
2022-3-2
Please show
which -all fitcensemble
dbtype fitcensemble 1:5
Doli Hazarika
2022-3-2
>> which -all fitcensemble
E:\Doli\MATLAB codes\fitcensemble.m
C:\Program Files\MATLAB\R2021a\toolbox\stats\classreg\fitcensemble.m % Shadowed
>> dbtype fitcensemble 1:5
1 function Mdl = fitcensemble(X,Y,'Bag')
2 %UNTITLED Summary of this function goes here
3 % Detailed explanation goes here
4 rng('default')
5 t = templateTree('Reproducible', true);
Walter Roberson
2022-3-2
Your E:\Doli\MATLAB codes\fitcensemble.m is interfering with calling the Mathworks fitcensemble function.
function Mdl = fitcensemble(X,Y,'Bag')
That is not valid syntax in defining a function. If you want to force Bag as the third parameter then you would want to do something like
function Mdl = my_fitcensemble(X,Y,varargin)
Mdl = fitcensemble(X, Y, 'Bag', varargin{:});
Doli Hazarika
2022-3-4
I tried but it is still showing the following error. I am unable to understand from the error
Maximum recursion limit of 500 reached.
Error in fitcensemble (line 7)
Mdl = fitcensemble(X,Y,'Bag',varargin{:});
Caused by:
Maximum recursion limit of 500 reached.
Walter Roberson
2022-3-4
It does work.
%illustration that the code DOES work when saved to a file name that does
%not conflict
%first write the code into a .m file
PROGRAM = "load ionosphere\nrng('default') %% For reproducibility\nlearner = templateKNN('NumNeighbors',2);\nMdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...\n 'Learners',learner,'NumLearningCycles',13);";
fid = fopen('my_fitcensemble.m', 'w');
fprintf(fid, PROGRAM);
fclose(fid);
%now verify that the code looks like we expect
dbtype my_fitcensemble
1 load ionosphere
2 rng('default') % For reproducibility
3 learner = templateKNN('NumNeighbors',2);
4 Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...
5 'Learners',learner,'NumLearningCycles',13);
%now run the code
my_fitcensemble
%check the results
whos
Name Size Bytes Class Attributes
Description 5x79 790 char
Mdl 1x1 613095 classreg.learning.classif.ClassificationEnsemble
PROGRAM 1x1 562 string
X 351x34 95472 double
Y 351x1 37206 cell
ans 1x1 8 double
fid 1x1 8 double
learner 1x1 2078 classreg.learning.FitTemplate
Doli Hazarika
2022-3-7
Yes it worked this way. But instead of creating a function and calling it directly why are we doing it this way?
Walter Roberson
2022-3-7
I did it that way to prove that if you had a file with that content that it would work. All you need is the my_fitensemble file, not the code that creates it.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Programming Utilities 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
