Loop machine learning algorithm functions

1 次查看(过去 30 天)
I want to create a loop where multiple different machine learning models are created with the same input and output.
Lets say the input is:
x = [1 1 1 1 2 2 2 2 3 3 3 3]
and the output is:
y = categorical(["dog" "dog" "dog" "dog" "cat" "cat" "cat" "cat" "bird" "bird" "bird" "bird"])
I want to fit a KNN, SVM and a Decision Tree classifier. Instead of specifying:
A1 = fitcknn(x,y)
A2 = fitcecoc(x,y)
A3 = fitctree(x,y)
Instead of the above I want to create a loop going over the functions {fitcknn fitcecoc fitctree}
I tried something like this:
for classifier = [fitcknn fitcecoc fitctree]
A = classifier(x,y)
end

采纳的回答

Voss
Voss 2021-12-17
funcs = {@fitcknn @fitcecoc @fitctree};
n_funcs_given = numel(funcs);
A = cell(1,n_funcs_given);
for i = 1:n_funcs_given
A{i} = funcs{i}(x,y);
end
  1 个评论
yanqi liu
yanqi liu 2021-12-30
yes,sir,it is great method
clc; clear all; close all;
x = [1 1 1 1 2 2 2 2 3 3 3 3];
y = categorical(["dog" "dog" "dog" "dog" "cat" "cat" "cat" "cat" "bird" "bird" "bird" "bird"]);
funcs = {@fitcknn @fitcecoc @fitctree};
n_funcs_given = numel(funcs);
A = cell(1,n_funcs_given);
for i = 1:n_funcs_given
A{i} = funcs{i}(x(:),y(:));
end
A
A = 1×3 cell array
{1×1 ClassificationKNN} {1×1 ClassificationECOC} {1×1 ClassificationTree}

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by