2015-04-05 27 views
-1

似乎通过JavaScript进行机器学习处于起步阶段,因为几乎没有任何图书馆适合其他计算智能和可视化。树状图:figue.js(聚类)输出,以适应可视化

我使用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 
} 

我可以说这是很难解决的。还是有人知道任何可能编译结构的库?

+0

所以,你要寻找的figue.js输出之间的适配器,使其在本地d3.js工作? – 2015-04-05 14:53:43

+0

@JaredFarrish事实上,我现在正在写一篇,但它不起作用。适配器是这个词,谢谢。 – 2015-04-05 14:55:28

+0

我没有看到任何可能工作的东西,它本身并不是一个完整的包(如OpenRefine)或某些R来源的数据适配器。我看到其他人也问过同样的问题;如果你开发一个解决方案,我会建议提供这个答案,以便其他人可以受益。 – 2015-04-05 15:40:40

回答

1
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)); 

以下是查看结果!

enter image description here