作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个从头开始生成决策树的代码。在某一时刻,我需要将带有数据的 2D 数组(填充)拆分为多个 2D 数组,然后将这些数组拆分为更多数组。例如,如果我的二维数组中有这样的数据
sunny, hot, high, weak, no
sunny, hot, high, strong, no
overcast, hot, high, weak, yes
rain, mild, high, weak, yes
rain,cool, normal, weak, yes
rain, cool, normal, strong, no
overcast, cool, normal, strong, yes
sunny, mild, high, weak, no
sunny, cool, normal, weak, yes
rain, mild, normal, weak, yes
sunny, mild, normal, strong, yes
overcast, mild, high, strong, yes
overcast, hot, normal, weak, yes
rain, mild, high, strong, no
所以我将有一个代码来计算要分割的最佳列,然后我希望它分割它。例如,假设我想根据第一列的值进行拆分。结果将是三个子集
sunny rain overcast
sunny, hot, high, weak, no rain, mild, high, weak, yes overcast, hot, high, weak, yes
sunny, hot, high, strong, no rain,cool, normal, weak, yes overcast, cool, normal, strong, yes
sunny, mild, high, weak, no rain, cool, normal, strong, no overcast, mild, high, strong, yes
sunny, cool, normal, weak, yes rain, mild, normal, weak, yes overcast, hot, normal, weak, yes
sunny, mild, normal, strong, yes rain, mild, high, strong, no
然后我想使用相同的算法,找到最佳的分割值,然后再次分割,例如,假设我想根据第四列的值再次分割阳光数组。这次的结果将是两个子集
weak strong
sunny, hot, high, weak, no sunny, hot, high, strong, no
sunny, mild, high, weak, no sunny, mild, normal, strong, yes
sunny, cool, normal, weak, yes
我知道逻辑应该是这样的
split(featureToSplitOn, data)
int bestColumn=findBestColumnToSplitOn();
for every value in best column
split(value,subset);
但老实说我不知道该怎么做。有什么想法吗?
最佳答案
我认为拆分不是正确的术语。当我查看您的示例时,您似乎更愿意对数据进行分组。在 Java 中对集合中的元素进行分组的最佳且最简单的方法是借助流。以下是一些可以作为您起点的示例:
String[][] myArray = { {"sunny", "hot", "high", "weak", "no"},
{"sunny", "hot", "high", "strong", "no"},
{"overcast", "hot", "high", "weak", "yes"},
{"rain", "mild", "high", "weak", "yes"},
{"rain", "cool", "normal", "weak", "yes"},
{"rain", "cool", "normal", "strong", "no"},
{"overcast", "cool", "normal", "strong", "yes"},
{"sunny", "mild", "high", "weak", "no"},
{"sunny", "cool", "normal", "weak", "yes"},
{"rain", "mild", "normal", "weak", "yes"},
{"sunny", "mild", "normal", "strong", "yes"},
{"overcast", "mild", "high", "strong", "yes"},
{"overcast", "hot", "normal", "weak", "yes"},
{"rain", "mild", "high", "strong", "no"}
};
Map<String,List<String[]>> mapByfirstColumn = Arrays.stream(myArray).collect(Collectors.groupingBy(e->e[0]));
mapByfirstColumn.forEach((key, val)-> {
System.out.println(key);
val.forEach(arr -> {System.out.println(Arrays.toString(arr));});
});
上述代码的输出是:
rain
[rain, mild, high, weak, yes]
[rain, cool, normal, weak, yes]
[rain, cool, normal, strong, no]
[rain, mild, normal, weak, yes]
[rain, mild, high, strong, no]
overcast
[overcast, hot, high, weak, yes]
[overcast, cool, normal, strong, yes]
[overcast, mild, high, strong, yes]
[overcast, hot, normal, weak, yes]
sunny
[sunny, hot, high, weak, no]
[sunny, hot, high, strong, no]
[sunny, mild, high, weak, no]
[sunny, cool, normal, weak, yes]
[sunny, mild, normal, strong, yes]
您可以通过将分组应用于前面的组来实现多级分组,如下所示
Map<String,Map<String,List<String[]>>> mapByFirstThen3rd = Arrays.stream(myArray)
.collect(Collectors.groupingBy(e->e[0],Collectors.groupingBy(e->e[3])));
mapByFirstThen3rd.forEach((outerKey, map)-> {
System.out.println();
System.out.println(outerKey);
map.forEach((innerKey, val) ->{
System.out.println(innerKey);
val.forEach(arr -> {System.out.println(Arrays.toString(arr));});
});
});
它将输出类似
的内容rain
strong
[rain, cool, normal, strong, no]
[rain, mild, high, strong, no]
weak
[rain, mild, high, weak, yes]
[rain, cool, normal, weak, yes]
[rain, mild, normal, weak, yes]
overcast
strong
[overcast, cool, normal, strong, yes]
[overcast, mild, high, strong, yes]
weak
[overcast, hot, high, weak, yes]
[overcast, hot, normal, weak, yes]
sunny
strong
[sunny, hot, high, strong, no]
[sunny, mild, normal, strong, yes]
weak
[sunny, hot, high, weak, no]
[sunny, mild, high, weak, no]
[sunny, cool, normal, weak, yes]
关于java - 如何根据列值Java将一个2D数组拆分为多个2D数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59052697/
我是一名优秀的程序员,十分优秀!