how to implement KNN classification in simulink

2 次查看(过去 30 天)
How to implement KNN classification in simulink. I used fitcknn and predict functions in my script file,but the same cannot be used in matlab function block in simulink.
Also my class labels (response variable) are in the form of strings. i am not able pass string inputs in simulink.
for ex: suppose i train my KNN with following data
my predictor values are p=[1 2 3]
my response values are r=['one','two','three'];
my test input is 3
the commands i used in script file are:
mdl=fitcknn(p,r);
y=predict(mdl,3);
the answer after running the script was
y=three (expected as per training data)
The same result i want to simulate in simulink. Kindly suggest me the right approach.

回答(1 个)

Vandana Rajan
Vandana Rajan 2016-12-19
编辑:Vandana Rajan 2016-12-20
Hi Pavan,
1. Since Simulink does not support string inputs, you can convert class labels to digits. Like Class A can be represented by 1, Class B by 2 and so on.
2. 'fitcknn' and 'predict' functions are not supported for standalone code generation, you can use 'coder.extrinsic' declaration for extrinsic functions. The link below can give you more details about 'coder.extrinsic'
https://www.mathworks.com/help/simulink/slref/coder.extrinsic.html
3. Since 'fitcknn' outputs a model, which is an object of type 'ClassificationKNN' and not a numeric output, MATLAB function block will not support it. Hence you may go for using 'system objects' in a 'MATLAB system' block instead of MATLAB function block. Use 'interpreted execution' mode for the system block.
You may use the link below for more details on system block
https://in.mathworks.com/help/simulink/ug/what-is-matlab-system-block.html

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by