Skip to content

Commit 15d1290

Browse files
committed
Fix for issue #24 with single linkage to pandas ... also added numpy output.
1 parent 6e01fbd commit 15d1290

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

hdbscan/plots.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,17 @@ def plot(self, axis=None, truncate_mode=None, p=0, vary_line_width=True):
485485

486486
return axis
487487

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+
488499
def to_pandas(self):
489500
"""Return a pandas dataframe representation of the single linkage tree.
490501
@@ -510,15 +521,15 @@ def to_pandas(self):
510521
max_node = 2 * self._linkage.shape[0]
511522
num_points = max_node - (self._linkage.shape[0] - 1)
512523

513-
parent_array = np.arange(num_points, max_node)
524+
parent_array = np.arange(num_points, max_node + 1)
514525

515526
result = DataFrame({
516527
'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']]
522533

523534
return result
524535

0 commit comments

Comments
 (0)