Groovy调用Weka生成决策树


运行效果:
Groovy调用Weka生成决策树_第1张图片

Groovy代码:
import weka.classifiers.Classifier
import weka.classifiers.trees.J48
import weka.core.Instances
import weka.core.converters.ArffLoader
import weka.gui.treevisualizer.PlaceNode2
import weka.gui.treevisualizer.TreeVisualizer

import javax.swing.JFrame
import java.awt.BorderLayout

/**
 Weka的决策树分析功能测试
 @author bruce.lin.chn
 @version 2013-01-29
*/


Classifier classifier= new J48()
ArffLoader arffLoader=new ArffLoader()
arffLoader.setFile(new File("../cpu.with.vendor.arff"))

Instances instancesTraining=arffLoader.getDataSet()
instancesTraining.setClassIndex(0)

println "共加载:"+instancesTraining.numInstances().toString()
classifier.buildClassifier(instancesTraining)

println "训练完毕!"

TreeVisualizer treeVisualizer=new TreeVisualizer(null, classifier.graph(), new PlaceNode2())
JFrame jFrame=new JFrame("决策树测试:J48")
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
jFrame.setSize(1024, 600)
jFrame.getContentPane().setLayout(new BorderLayout())
jFrame.getContentPane().add(treeVisualizer, BorderLayout.CENTER)

jFrame.setVisible(true)
treeVisualizer.fitToScreen()


你可能感兴趣的:(groovy)