Aan de slagGa gratis aan de slag

Clustering stocks using KMeans

In this exercise, you'll cluster companies using their daily stock price movements (i.e. the dollar difference between the closing and opening prices for each trading day). You are given a NumPy array movements of daily price movements from 2010 to 2015 (obtained from Yahoo! Finance), where each row corresponds to a company, and each column corresponds to a trading day.

Some stocks are more expensive than others. To account for this, include a Normalizer at the beginning of your pipeline. The Normalizer will separately transform each company's stock price to a relative scale before the clustering begins.

Note that Normalizer() is different to StandardScaler(), which you used in the previous exercise. While StandardScaler() standardizes features (such as the features of the fish data from the previous exercise) by removing the mean and scaling to unit variance, Normalizer() rescales each sample - here, each company's stock price - independently of the other.

KMeans and make_pipeline have already been imported for you.

Deze oefening maakt deel uit van de cursus

Unsupervised Learning in Python

Cursus bekijken

Oefeninstructies

  • Import Normalizer from sklearn.preprocessing.
  • Create an instance of Normalizer called normalizer.
  • Create an instance of KMeans called kmeans with 10 clusters.
  • Using make_pipeline(), create a pipeline called pipeline that chains normalizer and kmeans.
  • Fit the pipeline to the movements array.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Import Normalizer
____

# Create a normalizer: normalizer
normalizer = ____

# Create a KMeans model with 10 clusters: kmeans
kmeans = ____

# Make a pipeline chaining normalizer and kmeans: pipeline
pipeline = ____

# Fit pipeline to the daily price movements
____
Code bewerken en uitvoeren