gpt4 book ai didi

matlab - Adaboost 的自定义学习器函数

转载 作者:太空宇宙 更新时间:2023-11-03 19:17:28 24 4
gpt4 key购买 nike

我正在使用 Adaboost 来解决分类问题。我们可以做到以下几点:

ens = fitensemble(X, Y, 'AdaBoostM1', 100, 'Tree')

现在“树”是学习器,我们可以将其更改为“判别式”或“KNN”。每个学习者都使用特定的模板对象创建函数。更多信息 here .

是否可以创建自己的函数并将其用作学习器?以及如何?

最佳答案

我打开templateTree.m和templateKNN.m,看看MATLAB是如何定义模板对象创建函数的。

function temp = templateKNN(varargin)
classreg.learning.FitTemplate.catchType(varargin{:});
temp = classreg.learning.FitTemplate.make('KNN','type','classification',varargin{:});
end

function temp = templateTree(varargin)
temp = classreg.learning.FitTemplate.make('Tree',varargin{:});
end

说明MATLAB在FitTemplate中有一个函数叫make,如果你打开这个m文件,你会看到:

   function temp = make(method,varargin)
% Check the type of the required argument
if ~ischar(method)
error(message('stats:classreg:learning:FitTemplate:make:BadArgs'));
end

% Extract type (classification or regression)
args = {'type'};
defs = { ''};
[usertype,~,modelArgs] = ...
internal.stats.parseArgs(args,defs,varargin{:});

% Check usertype
if ~isempty(usertype)
usertype = gettype(usertype);
end

% Method
namesclass = classreg.learning.classificationModels();
namesreg = classreg.learning.regressionModels();
[tfclass,locclass] = ismember(lower(method),lower(namesclass));
[tfreg,locreg] = ismember(lower(method),lower(namesreg));
if ~tfclass && ~tfreg
error(message('stats:classreg:learning:FitTemplate:make:UnknownMethod', method));
end
if tfclass && tfreg
method = namesclass{locclass}; % can get it from namesreg too
type = usertype;

% If type is not passed for an ensemble method, try to
% figure it out from learner types. This is useful for
% users who want to type
% fitensemble(X,Y,'Subspace',100,'Discriminant')
% instead of
% fitensemble(X,Y,'Subspace',100,'Discriminant','type','classification')
if isempty(type) && ismember(method,classreg.learning.ensembleModels())
[learners,~,~] = internal.stats.parseArgs({'learners'},{},modelArgs{:});
if ischar(learners) || isa(learners,'classreg.learning.FitTemplate')
learners = {learners};
elseif ~iscell(learners)
error(message('stats:classreg:learning:FitTemplate:make:BadLearnerTemplates'));
end
L = numel(learners);
% The user can pass several learner templates, and some
% of these learners may be appropriate for
% classification, some for regression, and some for
% both. The ensemble type cannot be determined
% unambiguously unless if all learners are appropriate
% for one type of learning *only*. For example, in 12a
% t1 = ClassificationDiscriminant.template
% t2 = ClassificationKNN.template
% fitensemble(X,Y,'Subspace',10,{t1 t2})
% is going to work because both discriminant and k-NN
% can be used for classification only. If you want to
% mix discriminant and tree, you have to specify the
% ensemble type explicitly:
% t1 = ClassificationDiscriminant.template
% t2 = ClassificationTree.template
% fitensemble(X,Y,'Bag',10,{t1 t2},'type','classification')
types = zeros(L,1); % -1 for regression and 1 for classification
for l=1:L
meth = learners{l};
if isa(meth,'classreg.learning.FitTemplate')
meth = meth.Method;
end
isc = ismember(lower(meth),lower(namesclass));
isr = ismember(lower(meth),lower(namesreg));
if ~isc && ~isr
error(message('stats:classreg:learning:FitTemplate:make:UnknownMethod', meth));
end
types(l) = isc - isr;
end
if all(types==1)
type = 'classification';
elseif all(types==-1)
type = 'regression';
end
end
elseif tfclass
method = namesclass{locclass};
type = 'classification';
else
method = namesreg{locreg};
type = 'regression';
end

% Make sure the type is consistent
if ~isempty(usertype) && ~strcmp(usertype,type)
error(message('stats:classreg:learning:FitTemplate:make:UserTypeMismatch', method, usertype));
end

% Make template
temp = classreg.learning.FitTemplate(method,modelArgs);
temp = fillIfNeeded(temp,type);
end

您必须更改此功能。

关于matlab - Adaboost 的自定义学习器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45936175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com