How to perform multidimensional scaling in Python?

By Shivang Yadav Last updated : November 21, 2023

Multidimensional Scaling

Multidimensional Scaling abbreviated as (MDS) is a statistical method used for visualizing the pairwise dissimilarity or similarity between a set of data points in a lower-dimensional space. It's often used in data visualization and dimensionality reduction.

In Python, you can perform MDS using libraries like scikit-learn and the SciPy library.

Let's see an example of multidimensional Scaling in Python.

Example

In this example, we are performing multidimensional scaling.

import pandas as pd
from sklearn.manifold import MDS
import matplotlib.pyplot as plt

myDataFrame = pd.DataFrame(
    {
        "player": ["P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9"],
        "points": [4, 4, 6, 7, 8, 14, 16, 25, 28],
        "assists": [3, 2, 2, 5, 7, 6, 8, 10, 11],
        "blocks": [7, 6, 5, 8, 8, 4, 2, 2, 1],
        "rebounds": [4, 5, 6, 5, 8, 10, 4, 2, 2],
    }
)

myDataFrame = myDataFrame.set_index("player")
print(myDataFrame)

mds = MDS(random_state=0)
scaledDataFrame = mds.fit_transform(myDataFrame)

plt.scatter(scaledDataFrame[:, 0], scaledDataFrame[:, 1])

plt.xlabel("Coordinate 1")
plt.ylabel("Coordinate 2")

for i, txt in enumerate(myDataFrame.index):
    plt.annotate(txt, (scaledDataFrame[:, 0][i] + 0.3, scaledDataFrame[:, 1][i]))

plt.show()

Output

The output of the above program is:

points  assists  blocks  rebounds
player                                   
P1           4        3       7         4
P2           4        2       6         5
P3           6        2       5         6
P4           7        5       8         5
P5           8        7       8         8
P6          14        6       4        10
P7          16        8       2         4
P8          25       10       2         2
P9          28       11       1         2
multidimensional scaling | python example

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.