资源共享分类
通信信号 图像语音 神经网络 智能优化 控制器 数值仿真 数学建模 其他专业

微信:HuangL1121
Q Q:1224848052
团队:嘉兴麦特莱博软件开发工作室
地址:嘉兴港区
 
通信信号
MATLAB代做|FPGA代做-CART算法的MATLAB源码
来源:本站    日期:2019/7/28    浏览量:163  

功能描述:

联系:highspeedlogic

QQ :1224848052

微信:HuangL1121

邮箱:1224848052@qq.com

网站:http://www.mat7lab.com/

网站:http://www.hslogic.com/

微信扫一扫:

function test_targets = CART(train_patterns, train_targets, test_patterns, params)

% Classify using classification and regression trees
% Inputs:
% training_patterns   - Train patterns
% training_targets - Train targets
%   test_patterns       - Test  patterns
% params         - [Impurity type, Percentage of incorrectly assigned samples at a node]
%                         Impurity can be: Entropy, Variance (or Gini), or Misclassification
%
% Outputs
% test_targets        - Predicted targets


[Ni, M]    = size(train_patterns);

%Get parameters
split_type = params.split_type;
inc_node = params.inc_node;
%[split_type, inc_node] = process_params(params);

%Build the tree recursively
disp('Building tree')
tree            = make_tree(train_patterns, train_targets, M, split_type, inc_node);

%Classify test patterns according to the tree
disp('Classify test patterns using the tree')
test_targets    = use_tree(test_patterns, 1:size(test_patterns,2), tree);

%END

function targets = use_tree(patterns, indices, tree)
%Classify recursively using a tree

if isnumeric(tree.Raction)
   %Reached an end node
   targets = zeros(1,size(patterns,2));
   targets(indices) = tree.Raction(1);
else
   %Reached a branching, so:
   %Find who goes where
   in_right    = indices(find(eval(tree.Raction)));
   in_left     = indices(find(eval(tree.Laction)));
   
   Ltargets = use_tree(patterns, in_left, tree.left);
   Rtargets = use_tree(patterns, in_right, tree.right);
   
   targets = Ltargets + Rtargets;
end
%END use_tree 

function tree = make_tree(patterns, targets, Dlength, split_type, inc_node)
%Build a tree recursively

if (length(unique(targets)) == 1),
   %There is only one type of targets, and this generates a warning, so deal with it separately
   tree.right      = [];
   tree.left       = [];
   tree.Raction    = targets(1);
   tree.Laction    = targets(1);
   return
end

[Ni, M] = size(patterns);
Nt      = unique(targets);
N       = hist(targets, Nt);

if ((sum(N < Dlength*inc_node) == length(Nt) - 1) | (M == 1)), 
   %No further splitting is neccessary
   tree.right      = [];
   tree.left       = [];
   if (length(Nt) ~= 1),
      MLlabel   = find(N == max(N));
   else
      MLlabel   = 1;
   end
   tree.Raction    = Nt(MLlabel);
   tree.Laction    = Nt(MLlabel);
   
else
   %Split the node according to the splitting criterion  
   deltaI = zeros(1,Ni);
   split_point = zeros(1,Ni);
   op = optimset('Display', 'off');   
   for i = 1:Ni,
      split_point(i) = fminbnd('CARTfunctions', min(patterns(i,:)), max(patterns(i,:)), op, patterns, targets, i, split_type);
      I(i) = feval('CARTfunctions', split_point(i), patterns, targets, i, split_type);
   end
   
   [m, dim] = min(I);
   loc = split_point(dim);
    
   %So, the split is to be on dimention 'dim' at location 'loc'
   indices = 1:M;
   tree.Raction= ['patterns(' num2str(dim) ',indices) >  ' num2str(loc)];
   tree.Laction= ['patterns(' num2str(dim) ',indices) <= ' num2str(loc)];
   in_right    = find(eval(tree.Raction));
   in_left     = find(eval(tree.Laction));
   
   if isempty(in_right) | isempty(in_left)
      %No possible split found
   tree.right      = [];
    tree.left       = [];
   if (length(Nt) ~= 1),
       MLlabel  = find(N == max(N));
   else
       MLlabel = 1;
   end
    tree.Raction    = Nt(MLlabel);
    tree.Laction    = Nt(MLlabel);
   else
   %...It's possible to build new nodes
    tree.right = make_tree(patterns(:,in_right), targets(in_right), Dlength, split_type, inc_node);
    tree.left  = make_tree(patterns(:,in_left), targets(in_left), Dlength, split_type, inc_node);      
   end
   
end

   上一篇: MATLAB代做|FPGA代做-EM最大期望参数估计算法的MATLAB仿真源码    下一篇:    
   相关阅读
· MATLAB代做|FPGA代做|simulink代做—— 2020/7/14
· MATLAB代做|FPGA代做|simulink代做—— 2020/5/30
· MATLAB代做|FPGA代做|simulink代做—— 2020/5/30
· MATLAB代做|FPGA代做|simulink代做—— 2020/2/1
· MATLAB代做|FPGA代做|simulink代做—— 2019/12/11
· MATLAB代做|FPGA代做|simulink代做—— 2019/12/11
· MATLAB代做|FPGA代做|simulink代做—— 2019/12/11
· Alexnet网络结构 2019/11/20
· MATLAB代做|FPGA代做|simulink代做—— 2019/9/15
· MATLAB代做|FPGA代做|simulink代做—— 2019/8/29
Copyright 2017-2024 © 嘉兴麦特莱博软件开发工作室
  • 网站备案号:浙ICP备18008591号-1