Exercise: Dimensionality Reduction in Plotly

Practice with some lab exercises.

We'll cover the following

Exercise 1

Take the matrix XX, scale the data, apply principal component analysis and plot the first two principal components using Plotly Express.

Solution

This code snippet creates a PCA (principal component analysis) pipeline, transforms the input data using this pipeline, and visualizes the first two principal components using a scatter plot. Here’s an overview of the different parts of the code:

  • A pipeline is created using the make_pipeline() function on line 2 from the sklearn.pipeline module. The pipeline consists of two steps: standardizing the data using StandardScaler() from the sklearn.preprocessing module and applying PCA using PCA() from the sklearn.decomposition module.

  • The input data X is transformed using the created pca_pipeline by calling the fit_transform() method on line 5. The resulting transformed data is stored in the transformed_data variable.

  • A scatter plot is created using the px.scatter() function on line 8 from the plotly.express module. The x-axis represents the first principal component (transformed_data[:, 0]), and the y-axis represents the second principal component (transformed_data[:, 1]). The data points are colored based on the ‘Class’ column of the songs DataFrame.

  • The update_layout() function on line 13 is called on the fig object to set the title and axis titles for the scatter plot. Finally, the show() function (Line 15) is called to display the generated PCA plot.

Get hands-on with 1200+ tech skills courses.