Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using a hierarchical clustering library


We will group together a list of points using a hierarchical clustering approach. We will start by assuming that each point is its own cluster. The two closest clusters merge together and the algorithm repeats until the stopping criteria is met. In this algorithm, we will use a library to run hierarchical clustering until there are a specific number of clusters remaining.

Getting ready

Install the hierarchical clustering package using cabal as follows (documentation is available at http://hackage.haskell.org/package/hierarchical-clustering):

$ cabal install hierarchical-clustering

How to do it…

Insert the following code in a new file, which we call Main.hs:

  1. Import the required library:

    import Data.Clustering.Hierarchical
  2. Define a Point data type:

    data Point = Point [Double] deriving Show
  3. Define the Euclidian distance metric:

    dist :: Point -> Point -> Distance
    dist (Point a) (Point b) = sqrt $ sum $ map (^2) $ 
                               zipWith (-) a...