meanshift

package
Version: v0.0.0-...-38d63f0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2015 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package meanshift provides mean shift clustering for ℝⁿ data.

Example
package main

import (
	"github.com/biogo/cluster/meanshift"

	"fmt"
	"strings"
)

type Feature struct {
	ID    string
	Start int
	End   int
}

func (f *Feature) Len() int { return f.End - f.Start }

type Features []*Feature

func (f Features) Len() int               { return len(f) }
func (f Features) Values(i int) []float64 { return []float64{float64(f[i].Start), float64(f[i].End)} }

var feats = []*Feature{
	{ID: "0", Start: 1, End: 1700},
	{ID: "1", Start: 2, End: 1700},
	{ID: "2", Start: 3, End: 610},
	{ID: "3", Start: 2, End: 605},
	{ID: "4", Start: 1, End: 600},
	{ID: "5", Start: 2, End: 750},
	{ID: "6", Start: 650, End: 900},
	{ID: "7", Start: 700, End: 950},
	{ID: "8", Start: 1000, End: 1700},
	{ID: "9", Start: 950, End: 1712},
	{ID: "10", Start: 1000, End: 1650},
}

func main() {
	ms := meanshift.New(Features(feats), meanshift.NewTruncGauss(60, 3), 0.1, 5)
	err := ms.Cluster()
	if err != nil {
		fmt.Println(err)
		return
	}

	for ci, c := range ms.Centers() {
		fmt.Printf("Cluster %d:\n", ci)
		for _, i := range c.Members() {
			f := feats[i]
			fmt.Printf("%2s %s%s\n",
				f.ID,
				strings.Repeat(" ", f.Start/20),
				strings.Repeat("-", f.Len()/20),
			)
		}
		fmt.Println()
	}

	var within float64
	for _, ss := range ms.Within() {
		within += ss
	}
	fmt.Printf("betweenSS / totalSS = %.6f\n", 1-(within/ms.Total()))

}
Output:

Cluster 0:
 5 -------------------------------------

Cluster 1:
 0 ------------------------------------------------------------------------------------
 1 ------------------------------------------------------------------------------------

Cluster 2:
 4 -----------------------------
 3 ------------------------------
 2 ------------------------------

Cluster 3:
 7                                    ------------
 6                                 ------------

Cluster 4:
 9                                                --------------------------------------
 8                                                   -----------------------------------
10                                                   --------------------------------

betweenSS / totalSS = 0.998655

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MeanShift

type MeanShift struct {
	// contains filtered or unexported fields
}

MeanShift implements data clustering using the mean shift algorithm.

func New

func New(data cluster.Interface, k Shifter, tol float64, maxIter int) *MeanShift

New creates a new mean shift Clusterer object populated with data from an Interface value, data and using the Shifter k.

func (*MeanShift) Centers

func (ms *MeanShift) Centers() []cluster.Center

Centers returns the centers determined by a previous call to Cluster.

func (*MeanShift) Cluster

func (ms *MeanShift) Cluster() error

Cluster runs a clustering of the data using the mean shift algorithm.

func (*MeanShift) Total

func (ms *MeanShift) Total() float64

Total calculates the total sum of squares for the data relative to the data mean.

func (*MeanShift) Values

func (ms *MeanShift) Values() []cluster.Value

Values returns a slice of the values in the MeanShift.

func (*MeanShift) Within

func (ms *MeanShift) Within() []float64

Within calculates the sum of squares within each cluster. It returns nil if Cluster has not been called.

type Shifter

type Shifter interface {
	// Init initialises the Shifter with the provided data.
	Init(cluster.Interface)

	// Shift performs a single iteration of the mean shift algorithm and
	// returns the sum of squares differences between the initial state
	// and the final state.
	Shift() float64

	// Bandwidth returns the bandwidth parameter of the Shifter.
	Bandwidth() float64

	// Centers returns the cluster centers of the clustered data.
	Centers() []cluster.Center
}

Shifter implements a single step of the mean shift algorithm.

type TruncGauss

type TruncGauss struct {
	// contains filtered or unexported fields
}

func NewTruncGauss

func NewTruncGauss(h, oversample float64) *TruncGauss

func (*TruncGauss) Bandwidth

func (s *TruncGauss) Bandwidth() float64

func (*TruncGauss) Centers

func (s *TruncGauss) Centers() []cluster.Center

func (*TruncGauss) Init

func (s *TruncGauss) Init(data cluster.Interface)

func (*TruncGauss) Shift

func (s *TruncGauss) Shift() (delta float64)

type Uniform

type Uniform struct {
	// contains filtered or unexported fields
}

func NewUniform

func NewUniform(h float64) *Uniform

func (*Uniform) Bandwidth

func (s *Uniform) Bandwidth() float64

func (*Uniform) Centers

func (s *Uniform) Centers() []cluster.Center

func (*Uniform) Init

func (s *Uniform) Init(data cluster.Interface)

func (*Uniform) Shift

func (s *Uniform) Shift() (delta float64)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL