module Indexable(T)

Overview

A container that allows accessing elements via a numeric index.

Indexing starts at 0. A negative index is assumed to be relative to the end of the container: -1 indicates the last element, -2 is the next to last element, and so on.

Types including this module are typically Array-like types.

Stability guarantees

Several methods in Indexable, such as #bsearch and #cartesian_product, require the collection to be stable; that is, calling #each(&) over and over again should always yield the same elements, provided the collection is not mutated between the calls. In particular, #each(&) itself should not mutate the collection throughout the loop. Stability of an Indexable is guaranteed if the following criteria are met:

The standard library assumes that all including types of Indexable are always stable. It is undefined behavior to implement an Indexable that is not stable or only conditionally stable.

Included Modules

Defined in:

lib/views/src/core_ext/indexable.cr
lib/views/src/views/indexable_view.cr
hclust/core_ext/indexable.cr

Instance Method Summary

Instance Method Detail

def centroids(count : Int, rule : HClust::Rule = :single, & : T, T -> Float64) : Array(T) #

Returns the centroids of the count clusters or fewer using the linkage rule rule based on the distances computed by the given block.

The centroid of a cluster is defined as the element with the smallest average distance to all other members of the cluster. The block receives two elements and must return the dissimilarity between them.

require "hclust"
require "hclust/core_ext"

[1, 2, 10, 11, 20].centroids(2) { |a, b| (a - b).abs.to_f }
# => [2, 20]

See HClust.centroids.


[View source]
def centroids(*, cutoff : Number, rule : HClust::Rule = :single, & : T, T -> Float64) : Array(T) #

Returns the centroids of the clusters using the linkage rule rule based on the distances computed by the given block. The clusters are generated such that the cophenetic distance between any two elements in a cluster is less than or equal to cutoff.

The centroid of a cluster is defined as the element with the smallest average distance to all other members of the cluster. The block receives two elements and must return the dissimilarity between them.

require "hclust"
require "hclust/core_ext"

[1, 2, 10, 11, 20].centroids(cutoff: 3) { |a, b| (a - b).abs.to_f }
# => [1, 10, 20]

See HClust.centroids.


[View source]
def cluster(count : Int, rule : HClust::Rule = :single, & : T, T -> Float64) : Array(Array(T)) #

Clusters the elements into count clusters or fewer using the linkage rule rule based on the distances computed by the given block.

The block receives two elements and must return the dissimilarity between them.

require "hclust"
require "hclust/core_ext"

[1, 2, 10, 11, 20].cluster(2) { |a, b| (a - b).abs.to_f }
# => [[1, 2, 10, 11], [20]]

See HClust.cluster.


[View source]
def cluster(*, cutoff : Number, rule : HClust::Rule = :single, & : T, T -> Float64) : Array(Array(T)) #

Clusters the elements using the linkage rule rule based on the distances computed by the given block. The clusters are generated such that the cophenetic distance between any two elements in a cluster is less than or equal to cutoff.

The block receives two elements and must return the dissimilarity between them.

require "hclust"
require "hclust/core_ext"

[1, 2, 10, 11, 20].cluster(cutoff: 3) { |a, b| (a - b).abs.to_f }
# => [[1, 2], [10, 11], [20]]

See HClust.cluster.


[View source]
def labels(count : Int, rule : HClust::Rule = :single, & : T, T -> Float64) : Array(Int32) #

Returns a mask of cluster labels into count clusters or fewer using the linkage rule rule based on the distances computed by the given block.

The returned array has the same size as self. The value at position i is the 0-based index of the cluster containing the ith element. Cluster indexes are sorted by population, so cluster 0 is the most populated, cluster 1 the second, and so on.

require "hclust"
require "hclust/core_ext"

[1, 2, 10, 11, 20].labels(2) { |a, b| (a - b).abs.to_f }
# => [0, 0, 0, 0, 1]

See HClust.labels.


[View source]
def labels(*, cutoff : Number, rule : HClust::Rule = :single, & : T, T -> Float64) : Array(Int32) #

Returns a mask of cluster labels using the linkage rule rule based on the distances computed by the given block. The clusters are generated such that the cophenetic distance between any two elements in a cluster is less than or equal to cutoff.

The returned array has the same size as self. The value at position i is the 0-based index of the cluster containing the ith element. Cluster indexes are sorted by population, so cluster 0 is the most populated, cluster 1 the second, and so on.

require "hclust"
require "hclust/core_ext"

[0.0, 10.0, 10.1, 10.2, 10.3].labels(cutoff: 1) { |a, b| (a - b).abs.to_f }
# => [1, 0, 0, 0, 0]

See HClust.labels.


[View source]