mirror of
https://github.com/muesli/kmeans.git
synced 2025-09-27 03:56:17 +08:00
The Plotter interface lets you attach custom plotters
This commit is contained in:
12
kmeans.go
12
kmeans.go
@@ -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))
|
||||
|
Reference in New Issue
Block a user