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
:
- Import the required library:
import Data.Clustering.Hierarchical
- Define a Point data type:
data Point = Point [Double] deriving Show
- Define the Euclidian distance metric:
dist :: Point -> Point -> Distance dist (Point a) (Point b) = sqrt $ sum $ map (^2) $ zipWith...