機器學習筆記關於MATLAB實現決策樹

來源:互聯網
上載者:User

標籤:

 

 

%% I. 清空環境變數clear allclcwarning off%% II. 匯入資料load data.mat%%% 1. 隨機產生訓練集/測試集a = randperm(569);Train = data(a(1:500),:);Test = data(a(501:end),:);%%% 2. 訓練資料P_train = Train(:,3:end);T_train = Train(:,2);%%% 3. 測試資料P_test = Test(:,3:end);T_test = Test(:,2);%% III. 建立決策樹分類器ctree = ClassificationTree.fit(P_train,T_train);%%% 1. 查看決策樹視圖view(ctree);view(ctree,‘mode‘,‘graph‘);%% IV. 模擬測試T_sim = predict(ctree,P_test);%% V. 結果分析count_B = length(find(T_train == 1));count_M = length(find(T_train == 2));rate_B = count_B / 500;rate_M = count_M / 500;total_B = length(find(data(:,2) == 1));total_M = length(find(data(:,2) == 2));number_B = length(find(T_test == 1));number_M = length(find(T_test == 2));number_B_sim = length(find(T_sim == 1 & T_test == 1));number_M_sim = length(find(T_sim == 2 & T_test == 2));disp([‘病例總數:‘ num2str(569)...      ‘  良性:‘ num2str(total_B)...      ‘  惡性:‘ num2str(total_M)]);disp([‘訓練集病例總數:‘ num2str(500)...      ‘  良性:‘ num2str(count_B)...      ‘  惡性:‘ num2str(count_M)]);disp([‘測試集病例總數:‘ num2str(69)...      ‘  良性:‘ num2str(number_B)...      ‘  惡性:‘ num2str(number_M)]);disp([‘良性乳腺腫瘤確診:‘ num2str(number_B_sim)...      ‘  誤診:‘ num2str(number_B - number_B_sim)...      ‘  確診率p1=‘ num2str(number_B_sim/number_B*100) ‘%‘]);disp([‘惡性乳腺腫瘤確診:‘ num2str(number_M_sim)...      ‘  誤診:‘ num2str(number_M - number_M_sim)...      ‘  確診率p2=‘ num2str(number_M_sim/number_M*100) ‘%‘]);  %% VI. 葉子節點含有的最小樣本數對決策樹效能的影響leafs = logspace(1,2,10);N = numel(leafs);err = zeros(N,1);for n = 1:N    t = ClassificationTree.fit(P_train,T_train,‘crossval‘,‘on‘,‘minleaf‘,leafs(n));    err(n) = kfoldLoss(t);endplot(leafs,err);xlabel(‘葉子節點含有的最小樣本數‘);ylabel(‘交叉驗證誤差‘);title(‘葉子節點含有的最小樣本數對決策樹效能的影響‘)%% VII. 設定minleaf為13,產生最佳化決策樹OptimalTree = ClassificationTree.fit(P_train,T_train,‘minleaf‘,13);view(OptimalTree,‘mode‘,‘graph‘)%%% 1. 計算最佳化後決策樹的重採樣誤差和交叉驗證誤差resubOpt = resubLoss(OptimalTree)lossOpt = kfoldLoss(crossval(OptimalTree))%%% 2. 計算最佳化前決策樹的重採樣誤差和交叉驗證誤差resubDefault = resubLoss(ctree)lossDefault = kfoldLoss(crossval(ctree))%% VIII. 剪枝[~,~,~,bestlevel] = cvLoss(ctree,‘subtrees‘,‘all‘,‘treesize‘,‘min‘)cptree = prune(ctree,‘Level‘,bestlevel);view(cptree,‘mode‘,‘graph‘)%%% 1. 計算剪枝後決策樹的重採樣誤差和交叉驗證誤差resubPrune = resubLoss(cptree)lossPrune = kfoldLoss(crossval(cptree))

 結果

Decision tree for classification 1  if x23<112.8 then node 2 elseif x23>=112.8 then node 3 else 1 2  if x28<0.1456 then node 4 elseif x28>=0.1456 then node 5 else 1 3  if x7<0.07214 then node 6 elseif x7>=0.07214 then node 7 else 2 4  if x28<0.1358 then node 8 elseif x28>=0.1358 then node 9 else 1 5  if x22<23.74 then node 10 elseif x22>=23.74 then node 11 else 2 6  if x2<19.83 then node 12 elseif x2>=19.83 then node 13 else 2 7  if x8<0.03456 then node 14 elseif x8>=0.03456 then node 15 else 2 8  if x11<1.04755 then node 16 elseif x11>=1.04755 then node 17 else 1 9  if x28<0.139 then node 18 elseif x28>=0.139 then node 19 else 110  class = 111  if x5<0.09096 then node 20 elseif x5>=0.09096 then node 21 else 212  class = 113  class = 214  class = 115  class = 216  if x21<16.805 then node 22 elseif x21>=16.805 then node 23 else 117  class = 218  class = 219  class = 120  class = 121  class = 222  if x14<48.975 then node 24 elseif x14>=48.975 then node 25 else 123  class = 124  if x15<0.003294 then node 26 elseif x15>=0.003294 then node 27 else 125  class = 126  class = 127  if x22<33.35 then node 28 elseif x22>=33.35 then node 29 else 128  class = 129  if x22<33.56 then node 30 elseif x22>=33.56 then node 31 else 130  class = 231  class = 1病例總數:569  良性:357  惡性:212訓練集病例總數:500  良性:310  惡性:190測試集病例總數:69  良性:47  惡性:22良性乳腺腫瘤確診:45  誤診:2  確診率p1=95.7447%惡性乳腺腫瘤確診:19  誤診:3  確診率p2=86.3636%resubOpt =    0.0460lossOpt =    0.0740resubDefault =    0.0140lossDefault =    0.0820bestlevel =     4resubPrune =    0.0300lossPrune =    0.0800

  

機器學習筆記關於MATLAB實現決策樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.