Documentation ¶
Overview ¶
Package knn contains structures and functions that help implemnt the K-Nearest Neighbors algorithm.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Classify ¶
func Classify(numNeighbors int, point interface{}, training DataSet, df DistanceFunc, cf ClassificationFunc) interface{}
Classify is the main entry point for the KNN processing for an individual piece of data that needs to be classified. It requires the number of neighbors needed for classification, the data point being classified, the training data used to perform the classification, a method for calculating distance between neighbors, and a method for converting between classification types.
func KNN ¶
func KNN(numNeighbors int, testing, training DataSet, df DistanceFunc, cf ClassificationFunc) []interface{}
KNN performs K-Nearest Neighbors analysis on a testing data set given a training data set.
Types ¶
type ClassificationFunc ¶
type ClassificationFunc func(t interface{}) interface{}
ClassificationFunc returns the classification label for a given neighbor.
type DataSet ¶
type DataSet interface {
Iterate() <-chan interface{}
}
DataSet provides the classifer the ability to iterate over data being used for training and/or classification.
type DistanceFunc ¶
type DistanceFunc func(a, b interface{}) float64
DistanceFunc is responsible for calculaing the distance between two neighbors.
type TopNList ¶
type TopNList struct {
// contains filtered or unexported fields
}
TopNList is a data structure that stores the a classification target's top-N neighbors weighted by minium distance.
func NewTopNList ¶
NewTopNList creates a new TopNList that will retain up to n neighbors, keep the closest neighbors.
func (*TopNList) Add ¶
Add adds a neighbor to the TopNList if the neighbor is one of the n-closest neighbors.
func (*TopNList) GetClassification ¶
func (l *TopNList) GetClassification(cf ClassificationFunc) interface{}
GetClassification polls the top-n neighbors for their classification and then returns the most common classification found among the neighbors. The ClassificationExtractor is used to extract the classification from the neighbor.