anchor.diff:  (**
   ##Hierarchical clustering
   
  -
  -
  -*)
  -
  -//let htmp = HierarchicalClustering.generate' DistanceMetrics.euclidean Linker.wardLwLinker data
  -// slower
  -
  -let htmp = 
  -    HierarchicalClustering.generate DistanceMetrics.euclidean Linker.wardLwLinker data
  +Hierarchical clustering results in a tree structure, that has a single cluster (node) on its
  +root and recursevely splits up into clusters of elements that are more similar to each other than to elements
  +of other clusters. For generating multiple cluster results with different number of clusters, the clustering 
  +has to performed only once. Subsequently a threshold can be determined which will result in the desired number of clusters.
  +
  +*)
  +
  +open FSharp.Stats.ML.Unsupervised.HierarchicalClustering
  +#time
  +
  +// calculates the clustering and reports a single root cluster (node), 
  +// that may recursively contains further nodes
  +let clusterResultH = 
  +    HierarchicalClustering.generate DistanceMetrics.euclideanNaNSquared Linker.wardLwLinker data true
  +    |> fun w -> 1.
  +
  +let clusterResultH = 
  +    HierarchicalClustering.generateFloat DistanceMetrics.euclideanNaNSquared Linker.wardLwLinker data
  +    |> fun w -> 1.
  +
  +
  +euclideanNaNSquared 11:00 14:00 
  +// If a desired cluster number is specified, the following function cuts the cluster according
  +// to the depth, that results in the respective number of clusters (here 3). Only leaves are reported.
  +let threeClustersH = HierarchicalClustering.cutHClust 3 clusterResultH
  +
  +(**
  +
  +Every cluster leaf contains its raw values and an index that 
  +indicates the position of the respective data point in the raw data.
  +The index can be retrieved from leaves by HierarchicalClustering.getClusterId.
  +*)
  +
  +let inspectThreeClusters =
  +    threeClustersH
  +    |> List.map (fun cluster -> 
  +        cluster
  +        |> List.map (fun leaf -> 
  +            lables.[HierarchicalClustering.getClusterId leaf]
  +            )
  +        )
  +    |> fun clusteredLabels -> 
  +        sprintf "Detailed information for %i clusters is given:" clusteredLabels.Length,clusteredLabels
  +
  +(*** include-value:inspectThreeClusters ***)
  +
  +
  +// To recursevely flatten the cluster tree into leaves only, use flattenHClust.
  +// A leaf list is reported, that does not contain any cluster membership, 
  +// but is sorted by the clustering result.
  +let hLeaves = 
  +    clusterResultH
       |> HierarchicalClustering.flattenHClust
   
  -let hlable =    
  -    htmp
  -    |> Seq.map (fun c -> lable.[HierarchicalClustering.getClusterId c])
  -
  -let hdata =    
  -    htmp
  -    |> Seq.choose (fun c -> HierarchicalClustering.tryGetLeafValue c)
  -
  -
  -let hierClusteredData = 
  -    let labels = hlable |> Seq.mapi (fun i s -> sprintf "%s%i" s i)
  -    Chart.Heatmap(hdata,ColNames=colnames,RowNames=labels,Colorscale=colorscaleValue,Showscale=true)
  +// takes the sorted cluster result and reports a tuple of lable and data value.
  +let dataSortedByClustering =    
  +    hLeaves
  +    |> Seq.choose (fun c -> 
  +        let lable  = lables.[HierarchicalClustering.getClusterId c]
  +        let values = HierarchicalClustering.tryGetLeafValue c
  +        match values with
  +        | None -> None
  +        | Some x -> Some (lable,x)
  +        )
  +
  +
  +let hierClusteredDataHeatmap = 
  +    let (hlable,hdata) =
  +        dataSortedByClustering
  +        |> Seq.unzip
  +    Chart.Heatmap(hdata,ColNames=colnames,RowNames=hlable,Colorscale=colorscaleValue,Showscale=true)
       |> Chart.withMarginSize(Left=250.)
  -    |> Chart.withTitle "clustered iris data (hierarchical Clustering)"
  -(*** include-value:hierClusteredData ***)
  -
  -
  +    |> Chart.withTitle "Clustered iris data (hierarchical clustering)"
  +
  +(*** include-value:hierClusteredDataHeatmap ***)
   
   (**
   
  
positive.code: (**
  #Clustering
  
  ##Iterative Clustering
  
  ###k-means clustering
  *)
  
  open FSharp.Stats
  
negative.code:     <| data <| 4
  
  let clusteredIrisData =
      Array.zip lables data
      |> Array.sortBy (fun (l,dataPoint) -> fst (kmeansResult.Classifier dataPoint)) 
      |> Array.unzip
      |> fun (labels,d) -> 
          Chart.Heatmap(d,ColNames=colnames,RowNames=labels,Colorscale=colorscaleValue,Showscale=true)
          |> Chart.withMarginSize(Left=250.)
          |> Chart.withTitle "clustered iris data (k-means clustering)"
  