树状图:figue.js(聚类)输出以适合可视化

Dendrogram: figue.js (clustering) output to fit visualization

似乎通过 JavaScript 进行的机器学习还处于起步阶段,因为几乎没有任何库可以在计算和可视化方面相互适应。

我正在使用 figue.js 库并希望通过以下方式输出结果:

http://www.meccanismocomplesso.org/en/dendrogramma-d3-parte1/ http://www.meccanismocomplesso.org/en/dendrogramma-d3-parte2/ http://www.meccanismocomplesso.org/en/dendrogramma-d3-parte3/

这是它的要求:

{
  "name": "root", "y" : 0,
  "children": [
  {
    "name": "parent A", "y" : 30,
    "children": [
      {"name": "child A1", "y" : 100},
      {"name": "child A2", "y" : 100},
      {"name": "child A3", "y" : 100}
    ]
  },{
    "name": "parent B", "y" : 76,
    "children": [
      {"name": "child B1", "y" : 100},
      {"name": "child B2", "y" : 100} // <--- number at the end is the ultrametric distance
    ]
  }
  ]
}

这就是 figue.js 给我的(当然不一样,所有的开发者都这样做...):

{
    "label": -1,
    "left": {
        "label": "Point 1",
        "left": null,
        "right": null,
        "dist": 0,
        "centroid": [
            13.0406203,
            55.606759100000005
        ],
        "size": 1,
        "depth": 0
    },
    "right": {
        "label": -1,
        "left": {
            "label": -1,
            "left": {
                "label": -1,
                "left": {
                    "label": "Point 2",
                    "left": null,
                    "right": null,
                    "dist": 0,
                    "centroid": [
                        13.0403852,
                        55.6066934
                    ],
                    "size": 1,
                    "depth": 0
                },
                "right": {
                    "label": "Point 5",
                    "left": null,
                    "right": null,
                    "dist": 0,
                    "centroid": [
                        13.0404121,
                        55.6066418
                    ],
                    "size": 1,
                    "depth": 0
                },
                "dist": 0.00005819080683319214,
                "centroid": [
                    13.04039865,
                    55.606667599999994
                ],
                "size": 2,
                "depth": 1
            },
            "right": {
                "label": "Point 3",
                "left": null,
                "right": null,
                "dist": 0,
                "centroid": [
                    13.0404818,
                    55.606629700000006
                ],
                "size": 1,
                "depth": 0
            },
            "dist": 0.00007074249076717127, // <--- ultrametric distance
            "centroid": [
                13.040426366666667,
                55.60665496666667
            ],
            "size": 3,
            "depth": 2
        },
        "right": {
            "label": "Point 4",
            "left": null,
            "right": null,
            "dist": 0,
            "centroid": [
                13.0405408,
                55.6066934
            ],
            "size": 1,
            "depth": 0
        },
        "dist": 0.00008682562985036432,
        "centroid": [
            13.040454975000001,
            55.606664574999996
        ],
        "size": 4,
        "depth": 3
    },
    "dist": 0.00010313457228779574,
    "centroid": [
        13.040488040000003,
        55.606683479999994
    ],
    "size": 5,
    "depth": 4
}

我可以说这很难解决。或者有人知道可以编译该结构的任何库吗?

var root = figue.figue.agglomerate(figueLabels, figueVectors, figue.figue.EUCLIDIAN_DISTANCE, figue.figue.SINGLE_LINKAGE);

// the above code is from figue.js, I have however modified the library so that it is module and works in nodejs

// the code below is the adapter

function recursive(json) {
    var str;
    if (json.left !== null || json.right !== null) {
        str = {
            name: '',
            y: json.dist,
            children: []
        };
        if (json.left !== null) {
            str.children.push(recursive(json.left));
        }
        if (json.right !== null) {
            str.children.push(recursive(json.right));
        }
    } else {

        str = {
            name: json.label,
            y: json.dist
        };
    }
    return str;
}

var json = recursive(root);

// there we go, now we can use the json variable as an argument for the visualization

console.log(JSON.stringify(json));

结果如下!