我在这个小函数中工作,为随机森林火车准备索引。使用此函数,我获得了一组示例的索引和示例的一部分功能的索引。我发现代码有点慢,有更好更快的方法吗?
这是我的代码
function [idx_linhas, idx_features ] = prepararsementes(X,features,nseeds,treesize)
%Esta função prepara os index para a "semeadura" de uma random forest
idx_linhas = nan(nseeds,treesize);
idx_features= nan(nseeds,features);
for idx=1:nseeds
[~,idx_linhas(idx,:)] = datasample(X,treesize,'Replace',true);
end
for idx=1:nseeds
[~,idx_features(idx,:)] = datasample(X,features,2);
end
idx_linhas = idx_linhas.';
end
提前致谢!
答案 0 :(得分:3)
试试这个:
function [idx_linhas, idx_features] = prepararsementes(X, features, nseeds, treesize)
% instead of loop, call datasample() only once, and reshape
% note that ('replace', true) is the default, so I omitted that
[~,idx] = datasample(X, nseeds*treesize);
idx_linhas = reshape(idx, nseeds, treesize).';
[~,idx] = datasample(X, nseeds*features, 2);
idx_features = reshape(idx, nseeds, features);
end
从统计学的角度来说,我认为结果应该是相同的,因为你没有具体的权重,你在两种情况下都用替换。