scikit-learn / scikit-learn

scikit-learn: machine learning in Python
https://scikit-learn.org
BSD 3-Clause "New" or "Revised" License
60.11k stars 25.4k forks source link

Make Isomap to work with any precomputed neighborhood graph, at least radius_neighbors_graph #19744

Open oleg-kachan opened 3 years ago

oleg-kachan commented 3 years ago

Describe the workflow you want to enable

Now Isomap assumes only k-nearest neighbors neighborhoods, even if supplied with precomputed neighborhood graph, such as radius_neighbors_graph which by definition have an arbitrary number of neighbors for each point (those which are within provided eps-radius).

When supplied with precomputed matrix of eps-radius nearest neighbors Isomap gives error:

from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
from sklearn.manifold import MDS, Isomap

from sklearn.neighbors import radius_neighbors_graph

X, y = load_digits(return_X_y=True)
X = StandardScaler().fit_transform(X)

X, y = load_digits(return_X_y=True)
A_sparse = radius_neighbors_graph(X, radius=5.5, mode="distance")
isomap = Isomap(metric="precomputed")
isomap.fit(A_sparse)

ValueError: 6 neighbors per samples are required, but some samples have only 0 neighbors in precomputed graph matrix. Decrease number of neighbors used or recompute the graph with more neighbors.

Describe your proposed solution

Make Isomap work with supplied precomputed neighborhood graphs, with an arbitrary number of nearest neighbors.

TomDLT commented 3 years ago

Isomap is based on the computation of pairwise shortest paths on a graph. The graph is currently computed using a k-nearest neighbors method, but I don't see a reason not to accept radius-based nearest neighbors graphs. We could add a radius parameter, switching between k-neighbors and radius-based neighbors when radius=None or n_neighbors=None.

MaxwellLZH commented 3 years ago

take