knn

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2016 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package knn implement the K Nearest Neighbor using Euclidian to compute the distance between samples.

Index

Constants

View Source
const (
	// TEuclidianDistance used in Input.DistanceMethod.
	TEuclidianDistance = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Input

type Input struct {
	// DistanceMethod define how the distance between sample will be
	// measured.
	DistanceMethod int
	// ClassIdx define index of class in dataset.
	ClassIdx int
	// K define number of nearset neighbors that will be searched.
	K int
	// AllNeighbors contain all neighbours
	AllNeighbors Neighbors
}

Input parameters for KNN processing.

func (*Input) ComputeEuclidianDistance

func (in *Input) ComputeEuclidianDistance(samples dsv.Rows, instance dsv.Row)

ComputeEuclidianDistance compute the distance of instance with each sample in dataset `samples` and return it.

func (*Input) FindNeighbors

func (in *Input) FindNeighbors(samples dsv.Rows, instance dsv.Row) (
	kneighbors Neighbors,
)

FindNeighbors Given sample set and an instance, return the nearest neighbors as a slice of neighbours.

type Neighbors

type Neighbors struct {
	// Dataset contain the data in neighbors
	dsv.Dataset
	// Distance value
	Distances []float64
}

Neighbors is a mapping between sample and their distance. This type implement the sort interface.

func (*Neighbors) Add

func (neighbors *Neighbors) Add(row dsv.Row, distance float64)

Add new neighbor.

func (*Neighbors) Len

func (neighbors *Neighbors) Len() int

Len return the number of neighbors. This is for sort interface.

func (*Neighbors) Less

func (neighbors *Neighbors) Less(i, j int) bool

Less return true if i < j. This is for sort interface.

func (*Neighbors) SelectRange

func (neighbors *Neighbors) SelectRange(start, end int) (newn Neighbors)

SelectRange select all neighbors from index `start` to `end`. Return an empty set if start or end is out of range.

func (*Neighbors) Swap

func (neighbors *Neighbors) Swap(i, j int)

Swap content of object in index i with index j. This is for sort interface.

Jump to

Keyboard shortcuts

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