@@ -485,6 +485,17 @@ def plot(self, axis=None, truncate_mode=None, p=0, vary_line_width=True):
485
485
486
486
return axis
487
487
488
+ def to_numpy (self ):
489
+ """Return a numpy array representation of the single linkage tree.
490
+
491
+ This representation conforms to the scipy.cluster.hierarchy notion
492
+ of a single linkage tree, and can be used with all the associated
493
+ scipy tools. Please see the scipy documentation for more details
494
+ on the format.
495
+ """
496
+ return self ._linkage .copy ()
497
+
498
+
488
499
def to_pandas (self ):
489
500
"""Return a pandas dataframe representation of the single linkage tree.
490
501
@@ -510,15 +521,15 @@ def to_pandas(self):
510
521
max_node = 2 * self ._linkage .shape [0 ]
511
522
num_points = max_node - (self ._linkage .shape [0 ] - 1 )
512
523
513
- parent_array = np .arange (num_points , max_node )
524
+ parent_array = np .arange (num_points , max_node + 1 )
514
525
515
526
result = DataFrame ({
516
527
'parent' : parent_array ,
517
- 'left_child' : self ._linkage [0 ],
518
- 'right_child' : self ._linkage [1 ],
519
- 'distance' : self ._linkage [2 ],
520
- 'size' : self ._linkage [3 ]
521
- })
528
+ 'left_child' : self ._linkage . T [0 ],
529
+ 'right_child' : self ._linkage . T [1 ],
530
+ 'distance' : self ._linkage . T [2 ],
531
+ 'size' : self ._linkage . T [3 ]
532
+ })[[ 'parent' , 'left_child' , 'right_child' , 'distance' , 'size' ]]
522
533
523
534
return result
524
535
0 commit comments