K-Means Algorithm Python Example

Premium

In this post, we will provide an example of the implementation of the K-Means algorithm in python.  This K-Means algorithm python example consists of clustering a dataset that contains information of all the stocks that compose the Standard & Poor Index. 

This example contains the following five steps:

  • Obtain the 500 tickers for the SPY & 500 by scrapping the tickers symbols from Wikipedia. The function obtain_parse_wike_snp500() conduct this task.
  • Obtain closes prices from last year for each of the symbols using the Quandl API
  • Calculate mean and variance of the returns for each stock
  • Choose the best k value for the cluster the dataset
  • Fit the model with the k number of cluster

Unlock Premium Content

Upgrade your account to access the full article, downloads, and exercises.

You'll get access to:

  • Access complete tutorials and examples
  • Download source code and resources
  • Follow along with practical exercises
  • Get in-depth explanations