Adaboost 的自定义学习器函数
Posted
技术标签:
【中文标题】Adaboost 的自定义学习器函数【英文标题】:Custom learner function for Adaboost 【发布时间】:2018-02-06 17:41:40 【问题描述】:我正在使用 Adaboost 来拟合分类问题。我们可以做到以下几点:
ens = fitensemble(X, Y, 'AdaBoostM1', 100, 'Tree')
现在“树”是学习者,我们可以将其更改为“判别”或“KNN”。每个学习者使用特定的Template Object Creation Function
。更多信息here。
是否可以创建自己的函数并将其用作学习器?怎么做?
【问题讨论】:
不容易。例如,考虑您的树学习器,您需要一个templateTree()
和一个 ClassificationTree
类,您都可以在 %matlabroot%/toolbox/stats/clas-s-reg
中找到它们。您可以对clas-s-reg.learning.FitTemplate.make()
所需的通用 API 进行逆向工程,但我认为这是一个为期一周的项目。说了这么多,你有想要实现的特定学习器吗?
它确实是一个自定义函数,而不是文学的通用学习器。
【参考方案1】:
我打开 templateTree.m 和 templateKNN.m 看看 MATLAB 是如何定义模板对象创建函数的。
function temp = templateKNN(varargin)
clas-s-reg.learning.FitTemplate.catchType(varargin:);
temp = clas-s-reg.learning.FitTemplate.make('KNN','type','classification',varargin:);
end
和
function temp = templateTree(varargin)
temp = clas-s-reg.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:clas-s-reg: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 = clas-s-reg.learning.classificationModels();
namesreg = clas-s-reg.learning.regressionModels();
[tfclass,locclass] = ismember(lower(method),lower(namesclass));
[tfreg,locreg] = ismember(lower(method),lower(namesreg));
if ~tfclass && ~tfreg
error(message('stats:clas-s-reg:learning:FitTemplate:make:UnknownMethod', method));
end
if tfclass && tfreg
method = namesclasslocclass; % 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,clas-s-reg.learning.ensembleModels())
[learners,~,~] = internal.stats.parseArgs('learners',,modelArgs:);
if ischar(learners) || isa(learners,'clas-s-reg.learning.FitTemplate')
learners = learners;
elseif ~iscell(learners)
error(message('stats:clas-s-reg: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 = learnersl;
if isa(meth,'clas-s-reg.learning.FitTemplate')
meth = meth.Method;
end
isc = ismember(lower(meth),lower(namesclass));
isr = ismember(lower(meth),lower(namesreg));
if ~isc && ~isr
error(message('stats:clas-s-reg: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 = namesclasslocclass;
type = 'classification';
else
method = namesreglocreg;
type = 'regression';
end
% Make sure the type is consistent
if ~isempty(usertype) && ~strcmp(usertype,type)
error(message('stats:clas-s-reg:learning:FitTemplate:make:UserTypeMismatch', method, usertype));
end
% Make template
temp = clas-s-reg.learning.FitTemplate(method,modelArgs);
temp = fillIfNeeded(temp,type);
end
您必须更改此功能。
【讨论】:
以上是关于Adaboost 的自定义学习器函数的主要内容,如果未能解决你的问题,请参考以下文章