【发布时间】:2015-05-13 14:57:35
【问题描述】:
我有一个列表列表:
List<ArrayList<String>> D = new ArrayList<>();
填充后,它可能看起来像:
[“A”、“B”、“Y”]
[“C”、“D”、“Y”]
[“A”、“D”、“N”]
我想根据唯一的属性值(假设索引 1)将列表列表拆分为分区。
所以索引 1 处的属性有两个唯一值,“B”和“D”,所以我想拆分为:
[“A”、“B”、“Y”]
[“C”、“D”、“Y”]
[“A”、“D”、“N”]
并将它们放入List<ArrayList<ArrayList<String>>> sublists;
有没有一种聪明的方法可以做到这一点,或者我只是做这样的事情:
List<ArrayList<ArrayList<String>>> sublists = new ArrayList<>();
int featIdx = 1;
// generate the subsets
for (ArrayList<String> record : D) {
String val = record.get(featIdx);
// check if the value exists in sublists
boolean found = false;
for (ArrayList<ArrayList<String>> entry : sublists) {
if (entry.get(0).get(featIdx).equals(val)) {
entry.add(record);
found = true;
break;
}
}
if (!found) {
sublists.add(new ArrayList<>());
sublists.get(sublists.size()-1).add(record);
}
}
这是 C4.5 决策树 algorithm 的一个步骤,所以如果有人有这方面的经验,如果你能告诉我这是否是生成子列表的正确方法,我将不胜感激。
谢谢。
【问题讨论】:
标签: java arraylist java-8 classification decision-tree