matlab 决策树 初学
clear all
clc
load('iris_data.mat'); %load the analyse data
a=randperm(150); %random permutation the numbers
Train_respond = classes( a(1:120) , : );
Train_variable = features( a(1:120) , : );
Test_respond = classes( a(121:end) , : );
Test_variable = features( a(121:end) , : );
ctree = fitctree(Train_variable ,Train_respond);
view(ctree,'Mode','graph')
predict_result=predict(ctree,Test_variable);
numTrain_A=length(find(Train_respond==1));
numTrain_B=length(find(Train_respond==2));
numTrain_C=length(find(Train_respond==3));
numTest_A=length(find(Test_respond==1));
numTest_B=length(find(Test_respond==2));
numTest_C=length(find(Test_respond==3));
numTotal_A=length(find(Train_respond==1));
numTotal_A=length(find(Train_respond==1));
numTotal_A=length(find(Train_respond==1));
right_A=length(find(Test_respond==1&predict_result==1));
right_B=length(find(Test_respond==2&predict_result==2));
right_C=length(find(Test_respond==3&predict_result==3));
ratio_A=right_A/numTest_A;
ratio_B=right_B/numTest_B;
ratio_C=right_C/numTest_C;
ratio_total=sum([right_A,right_B,right_C])/length(Test_respond);
disp(['the correct ratio of A is: ',num2str(ratio_A) ])
disp(['the correct ratio of B is: ',num2str(ratio_B) ])
disp(['the correct ratio of C is: ',num2str(ratio_C) ])
disp(['the total correct ratio is: ',num2str(ratio_total)])
leafs=linspace( 1,60,60);
N=numel(leafs);%get elements number of the array
err=zeros(N,1);
for n=1:N
test_tree=fitctree(Train_variable,Train_respond,'MinLeafSize',leafs(n),'CrossVal','on');
err(n)=kfoldLoss(test_tree);
end
plot(leafs,err);
xlabel('the minleaf of the tree');
ylabel('the crossloss of the tree');[min_err,min_err_num]=min(err);
min_err_leaf=leafs(min_err_num);
disp(['the min error leaf number is: ',num2str(min_err_leaf)])
Optimized_tree=fitctree(Train_variable,Train_respond,'MinLeafSize',min_err_leaf);%genenrae the optimized tree
view(Optimized_tree,'Mode','graph')
m = max(ctree.PruneList) - 1;
[~,~,~,BestLevel] = cvloss(ctree,'SubTrees','all' );
ctreePrune = prune(ctree,'Level',BestLevel);
view(ctreePrune,'Mode','graph')
resubOriginal = resubLoss(ctree)
lossOriginal = kfoldLoss(crossval(ctree))
resubOptimized = resubLoss(Optimized_tree)
lossOptimized = kfoldLoss(crossval(Optimized_tree))
resubPrune = resubLoss(ctreePrune)
lossPrune = kfoldLoss(crossval(ctreePrune))
the correct ratio of A is: 1
the correct ratio of B is: 1
the correct ratio of C is: 0.90909
the total correct ratio is: 0.96667
the min error leaf number is: 6
resubOriginal =
0.0250
lossOriginal =
0.0583
resubOptimized =
0.0333
lossOptimized =
0.0500
resubPrune =
0.0250
lossPrune =
0.0500