Weka演算法Classifier-tree-RandomForest源碼分析(二)代碼實現,randomforest

來源:互聯網
上載者:User

Weka演算法Classifier-tree-RandomForest源碼分析(二)代碼實現,randomforest



RandomForest的實現異常的簡單,簡單的超出博主的預期,Weka在實現方式上組合了Bagging和RandomTree。


一、RandomForest的訓練

構建RandomForest的代碼如下:

  public void buildClassifier(Instances data) throws Exception {    // can classifier handle the data?    getCapabilities().testWithFail(data);    // remove instances with missing class    data = new Instances(data);    data.deleteWithMissingClass();    m_bagger = new Bagging();    RandomTree rTree = new RandomTree();    // set up the random tree options    m_KValue = m_numFeatures;    if (m_KValue < 1)      m_KValue = (int) Utils.log2(data.numAttributes()) + 1;    rTree.setKValue(m_KValue);    rTree.setMaxDepth(getMaxDepth());    // set up the bagger and build the forest    m_bagger.setClassifier(rTree);    m_bagger.setSeed(m_randomSeed);    m_bagger.setNumIterations(m_numTrees);    m_bagger.setCalcOutOfBag(true);    m_bagger.buildClassifier(data);  }
通過這段代碼很直觀的可以看出首先把無效資料去掉,然後建立了一個Bag,設定隨機森林中每棵樹所用到的屬性的值,設定最大深度,接著把這棵RandomTree當做基分類器傳遞給Bagging,最後調用bagging的訓練方法進行訓練。


二、RandomForest分類

看完訓練過程看具體的分類過程,也就是classifyInstance函數,值得注意的是,RandomForest繼承自Classifier,卻沒有隊classifyInstance方法進行重載,使用的是基類Classifier的classifyInstance函數,但卻重載了distributionForInstance,而distributionForInstance卻是Classifier的classifyInstance函數所用到的一個函數,返回一個instance在所有類上的機率。代碼如下:

  public double[] distributionForInstance(Instance instance) throws Exception {    return m_bagger.distributionForInstance(instance);  }
可以看到,算出給定instance在各class上的分布是委託給bagger去做的(真懶),所以這裡也不做詳細分析,詳細分析留到分析bagger的時候再說。

接下來看基類Classifier是如何使用distribution來給出分類結果的。

  public double classifyInstance(Instance instance) throws Exception {    double[] dist = distributionForInstance(instance);    if (dist == null) {      throw new Exception("Null distribution predicted");    }    switch (instance.classAttribute().type()) {    case Attribute.NOMINAL:      double max = 0;      int maxIndex = 0;      for (int i = 0; i < dist.length; i++) {        if (dist[i] > max) {          maxIndex = i;          max = dist[i];        }      }      if (max > 0) {        return maxIndex;      } else {        return Instance.missingValue();      }    case Attribute.NUMERIC:    case Attribute.DATE:      return dist[0];    default:      return Instance.missingValue();    }  }
可以很直觀的看到,如果要是一個分類,則給出機率最大值,如果是一個迴歸(即classIndex對應的屬性是數值),則返回dist[0],這裡是使用了一個約定,第一個元素代表迴歸值。


三、總結

對於RandomForest的程式碼分析差不多就結束了,基本沒什麼實質內容,因為演算法的主要工作都交由Bagging和RandomForest去做了,值得注意的是,當沒有指定抽樣屬性的數量時,Weka使用的log2(K)作為經驗值。


下一篇部落格將分析Weka的RandomTree,之後將分析Bagging,這樣就算把RandomForest補全了。




最近想學習資料採礦,問有基於weka的聚類演算法的源碼實現?

我最近剛開始接觸資料採礦,在學習kmeans,由於隨著kmeans中心的隨機改動,聚類結果有些不合理的變化,所以正在試圖將初始中心定下來
 
weka&資料採礦:Weka裡面有很多的演算法,但是Weka並沒有給出演算法怎實現的,有沒有相關的論文可以看看

我覺得直接看源碼比較容易理解,WEKA是開源的

如果你要資訊增益的話看這個:

www.360doc.com/....shtml
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.