99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀簡單易學(xué)的機(jī)器學(xué)習(xí)算法—決策樹之ID3算法
簡單易學(xué)的機(jī)器學(xué)習(xí)算法—決策樹之ID3算法
2017-03-22
收藏

簡單易學(xué)的機(jī)器學(xué)習(xí)算法—決策樹之ID3算法

一、決策樹分類算法概述
    決策樹算法是從數(shù)據(jù)的屬性(或者特征)出發(fā),以屬性作為基礎(chǔ),劃分不同的類。例如對于如下數(shù)據(jù)集

(數(shù)據(jù)集)
其中,第一列和第二列為屬性(特征),最后一列為類別標(biāo)簽,1表示是,0表示否。決策樹算法的思想是基于屬性對數(shù)據(jù)分類,對于以上的數(shù)據(jù)我們可以得到以下的決策樹模型

決策樹模型)
先是根據(jù)第一個屬性將一部份數(shù)據(jù)區(qū)分開,再根據(jù)第二個屬性將剩余的區(qū)分開。
    實現(xiàn)決策樹的算法有很多種,有ID3、C4.5和CART等算法。下面我們介紹ID3算法。
二、ID3算法的概述
    ID3算法是由Quinlan首先提出的,該算法是以信息論為基礎(chǔ),以信息熵和信息增益為衡量標(biāo)準(zhǔn),從而實現(xiàn)對數(shù)據(jù)的歸納分類。
    首先,ID3算法需要解決的問題是如何選擇特征作為劃分?jǐn)?shù)據(jù)集的標(biāo)準(zhǔn)。在ID3算法中,選擇信息增益最大的屬性作為當(dāng)前的特征對數(shù)據(jù)集分類。信息增益的概念將在下面介紹,通過不斷的選擇特征對數(shù)據(jù)集不斷劃分;
    其次,ID3算法需要解決的問題是如何判斷劃分的結(jié)束。分為兩種情況,第一種為劃分出來的類屬于同一個類,如上圖中的最左端的“非魚類”,即為數(shù)據(jù)集中的第5行和第6行數(shù)據(jù);最右邊的“魚類”,即為數(shù)據(jù)集中的第2行和第3行數(shù)據(jù)。第二種為已經(jīng)沒有屬性可供再分了。此時就結(jié)束了。
    通過迭代的方式,我們就可以得到這樣的決策樹模型。

(ID3算法基本流程)
三、劃分?jǐn)?shù)據(jù)的依據(jù)
    ID3算法是以信息熵和信息增益作為衡量標(biāo)準(zhǔn)的分類算法。
1、信息熵(Entropy)
   熵的概念主要是指信息的混亂程度,變量的不確定性越大,熵的值也就越大,熵的公式可以表示為:

其中為類別在樣本s中出現(xiàn)的概率。
2、信息增益(Information gain)
   信息增益指的是劃分前后熵的變化,可以用下面的公式表示:

其中,a表示樣本的屬性,是屬性所有的取值集合。v是a的其中一個屬性值,sv是s中a的值為v的樣例集合。

四、實驗仿真
1、數(shù)據(jù)預(yù)處理
    我們以下面的數(shù)據(jù)為例,來實現(xiàn)ID3算法:

我們首先需要對數(shù)據(jù)處理,例如age屬性,我們用0表示youth,1表示middle_aged,2表示senior等等。

(將表格數(shù)據(jù)化)
2、實驗結(jié)果

(原始的數(shù)據(jù))

(劃分1)

(劃分2)

(劃分3)

(最終的決策樹
MATLAB代碼
主程序
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
%% Decision Tree  
% ID3  
 
%導(dǎo)入數(shù)據(jù)  
%data = [1,1,1;1,1,1;1,0,0;0,1,0;0,1,0];    
 
data = [0,2,0,0,0;  
    0,2,0,1,0;  
    1,2,0,0,1;  
    2,1,0,0,1;  
    2,0,1,0,1;  
    2,0,1,1,0;  
    1,0,1,1,1;  
    0,1,0,0,0;  
    0,0,1,0,1;  
    2,1,1,0,1;  
    0,1,1,1,1;  
    1,1,0,1,1;  
    1,2,1,0,1;  
    2,1,0,1,0];  
 
% 生成決策樹  
createTree(data);  

生成決策樹
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
function [ output_args ] = createTree( data )  
    [m,n] = size(data);  
    disp('original data:');  
    disp(data);  
    classList = data(:,n);  
    classOne = 1;%記錄第一個類的個數(shù)  
    for i = 2:m  
        if classList(i,:) == classList(1,:)  
            classOne = classOne+1;  
        end  
    end  
      
    % 類別全相同  
    if classOne == m  
        disp('final data: ');  
        disp(data);  
        return;  
    end  
      
    % 特征全部用完  
    if n == 1  
        disp('final data: ');  
        disp(data);  
        return;  
    end  
      
    bestFeat = chooseBestFeature(data);  
    disp(['bestFeat: ', num2str(bestFeat)]);  
    featValues = unique(data(:,bestFeat));  
    numOfFeatValue = length(featValues);  
      
    for i = 1:numOfFeatValue  
        createTree(splitData(data, bestFeat, featValues(i,:)));  
        disp('-------------------------');  
    end  
end  

選擇信息增益最大的特征
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
%% 選擇信息增益最大的特征  
function [ bestFeature ] = chooseBestFeature( data )  
    [m,n] = size(data);% 得到數(shù)據(jù)集的大小  
      
    % 統(tǒng)計特征的個數(shù)  
    numOfFeatures = n-1;%最后一列是類別  
    % 原始的熵  
    baseEntropy = calEntropy(data);  
      
    bestInfoGain = 0;%初始化信息增益  
    bestFeature = 0;% 初始化最佳的特征位  
      
    % 挑選最佳的特征位  
    for j = 1:numOfFeatures  
        featureTemp = unique(data(:,j));  
        numF = length(featureTemp);%屬性的個數(shù)  
        newEntropy = 0;%劃分之后的熵  
        for i = 1:numF  
            subSet = splitData(data, j, featureTemp(i,:));  
            [m_1, n_1] = size(subSet);  
            prob = m_1./m;  
            newEntropy = newEntropy + prob * calEntropy(subSet);  
        end  
          
        %計算增益  
        infoGain = baseEntropy - newEntropy;  
          
        if infoGain > bestInfoGain  
            bestInfoGain = infoGain;  
            bestFeature = j;  
        end  
    end  
end  

計算熵
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
function [ entropy ] = calEntropy( data )  
    [m,n] = size(data);  
      
    % 得到類別的項  
    label = data(:,n);  
      
    % 處理完的label  
    label_deal = unique(label);  
      
    numLabel = length(label_deal);  
    prob = zeros(numLabel,2);  
      
    % 統(tǒng)計標(biāo)簽  
    for i = 1:numLabel  
        prob(i,1) = label_deal(i,:);  
        for j = 1:m  
            if label(j,:) == label_deal(i,:)  
                prob(i,2) = prob(i,2)+1;  
            end  
        end  
    end  
      
    % 計算熵  
    prob(:,2) = prob(:,2)./m;  
    entropy = 0;  
    for i = 1:numLabel  
        entropy = entropy - prob(i,2) * log2(prob(i,2));  
    end  
end  

劃分?jǐn)?shù)據(jù)
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
function [ subSet ] = splitData( data, axis, value )  
    [m,n] = size(data);%得到待劃分數(shù)據(jù)的大小  
      
    subSet = data;  
    subSet(:,axis) = [];  
    k = 0;  
    for i = 1:m  
        if data(i,axis) ~= value  
            subSet(i-k,:) = [];  
            k = k+1;  
        end  
    end     
end 

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }