Generate a 3D dataset and perform multidimensional scaling (MDS) using SciPy
9. Multidimensional Scaling (MDS)
Write a Numpy program to generate a 3D dataset and perform multidimensional scaling (MDS) using SciPy.
Sample Solution:
Python Code:
Output:
Original 3D Dataset: [[0.5488135 0.71518937 0.60276338] [0.54488318 0.4236548 0.64589411] [0.43758721 0.891773 0.96366276] [0.38344152 0.79172504 0.52889492] [0.56804456 0.92559664 0.07103606] [0.0871293 0.0202184 0.83261985] [0.77815675 0.87001215 0.97861834] [0.79915856 0.46147936 0.78052918] [0.11827443 0.63992102 0.14335329] [0.94466892 0.52184832 0.41466194]] Transformed 2D Dataset using MDS: [[-0.00549785 -0.09249204] [-0.05957237 0.17530568] [-0.34750737 -0.25221217] [ 0.11576864 -0.18536257] [ 0.4561849 -0.41964991] [-0.33109392 0.73362778] [-0.42005493 -0.34160784] [-0.26778615 0.1395934 ] [ 0.62383177 -0.04708346] [ 0.23572727 0.28988113]]
Explanation:
- Import Libraries:
- Import the necessary libraries: NumPy for array creation and manipulation, SciPy for distance calculations, scikit-learn for Multidimensional Scaling (MDS), and matplotlib for plotting.
- Generate 3D Dataset:
- Generate a 3D dataset with 10 points using "np.random.rand()". Seed the random number generator for reproducibility.
- Compute Distance Matrix:
- Compute the pairwise Euclidean distance matrix using pdist and squareform from SciPy.
- Perform Multidimensional Scaling (MDS):
- Use MDS from scikit-learn to transform the distance matrix into a 2D dataset. Set n_components to 2 to reduce the dimensionality to 2D and dissimilarity to 'precomputed' to use the precomputed distance matrix.
- Print Results:
- Print the original 3D dataset and the transformed 2D dataset to verify the transformation.
- Plot the Original and Transformed Datasets:
- Plot the original 3D dataset using a 3D scatter plot and the transformed 2D dataset using a 2D scatter plot with matplotlib.
- Finally display the plots using plt.show().
For more Practice: Solve these Related Problems:
- Write a Numpy program to generate a 3D dataset and reduce its dimensionality to 2D using SciPy's MDS implementation.
- Write a Numpy program to compare the Euclidean distances before and after applying multidimensional scaling on a dataset.
- Write a Numpy program to perform MDS on a dataset and then cluster the resulting low-dimensional points.
- Write a Numpy program to implement a custom stress function and optimize it using SciPy's MDS approach.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.Previous: Ordinary differential equations with NumPy and SciPy.
Next: Create and operate on a large Sparse matrix using SciPy's Sparse module.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics