Java 如何根据列值将二维数组拆分为多个二维数组?
我正在尝试编写一个代码,从头开始创建一个决策树。有一次,我需要将包含数据的二维数组(填充)拆分为多个二维数组,然后将这些数组拆分为更多的数组。例如,如果我的2D数组中有这样的数据Java 如何根据列值将二维数组拆分为多个二维数组?,java,arrays,multidimensional-array,decision-tree,Java,Arrays,Multidimensional Array,Decision Tree,我正在尝试编写一个代码,从头开始创建一个决策树。有一次,我需要将包含数据的二维数组(填充)拆分为多个二维数组,然后将这些数组拆分为更多的数组。例如,如果我的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, norm
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
然后我想使用相同的算法,找到分割的最佳值,然后再次分割,比如说,我想根据第四列的值再次分割sunny数组。这次的结果将是两个子集
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));});
});
只需将分组应用于前面的组,即可实现多级分组,如下所示
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));});
});
});
定义“要拆分的最佳列”。描述您的需求可以真正帮助您了解如何为其编码。
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]