clusters

package module
v0.0.0-...-9fc8474 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2023 License: MIT Imports: 3 Imported by: 2

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

GoDoc Build Status Coverage Status Go ReportCard

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

func (*Cluster) Recenter

func (c *Cluster) Recenter()

Recenter recenters a cluster

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

func (Clusters) Neighbour

func (c Clusters) Neighbour(point Observation, fromCluster int) (int, float64)

Neighbour returns the neighbouring cluster of a point along with the average distance to its points

func (Clusters) Recenter

func (c Clusters) Recenter()

Recenter recenters all clusters

func (Clusters) Reset

func (c Clusters) Reset()

Reset clears all point assignments

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL