我想制作一个决策树并将其分解为列表(名称,符号,val)。 我用这段代码制作了树:
//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个列表:
- title(&lt; = 1) - &gt;坏
- title(&gt; 1) - &gt;位置(&lt; = 1) - &gt; countryCode(&lt; = 1) - &gt;好
- title(&gt; 1) - &gt;位置(&lt; = 1) - &gt; countryCode(&gt; 1) - &gt;坏
- title(&gt; 1) - &gt;位置(&gt; 1) - &gt;好
我该怎么做?
答案 0 :(得分:2)
不是那么好但可能更好然后没有... 也许它会给你一个想法。
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());
}
输入
petalwidth&lt; = 0.6:Iris-setosa(50.0)
花瓣宽度&gt; 0.6
| petalwidth&lt; = 1.7
| | petallength&lt; = 4.9:Iris-versicolor(48.0 / 1.0)
| | petallength&gt; 4.9
| | |花瓣宽度<= 1.5:Iris-virginica(3.0)
| | |花瓣宽度&gt; 1.5:虹膜鸢尾(3.0 / 1.0)
|花瓣宽度&gt; 1.7:Iris-virginica(46.0 / 1.0)
叶数:5
树的大小:9
输出:
petalwidth&lt; = 0.6:Iris-setosa(50.0)
花瓣宽度&gt; 0.6 petalwidth <= 1.7 petallength&lt; = 4.9:Iris-versicolor(48.0 / 1.0)
花瓣宽度&gt; 0.6瓣宽&lt; = 1.7 petallength&gt; 4.9 petalwidth&lt; = 1.5:Iris-virginica(3.0)
花瓣宽度&gt; 0.6瓣宽&lt; = 1.7 petallength&gt; 4.9 petalwidth&gt; 1.5:虹膜鸢尾(3.0 / 1.0)
花瓣宽度&gt; 0.6 petalwidth&gt; 1.7:Iris-virginica(46.0 / 1.0)
答案 1 :(得分:1)
首先解析行的答案似乎有些错误(我无法评论它:(),似乎它用parent子句替换每个'|',但对于像这样的树:
一个 | C 乙 | 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");
}
}