MulaiMulai sekarang secara gratis

Hierarchies of stocks

In chapter 1, you used k-means clustering to cluster companies according to their stock price movements. Now, you'll perform hierarchical clustering of the companies. You are given a NumPy array of price movements movements, where the rows correspond to companies, and a list of the company names companies. SciPy hierarchical clustering doesn't fit into a sklearn pipeline, so you'll need to use the normalize() function from sklearn.preprocessing instead of Normalizer.

linkage and dendrogram have already been imported from scipy.cluster.hierarchy, and PyPlot has been imported as plt.

Latihan ini adalah bagian dari kursus

Unsupervised Learning in Python

Lihat Kursus

Petunjuk latihan

  • Import normalize from sklearn.preprocessing.
  • Rescale the price movements for each stock by using the normalize() function on movements.
  • Apply the linkage() function to normalized_movements, using 'complete' linkage, to calculate the hierarchical clustering. Assign the result to mergings.
  • Plot a dendrogram of the hierarchical clustering, using the list companies of company names as the labels. In addition, specify the leaf_rotation=90, and leaf_font_size=6 keyword arguments as you did in the previous exercise.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import normalize
____

# Normalize the movements: normalized_movements
normalized_movements = ____

# Calculate the linkage: mergings
mergings = ____

# Plot the dendrogram
____
plt.show()
Edit dan Jalankan Kode