[英]Weka Decision tree Java to lists
我想制作一个决策树并将其分解为列表(名称,符号,值)。 我用以下代码制作了树:
//Get File
BufferedReader reader = new BufferedReader(new FileReader(PATH + "TempArffFile.arff"));
//Get the data
Instances data = new Instances(reader);
reader.close();
//Setting class attribute
data.setClassIndex(data.numAttributes() - 1);
//Make tree
J48 tree = new J48();
String[] options = new String[1];
options[0] = "-U";
tree.setOptions(options);
tree.buildClassifier(data);
//Print tree
System.out.println(tree);
现在我需要将其分解为数组,我该怎么做?
示例:我得到这棵树:
title <= 1: bad (4.0)
title > 1
| positionMatch <= 1
| | countryCode <= 1: good (3.0/1.0)
| | countryCode > 1: bad (8.0/3.0)
| positionMatch > 1: good (4.0/1.0)
所以我想从那棵树上得到4个列表:
- 标题(<= 1)->错误
- title(> 1)-> position(<= 1)-> countryCode(<= 1)-> good
- title(> 1)-> position(<= 1)-> countryCode(> 1)->错误
- 标题(> 1)->位置(> 1)->好
我怎样才能做到这一点 ?
不太好,但可能更好然后什么也没有...也许它将给您一个主意。
public static void split(String tree){
String[] lines = tree.split("\n");
List<List<String>> lists = new ArrayList<List<String>>();
for(String line : lines){
List<String> temp = new ArrayList<String>();
while(line.indexOf("|") != -1){
temp.add("|");
line = line.replaceFirst("\\|", "");
}
temp.add(line.trim());
lists.add(temp);
}
for(int i = 0; i < 3; i++){
lists.remove(0);
}
for(int i = 0; i < 4; i++){
lists.remove(lists.size()-1);
}
List<String> substitutes = new ArrayList<String>();
for(List<String> list : lists){
for(int i = 0; i < list.size(); i++){
if(!list.get(i).contains(":") && !list.get(i).equals("|") && !substitutes.contains(list.get(i))){
substitutes.add(list.get(i));
}
}
}
for(List<String> list : lists){
for(int i = 0; i < list.size(); i++){
if(list.get(i).equals("|")){
list.set(i, substitutes.get(i));
}
}
}
StringBuilder sb = new StringBuilder();
for(List<String> list : lists){
String line = "";
for(String s : list){
line = line+" "+s;
}
if(line.endsWith(")")){
sb.append(line+"\n");
}
}
System.out.println(sb.toString());
}
输入
花瓣宽度<= 0.6:鸢尾(50.0)
花瓣宽度> 0.6
| 花瓣宽度<= 1.7
| | 花瓣长度<= 4.9:鸢尾花(48.0 / 1.0)
| | 花瓣长度> 4.9
| | | 花瓣宽度<= 1.5:鸢尾(3.0)
| | | 花瓣宽度> 1.5:鸢尾花(3.0 / 1.0)
| 花瓣宽度> 1.7:鸢尾(46.0 / 1.0)
叶数:5
树的大小:9
输出:
花瓣宽度<= 0.6:鸢尾(50.0)
花瓣宽度> 0.6花瓣宽度<= 1.7花瓣长度<= 4.9:鸢尾花(48.0 / 1.0)
花瓣宽度> 0.6花瓣宽度<= 1.7花瓣长度> 4.9花瓣宽度<= 1.5:鸢尾(3.0)
花瓣宽度> 0.6花瓣宽度<= 1.7花瓣长度> 4.9花瓣宽度> 1.5:鸢尾花(3.0 / 1.0)
花瓣宽度> 0.6花瓣宽度> 1.7:鸢尾(46.0 / 1.0)
首先,带有解析行的答案似乎有问题(我无法对此进行评论:(),似乎它用父子句替换了每个“ |”,但对于像这样的树:
A | CB | d
替换将是错误的,都是'|' C和D的前面将替换为A。
同样,扩展类J48并重新实现toString()也无济于事,因为树实际上存在于私有变量m_root中。
更新:
解析字符串的更好解决方案。
private void string(J48 j48) {
String tree = j48.toString();
String[] lines = tree.split("\n");
List<List<String>> lists = new ArrayList<List<String>>();
// Break lines into parts.
for(String line : lines){
List<String> temp = new ArrayList<String>();
while(line.indexOf("|") != -1){
temp.add("|");
line = line.replaceFirst("\\|", "");
}
temp.add(line.trim());
lists.add(temp);
}
// remove first 3 lines of the output.
for(int i = 0; i < 3; i++){
lists.remove(0);
}
// remove last 4 lines of the output.
for(int i = 0; i < 4; i++){
lists.remove(lists.size()-1);
}
// This is a ordered list of parents for any given node while traversing the tree.
List<String> parentClauses = new ArrayList<String>();
// this describes the depth
//int depth = -1;
// all the paths in the tree.
List<List<String>> paths = new ArrayList<List<String>>();
for (List<String> list : lists) {
int currDepth = 0;
for(int i = 0; i < list.size(); i++){
String token = list.get(i);
// find how deep is this node in the tree.
if (token.equals("|")) {
currDepth++;
}
else { // now we get to the string token for the node.
// if leaf, so we get one path..
if (token.contains(":")) {
List<String> path = new ArrayList<String>();
for (int index = 0; index < currDepth; index++) {
path.add(parentClauses.get(index));
}
path.add(token);
paths.add(path);
}
else {
// add this to the current parents list
parentClauses.add(currDepth, token);
}
}
}
}
// print each of the paths.
for (List<String> path : paths) {
String str = "";
for (String token : path) {
str += token + " AND ";
}
LOG.info(str + "\n");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.