Skip to content
Browse files

ENH slight improvement to mds speed, modified examples to not run mds…

… that long.
  • Loading branch information...
1 parent 3e5f482 commit e3369c189bc4cd4ad39d481ac40b974e66eaa21a Andreas Mueller committed Jun 3, 2012
Showing with 8 additions and 7 deletions.
  1. +2 −1 examples/manifold/plot_compare_methods.py
  2. +1 −1 examples/manifold/plot_lle_digits.py
  3. +5 −5 sklearn/manifold/mds.py
View
3 examples/manifold/plot_compare_methods.py
@@ -81,7 +81,8 @@
t0 = time()
-Y = manifold.MDS(n_components).fit_transform(euclidean_distances(X))
+mds = manifold.MDS(n_components, max_iter=100, n_init=1)
+Y = mds.fit_transform(euclidean_distances(X))
t1 = time()
print "MDS: %.2g sec" % (t1 - t0)
ax = fig.add_subplot(247)
View
2 examples/manifold/plot_lle_digits.py
@@ -171,7 +171,7 @@ def plot_embedding(X, title=None):
#----------------------------------------------------------------------
# MDS embedding of the digits dataset
print "Computing MDS embedding"
-clf = manifold.MDS(n_components=2, n_init=2)
+clf = manifold.MDS(n_components=2, n_init=1, max_iter=100)
t0 = time()
X_mds = clf.fit_transform(euclidean_distances(X))
print "Done. Stress: %f" % clf.stress_
View
10 sklearn/manifold/mds.py
@@ -142,7 +142,7 @@ def _smacof_single(similarities, metric=True, n_components=2, init=None,
if np.any((similarities - similarities.T) > eps):
raise ValueError("similarities must be symmetric")
- sim_flat = ((1 - np.tri(n_samples)) * similarities).flatten()
+ sim_flat = ((1 - np.tri(n_samples)) * similarities).ravel()
sim_flat_w = sim_flat[sim_flat != 0]
if init is None:
# Randomly choose initial configuration
@@ -164,7 +164,7 @@ def _smacof_single(similarities, metric=True, n_components=2, init=None,
if metric:
disparities = similarities
else:
- dis_flat = dis.flatten()
+ dis_flat = dis.ravel()
# similarities with 0 are considered as missing values
dis_flat_w = dis_flat[sim_flat != 0]
@@ -178,12 +178,12 @@ def _smacof_single(similarities, metric=True, n_components=2, init=None,
(disparities ** 2).sum())
# Compute stress
- stress = ((dis.flatten() - \
- disparities.flatten()) ** 2).sum() / 2
+ stress = ((dis.ravel() - \
+ disparities.ravel()) ** 2).sum() / 2
# Update X using the Guttman transform
+ dis[dis == 0] = 1e-5
ratio = disparities / dis
- ratio[np.isinf(ratio) | np.isnan(ratio)] = 0
B = - ratio
B[np.arange(len(B)), np.arange(len(B))] += ratio.sum(axis=1)
X = 1. / n_samples * np.dot(B, X)

0 comments on commit e3369c1

Please sign in to comment.
Something went wrong with that request. Please try again.