{"action":"closed","issue":{"url":"https://api.github.com/repos/scipy/scipy/issues/6785","repository_url":"https://api.github.com/repos/scipy/scipy","labels_url":"https://api.github.com/repos/scipy/scipy/issues/6785/labels{/name}","comments_url":"https://api.github.com/repos/scipy/scipy/issues/6785/comments","events_url":"https://api.github.com/repos/scipy/scipy/issues/6785/events","html_url":"https://github.com/scipy/scipy/issues/6785","id":189211034,"number":6785,"title":"ValueError: Linkage 'Z' uses the same cluster more than once thrown when clustering","user":{"login":"lynxoid","id":1205656,"avatar_url":"https://avatars.githubusercontent.com/u/1205656?v=3","gravatar_id":"","url":"https://api.github.com/users/lynxoid","html_url":"https://github.com/lynxoid","followers_url":"https://api.github.com/users/lynxoid/followers","following_url":"https://api.github.com/users/lynxoid/following{/other_user}","gists_url":"https://api.github.com/users/lynxoid/gists{/gist_id}","starred_url":"https://api.github.com/users/lynxoid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lynxoid/subscriptions","organizations_url":"https://api.github.com/users/lynxoid/orgs","repos_url":"https://api.github.com/users/lynxoid/repos","events_url":"https://api.github.com/users/lynxoid/events{/privacy}","received_events_url":"https://api.github.com/users/lynxoid/received_events","type":"User","site_admin":false},"labels":[{"id":37334597,"url":"https://api.github.com/repos/scipy/scipy/labels/defect","name":"defect","color":"e11d21","default":false},{"id":37342878,"url":"https://api.github.com/repos/scipy/scipy/labels/scipy.cluster","name":"scipy.cluster","color":"207de5","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-11-14T20:02:23Z","updated_at":"2016-11-16T15:07:44Z","closed_at":"2016-11-16T15:07:44Z","body":"I compute cophenet index on the Z matrix generated by the `scipy.cluster.hierarchy.linkage` function, but the computation errors out w/ `ValueError: Linkage 'Z' uses the same cluster more than once`. Here is a minimal example:\r\n\r\n```\r\nimport numpy as np\r\n\r\nfrom scipy.cluster.hierarchy import linkage\r\nfrom scipy.spatial.distance import pdist\r\nfrom scipy.cluster.hierarchy import cophenet\r\nfrom scipy.cluster.hierarchy import dendrogram\r\n\r\nmetric = 'hamming'\r\nM = np.loadtxt(\"z_bug_matrix.txt\")\r\nZ = linkage(M, method='average', metric=metric)\r\n# next line throws a \"Linkage 'Z' uses the same cluster more than once.\" error\r\nc, coph_dists = cophenet(Z, pdist(M, metric))\r\n# this also throws the same error:\r\n# dendrogram(\r\n#        Z,\r\n#        truncate_mode='lastp',  # show only the last p merged clusters\r\n#        p=20,                 # show the last 20 merges\r\n#        leaf_rotation=90.,      # rotates the x axis labels\r\n#        leaf_font_size=12.,     # font size for the x axis labels\r\n#        show_contracted=True,    # show item counts in the contracted clusters\r\n#        color_threshold=0.5 # color clusters\r\n#    )\r\n```\r\n\r\nError thrown in cophenet index:\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"test_scipy_z_bug.py\", line 10, in <module>\r\n    c, coph_dists = cophenet(Z, pdist(M, metric))\r\n  File \"/home/users/filippod/.conda/envs/ccs_py35/lib/python3.5/site-packages/scipy/cluster/hierarchy.py\", line 1096, in cophenet\r\n    is_valid_linkage(Z, throw=True, name='Z')\r\n  File \"/home/users/filippod/.conda/envs/ccs_py35/lib/python3.5/site-packages/scipy/cluster/hierarchy.py\", line 1421, in is_valid_linkage\r\n    % name_str)\r\nValueError: Linkage 'Z' uses the same cluster more than once.\r\n```\r\n\r\nError thrown in dendrogram:\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"test_scipy_z_bug.py\", line 20, in <module>\r\n    color_threshold=0.5 # color clusters\r\n  File \"/home/users/filippod/.conda/envs/ccs_py35/lib/python3.5/site-packages/scipy/cluster/hierarchy.py\", line 2227, in dendrogram\r\n    is_valid_linkage(Z, throw=True, name='Z')\r\n  File \"/home/users/filippod/.conda/envs/ccs_py35/lib/python3.5/site-packages/scipy/cluster/hierarchy.py\", line 1421, in is_valid_linkage\r\n    % name_str)\r\nValueError: Linkage 'Z' uses the same cluster more than once.\r\n```\r\n\r\nWhen examining the matrix, I find that an internal node appears twice -- once merged w/ a leaf and once merged w/ an internal node. Since a node is only allowed one parent, this seems to be a bug.\r\n\r\nscipy version: 0.18.1\r\nnumpy version: 1.11.1\r\n\r\nMatrix file is attached (25620 rows).\r\n[z_bug_matrix.txt](https://github.com/scipy/scipy/files/590334/z_bug_matrix.txt)\r\n"}}
{"actor":{"display_login":"nmayorov"}}