本文整理汇总了Python中scipy.cluster.hierarchy.to_tree方法的典型用法代码示例。如果您正苦于以下问题:Python hierarchy.to_tree方法的具体用法?Python hierarchy.to_tree怎么用?Python hierarchy.to_tree使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.cluster.hierarchy
的用法示例。
在下文中一共展示了hierarchy.to_tree方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cluster
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def cluster(self,maxClust):
D = self.corrDist()
Z = linkage(D[np.triu_indices(self.n, 1)]) # create a linkage matrix based on the distance matrix
if maxClust < 1:
maxClust = 1
if maxClust > self.n:
maxClust = self.n
map = self.__breakClust__(to_tree(Z),maxClust)
return map
# a recursive helper function which breaks down the dendrogram branches until all clusters have no more than maxClust elements
示例2: get_clustering_as_tree
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def get_clustering_as_tree(vectors, linkage=constants.linkage_method_default, distance=constants.distance_metric_default, progress=progress):
is_distance_and_linkage_compatible(distance, linkage)
progress.update('Clustering data with "%s" linkage using "%s" distance' % (linkage, distance))
linkage = hierarchy.linkage(vectors, metric=distance, method=linkage)
progress.update('Recovering the tree from the clustering result')
tree = hierarchy.to_tree(linkage, rd=False)
return tree
示例3: hct_from_lkg
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def hct_from_lkg(hac_z):
return HClustTree(sch.to_tree(hac_z))
示例4: test_mirac_wrong_args
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def test_mirac_wrong_args(self):
x = np.zeros((10, 10))
# wrong min_cl_n
with pytest.raises(ValueError) as excinfo:
cluster.MIRAC(x, metric='euclidean', min_cl_n=-0.1)
with pytest.raises(ValueError) as excinfo:
cluster.MIRAC(x, metric='euclidean', min_cl_n=-0.1)
# wrong cl_mdl_scale_factor
with pytest.raises(ValueError) as excinfo:
cluster.MIRAC(x, metric='euclidean', cl_mdl_scale_factor=-0.1)
# wrong encode type
with pytest.raises(ValueError) as excinfo:
cluster.MIRAC(x, metric='euclidean', encode_type='1')
with pytest.raises(ValueError) as excinfo:
cluster.MIRAC(x, metric='euclidean', encode_type=1)
with pytest.raises(ValueError) as excinfo:
cluster.MIRAC(x, metric='euclidean', dim_reduct_method='NONN')
# hac tree n_leaves different from n_samples
z = sch.linkage([[0], [5], [6], [8], [9], [12]],
method='single', optimal_ordering=True)
hct = eda.HClustTree(sch.to_tree(z))
with pytest.raises(ValueError) as excinfo:
cluster.MIRAC(x, metric='euclidean', hac_tree=hct)
# no specific purpose. Just to exaust the coverage
示例5: test_bi_partition_min_no_spl
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def test_bi_partition_min_no_spl(self):
# ____|____ 6
# | ___|____ 5
# | | __|___ 4
# | | | |
# 3 2 1 0
z = sch.linkage([[0, 0], [1, 1], [3, 3], [6, 6]],
metric='euclidean', method='complete',
optimal_ordering=True)
hct = eda.HClustTree(sch.to_tree(z))
assert hct.leaf_ids() == [3, 2, 1, 0]
labs, sids, lst, rst = hct.bi_partition(
soft_min_subtree_size=2, return_subtrees=True)
assert labs == [0, 0, 1, 1]
assert sids == [3, 2, 1, 0]
# hct should be changed accordingly
assert hct.leaf_ids() == [3, 2, 1, 0]
assert hct.left_leaf_ids() == [3, 2]
assert hct.right_leaf_ids() == [1, 0]
# subtrees
assert lst.leaf_ids() == [3, 2]
assert rst.leaf_ids() == [1, 0]
# prev
assert lst._prev is hct
assert rst._prev is hct
# ids
assert lst._node.id == 5
assert lst._node.left.id == 3
assert lst._node.right.id == 2
# ids
assert rst._node.id == 4
assert rst._node.left.id == 1
assert rst._node.right.id == 0
示例6: test_bi_partition_min_no_spl_lr_rev
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def test_bi_partition_min_no_spl_lr_rev(self):
# left right reversed
# ____|____ 6
# | ___|____ 5
# | | __|___ 4
# | | | |
# 3 2 1 0
z = sch.linkage([[0, 0], [1, 1], [3, 3], [6, 6]],
metric='euclidean', method='complete',
optimal_ordering=True)
root = sch.to_tree(z)
# reverse left right subtree
root_left = root.left
root.left = root.right
root.right = root_left
hct = eda.HClustTree(root)
assert hct.leaf_ids() == [2, 1, 0, 3]
labs, sids, lst, rst = hct.bi_partition(
soft_min_subtree_size=2, return_subtrees=True)
assert labs == [0, 0, 1, 1]
assert sids == [2, 1, 0, 3]
# hct should be changed accordingly
assert hct.leaf_ids() == [2, 1, 0, 3]
assert hct.left_leaf_ids() == [2, 1]
assert hct.right_leaf_ids() == [0, 3]
# subtrees
assert lst.leaf_ids() == [2, 1]
assert rst.leaf_ids() == [0, 3]
# prev
assert lst._prev is hct
assert rst._prev is hct
assert hct._left is lst._node
assert hct._right is rst._node
# ids
assert rst._node.id == 4
assert rst._node.left.id == 0
assert rst._node.right.id == 3
# ids
assert lst._node.id == 5
assert lst._node.left.id == 2
assert lst._node.right.id == 1
示例7: test_bi_partition_min_spl
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def test_bi_partition_min_spl(self):
# _____|_____
# | ____|____
# | __|__ __|__
# | | | | |
# 4 3 2 1 0
z = sch.linkage([[0, 0], [1, 1], [3, 3], [4, 4], [10, 10]],
metric='euclidean', method='complete',
optimal_ordering=True)
hct = eda.HClustTree(sch.to_tree(z))
assert hct.leaf_ids() == [4, 3, 2, 1, 0]
assert hct.left_leaf_ids() == [4]
assert hct.right().left().leaf_ids() == [3, 2]
assert hct.right().right().leaf_ids() == [1, 0]
labs, sids, lst, rst = hct.bi_partition(
soft_min_subtree_size=2, return_subtrees=True)
assert labs == [0, 0, 0, 1, 1]
assert sids == [4, 3, 2, 1, 0]
# hct should be changed accordingly
assert hct.leaf_ids() == [4, 3, 2, 1, 0]
assert hct.left_leaf_ids() == [4, 3, 2]
assert hct.right_leaf_ids() == [1, 0]
# left
assert lst._prev is hct
assert lst._node.left.left.id == 4
assert lst._node.left.right.id == 3
assert lst._node.right.id == 2
# right
assert rst._prev is hct
assert rst._node.left.id == 1
assert rst._node.right.id == 0
示例8: test_bi_partition_min_switch_spl
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def test_bi_partition_min_switch_spl(self):
# _______|________
# | _____|_____
# | ____|____ |
# | __|__ __|__ |
# | | | | | |
# 0 1 2 3 4 5
# round 1: ( ((0, (1, 2)), (3, 4)), (5) )
# round 2: ( (0, (1, 2), (3, (4, 5)) )
z = sch.linkage([[0], [5], [6], [8], [9], [12]],
method='single', optimal_ordering=True)
root = sch.to_tree(z)
assert root.left.id == 0
assert root.right.right.id == 5
assert root.right.left.left.left.id == 1
assert root.right.left.left.right.id == 2
assert root.right.left.right.left.id == 3
assert root.right.left.right.right.id == 4
hct = eda.HClustTree(root)
labs, sids, lst, rst = hct.bi_partition(
soft_min_subtree_size=3, return_subtrees=True)
assert labs == [0, 0, 0, 1, 1, 1]
assert sids == [0, 1, 2, 3, 4, 5]
# lst
assert hct._left is lst._node
assert lst._prev is hct
assert lst.left_leaf_ids() == [0]
assert lst.right_leaf_ids() == [1, 2]
# rst
assert hct._right is rst._node
assert rst._prev is hct
assert rst.left_leaf_ids() == [3]
assert rst.right_leaf_ids() == [4, 5]
示例9: run
# 需要导入模块: from scipy.cluster import hierarchy [as 别名]
# 或者: from scipy.cluster.hierarchy import to_tree [as 别名]
def run(self, pairwise_value_file,
method,
similarity,
max_sim_value,
name_col1,
name_col2,
value_col,
output_tree):
"""Perform hierarchical clustering on pairwise value files.
Parameters
----------
pairwise_value_file : str
File with pairwise similarity or dissimilarity values.
method : str
Clustering method to use.
similarity : boolean
Flag indicating file contain similarity values.
max_sim_value : float
Maximum value of similarity scores.
name_col1 : int
Index of first column with genome names.
name_col2 : int
Index of second column with genome names.
value_col : int
Index of column with similarity or dissimilarity values.
"""
diss_vector, genome_labels = self._parse_data(pairwise_value_file,
name_col1,
name_col2,
value_col,
similarity,
max_sim_value)
clusters = hierarchy.linkage(diss_vector, method=method)
tree = hierarchy.to_tree(clusters)
newick_str = self._save_newick(tree, "", tree.dist, genome_labels)
fout = open(output_tree, 'w')
fout.write(newick_str + '\n')
fout.close()