sknetwork-team / scikit-network

Graph Algorithms
Other
602 stars 67 forks source link

Dendrogram invalid? #477

Closed tschm closed 3 years ago

tschm commented 3 years ago

Description

Playing with cut_balanced failed

What I Did

Here's my dendrogram

[[ 8.         18.          0.20704904  2.        ]
 [ 0.          2.          0.37775594  2.        ]
 [16.         21.          0.45278056  3.        ]
 [ 4.         22.          0.48408993  4.        ]
 [ 1.         23.          0.49373192  5.        ]
 [ 3.         24.          0.54854167  6.        ]
 [ 9.         17.          0.5539515   2.        ]
 [13.         14.          0.56726755  2.        ]
 [ 5.         10.          0.59249521  2.        ]
 [20.         26.          0.59532007  4.        ]
 [12.         15.          0.61572425  2.        ]
 [ 7.         19.          0.6221304   2.        ]
 [27.         28.          0.62684343  4.        ]
 [11.         30.          0.65255261  3.        ]
 [ 6.         25.          0.65711535  7.        ]
 [31.         33.          0.66109365  5.        ]
 [29.         32.          0.68098031  8.        ]
 [35.         36.          0.79584075 13.        ]
 [34.         37.          1.03229063 20.        ]]

   labels, dendrogram = cut_balanced(dendrogram, max_cluster_size=4, return_dendrogram=True)
    print(dendrogram)
    print(labels)

results in this invalid dendrogram...

[[ 0.          0.          0.62684343  4.        ]
 [ 0.          0.          0.65255261  3.        ]
 [ 7.          0.          0.65711535  7.        ]
 [ 4.          9.          0.66109365  5.        ]
 [ 1.          8.          0.68098031  8.        ]
 [11.         12.          0.79584075 13.        ]
 [10.         13.          1.03229063 20.        ]]
tbonald commented 3 years ago

Many thanks for pointing out this bug.

Now corrected on the develop branch (update get_labels in hierarchy/postprocess.py).

The returned dendrogram is now:

array([[ 0. , 3. , 0.49373192, 5. ], [ 1. , 8. , 0.54854167, 6. ], [ 2. , 9. , 0.65711535, 7. ], [ 5. , 7. , 0.66109365, 5. ], [ 4. , 6. , 0.68098031, 8. ], [11. , 12. , 0.79584075, 13. ], [10. , 13. , 1.03229063, 20. ]])