Skip to content

TSNE in t_sne.py Replaced nan with 0 in distance matrix. Issue #4475 #4492

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sklearn/manifold/t_sne.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ def fit(self, X, y=None):
else:
distances = pairwise_distances(X, metric=self.metric)

# Replacing any nan entries with zero to avoid undefined distances
distances = np.nan_to_num(distances)

# Degrees of freedom of the Student's t-distribution. The suggestion
# alpha = n_components - 1 comes from "Learning a Parametric Embedding
# by Preserving Local Structure" Laurens van der Maaten, 2009.
Expand Down
16 changes: 16 additions & 0 deletions sklearn/manifold/tests/test_t_sne.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,19 @@ def test_reduction_to_one_component():
X = random_state.randn(5, 2)
X_embedded = tsne.fit_transform(X)
assert(np.all(np.isfinite(X_embedded)))

def test_undefined_correlation():
# t-SNE throws an exception with undefined correlation (issue #4475)
from sklearn.manifold import TSNE
import numpy as np
np.random.seed(42)

data = np.random.rand(10, 3)
data[-1, :] = 0

try:
model = TSNE(metric="correlation")
model.fit_transform(data)
assert True
except ValueError as e:
assert False