Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Error for cosine affinity when zero vectors present #7943

Merged
merged 5 commits into from Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions sklearn/cluster/hierarchical.py
Expand Up @@ -379,6 +379,10 @@ def linkage_tree(X, connectivity=None, n_components=None,
'Unknown linkage option, linkage should be one '
'of %s, but %s was given' % (linkage_choices.keys(), linkage))

if affinity == 'cosine' and np.any(~np.any(X, axis=1)):
raise ValueError(
'Cosine affinity cannot be used when X contains zero vectors')

if connectivity is None:
from scipy.cluster import hierarchy # imports PIL

Expand Down
9 changes: 9 additions & 0 deletions sklearn/cluster/tests/test_hierarchical.py
Expand Up @@ -115,6 +115,15 @@ def test_height_linkage_tree():
assert_true(len(children) + n_leaves == n_nodes)


def test_zero_cosine_linkage_tree():
# Check that zero vectors in X produce an error when
# 'cosine' affinity is used
X = np.array([[0, 1],
[0, 0]])
msg = 'Cosine affinity cannot be used when X contains zero vectors'
assert_raise_message(ValueError, msg, linkage_tree, X, affinity='cosine')


def test_agglomerative_clustering():
# Check that we obtain the correct number of clusters with
# agglomerative clustering.
Expand Down