【问题标题】:How to split a 2D Array into Multiple 2D Arrays depending on column values Java?如何根据列值 Java 将 2D 数组拆分为多个 2D 数组?
【发布时间】:2020-03-21 23:26:03
【问题描述】:

我正在尝试编写一个从头开始制作决策树的代码。在某一时刻,我需要将我的 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

然后我想使用相同的算法,找到要拆分的最佳值,然后再次拆分,例如,假设我想根据第四列的值再次拆分 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 arrays multidimensional-array decision-tree


【解决方案1】:

我不认为拆分是正确使用的术语。当我查看您的示例时,您似乎更愿意对数据进行分组。在 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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2013-05-07
    • 2021-01-27
    • 2014-08-31
    • 1970-01-01
    • 2018-07-05
    相关资源
    最近更新 更多