Which stocks move together?
In the previous exercise, you clustered companies by their daily stock price movements. So which company have stock prices that tend to change in the same way? You'll now inspect the cluster labels from your clustering to find out.
Your solution to the previous exercise has already been run. Recall that you constructed a Pipeline pipeline
containing a KMeans
model and fit it to the NumPy array movements
of daily stock movements. In addition, a list companies
of the company names is available.
This exercise is part of the course
Unsupervised Learning in Python
Exercise instructions
- Import
pandas
aspd
. - Use the
.predict()
method of the pipeline to predict the labels formovements
. - Align the cluster labels with the list of company names
companies
by creating a DataFramedf
withlabels
andcompanies
as columns. This has been done for you. - Use the
.sort_values()
method ofdf
to sort the DataFrame by the'labels'
column, and print the result. - Hit submit and take a moment to see which companies are together in each cluster!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import pandas
import pandas as pd
# Predict the cluster labels: labels
labels = ____
# Create a DataFrame aligning labels and companies: df
df = pd.DataFrame({'labels': labels, 'companies': companies})
# Display df sorted by cluster label
print(____)