The Plotter interface lets you attach custom plotters

This commit is contained in:
Christian Muehlhaeuser
2018-05-27 22:06:57 +02:00
parent 43c4b4685f
commit a2e4fa300c
5 changed files with 51 additions and 40 deletions

View File

@@ -11,27 +11,27 @@ import (
// Kmeans configuration/option struct
type Kmeans struct {
// when Debug is enabled, graphs are generated after each iteration
debug bool
plotter Plotter
// DeltaThreshold (in percent between 0.0 and 0.1) aborts processing if
// less than n% of data points shifted clusters in the last iteration
deltaThreshold float64
}
// NewWithOptions returns a Kmeans configuration struct with custom settings
func NewWithOptions(deltaThreshold float64, debug bool) (Kmeans, error) {
func NewWithOptions(deltaThreshold float64, plotter Plotter) (Kmeans, error) {
if deltaThreshold <= 0.0 || deltaThreshold >= 1.0 {
return Kmeans{}, fmt.Errorf("threshold is out of bounds (must be >0.0 and <1.0, in percent)")
}
return Kmeans{
debug: debug,
plotter: plotter,
deltaThreshold: deltaThreshold,
}, nil
}
// New returns a Kmeans configuration struct with default settings
func New() Kmeans {
m, _ := NewWithOptions(0.01, false)
m, _ := NewWithOptions(0.01, nil)
return m
}
@@ -100,8 +100,8 @@ func (m Kmeans) Partition(dataset Points, k int) (Clusters, error) {
if changes > 0 {
clusters.recenter()
}
if m.debug {
draw(clusters, i)
if m.plotter != nil {
m.plotter.Plot(clusters, i)
}
if changes < int(float64(len(dataset))*m.deltaThreshold) {
// fmt.Println("Aborting:", changes, int(float64(len(dataset))*m.TerminationThreshold))