C4.5演算法是一種用來對數據實作出決策樹(Decision tree analysis)分析的演算法,由Ross Quinlan於1993改進ID3演算法(Ross Quinlan,1979)提出.決策樹的分析可以處做出條件判斷式(也就是所謂的邏輯模型),用來對資料做預測,這樣的好處是生成的條件判斷可以讓人看得懂,有助於做出商業邏輯的判斷和理解,缺點是由於要做好幾次排序處理,所以效率比較低
C4.5演算法的分割規則是用:資訊熵(Entropy)和資訊獲利比率(Gain Ratio),虛擬碼參考自維基百科:
- Check for base cases
- For each attribute a
- Find the normalized information gain ratio from splitting on a
- Let a_bestbe the attribute with the highest normalized information gain
- Create a decision nodethat splits on a_best
- Recur on the sublists obtained by splitting on a_best, and add those nodes as children of node
實做的部分利用Weka平台,現在假設我要分析一款App是不是能夠成功被市場接受,已有的歷史資料如下:
(我這裡有省略13之後的資料,這份資料是自己亂數key的,只是demo用,沒有參考價值)
接著要轉換成weka看得懂的格式
@relation XXX表示分析的主題,
@attribute YYY表示分析的參數名稱
(numeric是數字,{}是集合)以下是範例:
@relation AppAnalysisTest
@attribute AppAttribute {social,tool,game}
@attribute AppStartUsers numeric
@attribute FirstMonthIncreasUser numeric
@attribute Pivot{yes, no}
@attribute Success{yes, no}
@data
social,10,50,yes,no
tool,1,79,no,yes
game,75,99,yes,yes
social,134,1020,no,yes
game,34,10009,yes,yes
social,0,803,yes,no
一樣有省略掉資料,我的訓練資料可以下載:
接著是程式碼的部分:
package javaapplication58; import java.awt.BorderLayout; import java.io.BufferedReader; import java.io.FileReader; import javax.swing.JFrame; import weka.classifiers.trees.J48; import weka.core.Instances; import weka.gui.treevisualizer.PlaceNode2; import weka.gui.treevisualizer.TreeVisualizer; public class JavaApplication58 { public static void main(String args[]) throws Exception { BufferedReader reader = new BufferedReader( new FileReader("App.arff")); //讀檔 J48 AnsC45=new J48();//C4.5的分析物件在weka.jar上叫做J48() Instances data = new Instances(reader);//把資料放進來 reader.close(); data.setClassIndex(data.numAttributes()-1); AnsC45.buildClassifier(data);//這裡做分析 System.out.println(AnsC45.toSummaryString());//印出結論 //視覺化分析結果 TreeVisualizer tv=new TreeVisualizer(null,AnsC45.graph(),new PlaceNode2()); JFrame visual=new JFrame("Example");//視窗名稱 visual.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); visual.setSize(800,600);//視窗大小 visual.getContentPane().add(tv,BorderLayout.CENTER);//加入圖表 visual.setVisible(true); // } }
跑出來的結果如下:
run:
Number of leaves: 4
Size of the tree: 6
以及
最後的解讀,一個可能可以成功的App的模式可以是一初始使用者人數大於10,如果小於10則只有工具類型的才會成功(以上分析純屬唬爛,因為數據是掰的)
另外,如果想要跑數據預測可以參考這裡
發表留言