Exercise: Dimensionality Reduction in Plotly
Practice with some lab exercises.
We'll cover the following
Exercise 1
Take the matrix , 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 thesklearn.pipeline
module. The pipeline consists of two steps: standardizing the data usingStandardScaler()
from thesklearn.preprocessing
module and applying PCA usingPCA()
from thesklearn.decomposition
module. -
The input data
X
is transformed using the createdpca_pipeline
by calling thefit_transform()
method on line 5. The resulting transformed data is stored in thetransformed_data
variable. -
A scatter plot is created using the
px.scatter()
function on line 8 from theplotly.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 thesongs
DataFrame. -
The
update_layout()
function on line 13 is called on thefig
object to set the title and axis titles for the scatter plot. Finally, theshow()
function (Line 15) is called to display the generated PCA plot.
Get hands-on with 1400+ tech skills courses.