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] Make OPTICS support sparse input #14736

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions sklearn/cluster/optics_.py
Expand Up @@ -194,7 +194,7 @@ class OPTICS(BaseEstimator, ClusterMixin):
the Conference "Lernen, Wissen, Daten, Analysen" (LWDA) (2018): 318-329.
"""

def __init__(self, min_samples=5, max_eps=np.inf, metric='minkowski', p=2,
def __init__(self, min_samples=5, max_eps=np.inf, metric='euclidean', p=2,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this won't work if the user changes p. So I'd rather reinterpret the metric as euclidean below when using the metric, rather than changing the default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to add commits to resolve it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will resolve this issue and add test cases this week.

metric_params=None, cluster_method='xi', eps=None, xi=0.05,
predecessor_correction=True, min_cluster_size=None,
algorithm='auto', leaf_size=30, n_jobs=None):
Expand Down Expand Up @@ -233,7 +233,7 @@ def fit(self, X, y=None):
self : instance of OPTICS
The instance.
"""
X = check_array(X, dtype=np.float)
X = check_array(X, accept_sparse='csr')

if self.cluster_method not in ['dbscan', 'xi']:
raise ValueError("cluster_method should be one of"
Expand Down Expand Up @@ -517,7 +517,7 @@ def _set_reach_dist(core_distances_, reachability_, predecessor_,
# the same logic as neighbors, p is ignored if explicitly set
# in the dict params
_params['p'] = p
dists = pairwise_distances(P, np.take(X, unproc, axis=0),
dists = pairwise_distances(P, X[unproc],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.take doesn't support the sparse matrix

metric, n_jobs=None,
**_params).ravel()

Expand Down
4 changes: 2 additions & 2 deletions sklearn/cluster/tests/test_optics.py
Expand Up @@ -352,7 +352,7 @@ def test_compare_to_ELKI():
# Tests against known extraction array
# Does NOT work with metric='euclidean', because sklearn euclidean has
# worse numeric precision. 'minkowski' is slower but more accurate.
clust1 = OPTICS(min_samples=5).fit(X)
clust1 = OPTICS(metric='minkowski', min_samples=5).fit(X)

assert_array_equal(clust1.ordering_, np.array(o1))
assert_array_equal(clust1.predecessor_[clust1.ordering_], np.array(p1))
Expand Down Expand Up @@ -386,7 +386,7 @@ def test_compare_to_ELKI():
11, 19, 15, 10, 47, -1, 20, 22, 25, 25, 25, 25, 22, 22, 23, -1, 30,
30, 34, 34, 34, 32, 32, 37, 38, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1]
clust2 = OPTICS(min_samples=5, max_eps=0.5).fit(X)
clust2 = OPTICS(metric='minkowski', min_samples=5, max_eps=0.5).fit(X)

assert_array_equal(clust2.ordering_, np.array(o2))
assert_array_equal(clust2.predecessor_[clust2.ordering_], np.array(p2))
Expand Down