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

Implementing hierarchical clustering


Another way to cluster data is by first assuming each data item as its own cluster. We can then take a step back and merge together two of the nearest clusters. This process forms a hierarchy of clusters.

Take, for example, an analogy relating to islands and water level. An island is nothing more than a mountain tip surrounded by water. Imagine we have islands scattered across a sea. If we were to slowly drop the water level of the sea, two nearby small islands would merge into a larger island because they are connected to the same mountain formation. We can stop the water level from dropping any time we have the desired number of larger islands.

How to do it…

In a new file, which we name Main.hs, insert this code:

  1. Import the built-in functions:

    import Data.Map (Map, (!), delete)
    import qualified Data.Map as Map
    import Data.Ord (comparing)
    import Data.List (sort, tails, transpose, minimumBy)
  2. Define a type synonym for points:

    type Point = [Double]
  3. Define a convenience...