Suppose you know the distances between cities but have no map. Multidimensional scaling (MDS) places the cities in a low-dimensional coordinate space so their plotted relationships reflect those distances. More generally, MDS reduces dimensionality while trying to preserve distances or similarities between data points. section 6.2
Choose the variant according to what the dissimilarities mean:
- Classical MDS uses Euclidean distances, with original and fitted distances expressed in the same metric. section 6.9
- Metric MDS allows a transformation of dissimilarities into fitted Euclidean distances. section 6.2.2
- Nonmetric MDS uses distance ranks rather than absolute values. It suits judgments that establish which pairs are closer without supplying reliable measured distances. section 6.2.2
MDS first establishes relationships between points, then seeks coordinates that represent them in fewer dimensions. Stress is the cost used to assess how well the fitted configuration preserves those relationships. Try several numbers of components and inspect a stress-versus-components plot for an elbow rather than assuming that a two-dimensional picture is adequate. Optimization can be computationally intensive and can settle at a local minimum. section 6.2.2 section 6.9
Visualize the Iris data
Load Iris features with scikit-learn, scale them with MinMaxScaler, and fit sklearn.manifold.MDS with three components and a fixed random_state for reproducibility. Color a scatter plot by Iris class. The illustrated code plots only the first two fitted coordinates, so that picture is a two-coordinate view of a three-component result. This quick visualization does not test alternative component counts or optimize stress. section 6.3
Check what a city-distance plot actually uses
The five-city exercise constructs a distance table, scales it, fits MDS for component counts from one through nine, and plots the resulting stress values before labeling the cities. Its code passes the scaled table directly to MDS. Thus each city is supplied as a row of features; the fit does not explicitly designate the table entries as precomputed pairwise dissimilarities. Interpret the output as a plot of the supplied rows, not automatically as a reconstruction of the original distance table. The final fit uses three components but displays only the first two. section 6.3
MDS_stress = []
for i in range(1, 10):
mds = MDS(n_components=i)
pts = mds.fit_transform(df_scaled)
MDS_stress.append(mds.stress_)
This loop records fitted stress across component counts. Inspect its input, df_scaled, when deciding which relationships the fitted coordinates represent. section 6.3
Start with PCA for a dimensionality-reduction problem and move to a more advanced method when it does not address the need. For practice, apply MDS to a vehicles dataset and compare clustering performance before and after reduction; use both the embedding and the downstream result to judge whether it helped. section 6.0 section 6.8
