Groupby 聚合和缺失的值组合

Groupby aggregations and missing combinations of values

我最近开始修补 Vega-Lite 模板,为名为 DVC 的开源数据科学软件制作混淆矩阵。您可以在 my PR here 中看到该模板,但我还会在下面重复一个简化版本:

{
    ...
    "data": {
        "values": [
            {"actual": "Wake", "predicted": "Wake", "rev": "HEAD"},
            {"actual": "Wake", "predicted": "Deep", "rev": "HEAD"},
            {"actual": "Light", "predicted": "Wake", "rev": "HEAD"},
            {"actual": "REM", "predicted": "Light", "rev": "HEAD"},
            ....
        ],
    },
    "spec": {
        "transform": [
            {
                "aggregate": [{"op": "count", "as": "xy_count"}],
                "groupby": ["actual", "predicted"],
            },
            {
                "joinaggregate": [
                    {"op": "max", "field": "xy_count", "as": "max_count"}
                ],
                "groupby": [],
            },
            {
                "calculate": "datum.xy_count / datum.max_count",
                "as": "percent_of_max",
            },
        ],
        "encoding": {
            "x": {"field": "predicted", "type": "nominal", "sort": "ascending"},
            "y": {"field": "actual", "type": "nominal", "sort": "ascending"},
        },
        "layer": [
            {
                "mark": "rect",
                "width": 300,
                "height": 300,
                "encoding": {
                    "color": {
                        "field": "xy_count",
                        "type": "quantitative",
                        "title": "",
                        "scale": {"domainMin": 0, "nice": True},
                    }
                },
            },
            {
                "mark": "text",
                "encoding": {
                    "text": {
                        "field": "xy_count",
                        "type": "quantitative"
                    },
                    "color": {
                        "condition": {
                            "test": "datum.xy_count / datum.max_count > 0.5",
                            "value": "white"
                        },
                        "value": "black"
                    }
                }
            }
        ]
    }
}

因此,由于我正在进行 groupby 聚合,因此混淆矩阵中可能存在没有条目的单元格。这是一个示例输出:link

我怎样才能用“后备”之类的东西填充这些单元格。我也研究过使用 pivot 和 impute,但不太明白。帮助非常感谢:)

您可以通过在转换序列的末尾添加两个 Impute transforms 来完成此操作:

  {"impute": "xy_count", "groupby": ["actual"], "key": "predicted", "keyvals": ["Deep", "Light", "Wake", "REM"], "value": 0},
  {"impute": "xy_count", "groupby": ["predicted"], "key": "actual", "keyvals": ["Deep", "Light", "Wake", "REM"], "value": 0}

keyvals 指定您希望在每个轴上估算哪些缺失值;如果每个键值至少存在一个组,则可以将其省略。