README
¶
clusters
Data structs and algorithms for clustering data observations and basic computations in n-dimensional spaces.
Example
import "github.com/muesli/clusters"
// fake some observations
var o clusters.Observations
o = append(o, clusters.Coordinates{1, 1})
o = append(o, clusters.Coordinates{3, 2})
o = append(o, clusters.Coordinates{5, 3})
// seed a new set of clusters
c, err := clusters.New(2, o)
// add observations to clusters
c[0].Append(o[0])
c[1].Append(o[1])
c[1].Append(o[2])
// calculate the centroids for each cluster
c.Recenter()
// find the nearest cluster for an observation
i := c.Nearest(o[1])
// => returns index 1
// find the neighbouring cluster and its average distance for an observation
i, d := c.Neighbour(o[0], 0)
// => returns index 1 with euclidean distance 12.5
Development
Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AverageDistance ¶
func AverageDistance(o Observation, observations Observations) float64
AverageDistance returns the average distance between o and all observations
Types ¶
type Cluster ¶
type Cluster struct { Center Coordinates Observations Observations }
A Cluster which data points gravitate around
func (*Cluster) Append ¶
func (c *Cluster) Append(point Observation)
Append adds an observation to the Cluster
func (Cluster) PointsInDimension ¶
func (c Cluster) PointsInDimension(n int) Coordinates
PointsInDimension returns all coordinates in a given dimension
type Clusters ¶
type Clusters []Cluster
Clusters is a slice of clusters
func New ¶
func New(k int, dataset Observations) (Clusters, error)
New sets up a new set of clusters and randomly seeds their initial positions
func (Clusters) CentersInDimension ¶
func (c Clusters) CentersInDimension(n int) Coordinates
CentersInDimension returns all cluster centroids' coordinates in a given dimension
func (Clusters) Nearest ¶
func (c Clusters) Nearest(point Observation) int
Nearest returns the index of the cluster nearest to point
type Coordinates ¶
type Coordinates []float64
Coordinates is a slice of float64
func (Coordinates) Coordinates ¶
func (c Coordinates) Coordinates() Coordinates
Coordinates implements the Observation interface for a plain set of float64 coordinates
func (Coordinates) Distance ¶
func (c Coordinates) Distance(p2 Coordinates) float64
Distance returns the euclidean distance between two coordinates
type Observation ¶
type Observation interface { Coordinates() Coordinates Distance(point Coordinates) float64 }
Observation is a data point (float64 between 0.0 and 1.0) in n dimensions
type Observations ¶
type Observations []Observation
Observations is a slice of observations
func (Observations) Center ¶
func (c Observations) Center() (Coordinates, error)
Center returns the center coordinates of a set of Observations