gokmeans

package module
v0.0.0-...-ea22cff Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2017 License: Apache-2.0 Imports: 2 Imported by: 16

README

gokmeans

A Go (golang) package that implements the K-means clustering algorithm.

Goroutines are used throughout to do some calulations concurrently.

Get package

go get github.com/mdesenfants/gokmeans

Example
package main

import (
	"fmt"
	"github.com/mdesenfants/gokmeans"
)

var observations []gokmeans.Node = []gokmeans.Node {
	gokmeans.Node{20.0, 20.0, 20.0, 20.0},
	gokmeans.Node{21.0, 21.0, 21.0, 21.0},
	gokmeans.Node{100.5, 100.5, 100.5, 100.5},
	gokmeans.Node{50.1, 50.1, 50.1, 50.1},
	gokmeans.Node{64.2, 64.2, 64.2, 64.2},
}

func main() {
	// Get a list of centroids and output the values
	if success, centroids := gokmeans.Train(observations, 2, 50); success {
		// Show the centroids
		fmt.Println("The centroids are")
		for _, centroid := range centroids {
			fmt.Println(centroid)
		}

		// Output the clusters
		fmt.Println("...")
		for _, observation := range observations {
			index := gokmeans.Nearest(observation, centroids)
			fmt.Println(observation, "belongs in cluster", index+1, ".")
		}
	}
}

Documentation

Overview

Gokmeans is a simple k-means clusterer that determines centroids with the Train function, and then classifies additional observations with the Nearest function.

package main

import (
	"fmt"
	"github.com/mdesenfants/gokmeans"
)

var observations []gokmeans.Node = []gokmeans.Node {
	gokmeans.Node{20.0, 20.0, 20.0, 20.0},
	gokmeans.Node{21.0, 21.0, 21.0, 21.0},
	gokmeans.Node{100.5, 100.5, 100.5, 100.5},
	gokmeans.Node{50.1, 50.1, 50.1, 50.1},
	gokmeans.Node{64.2, 64.2, 64.2, 64.2},
}

func main() {
	// Get a list of centroids and output the values
	if success, centroids := gokmeans.Train(observations, 2, 50); success {
		// Show the centroids
		fmt.Println("The centroids are")
		for _, centroid := range centroids {
			fmt.Println(centroid)
		}

		// Output the clusters
		fmt.Println("...")
		for _, observation := range observations {
			index := gokmeans.Nearest(observation, centroids)
			fmt.Println(observation, "belongs in cluster", index+1, ".")
		}
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Nearest

func Nearest(in Node, nodes []Node) int

Nearest return the index of the closest centroid from nodes

Types

type Node

type Node []float64

Node represents an observation of floating point values

func Train

func Train(Nodes []Node, clusterCount int, maxRounds int) (bool, []Node)

Train takes an array of Nodes (observations), and produces as many centroids as specified by clusterCount. It will stop adjusting centroids after maxRounds is reached. If there are less observations than the number of centroids requested, then Train will return (false, nil).

func Train2

func Train2(Nodes []Node, clusterCount int, maxRounds int, centroids []Node) (bool, []Node)

Provide initial centroids

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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