如何使用java代码在weka中显示IF-THEN规则
我有以图表形式显示规则的代码,但我如何获得IF-THEN规则。
我想像这样输出
outlook = sunny
| humidity = high: no (3.0)
| humidity = normal: yes (2.0)
outlook = overcast: yes (4.0)
outlook = rainy
| windy = TRUE: no (2.0)
| windy = FALSE: yes (3.0)
这是我显示图表格式规则的java代码
public class Evaluteagain extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter p = resp.getWriter();
p.write("\n");
DataSource source = null;
try {
source = new DataSource("E:/data/test/finalcsv.csv");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Instances dataset = null;
try {
dataset = source.getDataSet();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//set class index to the last attribute
dataset.setClassIndex(dataset.numAttributes()-1);
//create and build the classifier!
J48 tree = new J48();
try {
tree.buildClassifier(dataset);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// display classifier
final javax.swing.JFrame jf =
new javax.swing.JFrame("Tree Visualizer: J48");
jf.setSize(500,400);
jf.getContentPane().setLayout(new BorderLayout());
TreeVisualizer tv = null;
try {
tv = new TreeVisualizer(null,
tree.graph(),
new PlaceNode2());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jf.getContentPane().add(tv, BorderLayout.CENTER);
jf.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
jf.dispose();
}
});
jf.setVisible(true);
tv.fitToScreen();
}
}
}
答案 0 :(得分:-1)
以下代码用于介绍如何使用java代码在weka中显示IF-THEN规则 在这段代码中,我在Servlet中生成规则并在控制台中打印...
public class Ifthenrules extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DataSource source = null;
try {
//input dataset
source = new DataSource("E:/data/test/finalcsv.csv");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Instances dataset = null;
try {
dataset = source.getDataSet();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//set class index to the last attribute
dataset.setClassIndex(dataset.numAttributes()-1);
//create and build the classifier!
J48 tree = new J48();
try {
tree.buildClassifier(dataset);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String testt = "Ifthenrules";
try {
// Generate rules in IF-THEN form
tree.toSource(testt);
} catch (Exception e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
//now print rules
System.out.println(tree.toString());
}
}