O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Clustering Dow Jones stocks with scikits-learn

Clustering is a type of machine learning algorithm that aims to group items based on similarities. In this example, we will use the log returns of stocks in the Dow Jones Industrial Average (DJI or DJIA) index to cluster. Most of the steps of this recipe have already passed the review in previous chapters.

How to do it...

First, we will download the EOD price data for those stocks from Yahoo! Finance. Then, we will calculate a square affinity matrix. Finally, we will cluster the stocks with the AffinityPropagation class:

  1. Download price data for 2011 using the stock symbols of the DJI Index. In this example, we are only interested in the close price:
    # 2011 to 2012 start = datetime.datetime(2011, 01, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required