knn

package
v0.55.2 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: BSD-3-Clause, BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

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

Index

Constants

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

Variables

This section is empty.

Functions

This section is empty.

Types

type Neighbors

type Neighbors struct {
	// contains filtered or unexported fields
}

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

func (*Neighbors) Add

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

Add new neighbor.

func (*Neighbors) Contain

func (neighbors *Neighbors) Contain(row *tabula.Row) (bool, int)

Contain return true if `row` is in neighbors and their index, otherwise return false and -1.

func (*Neighbors) Distance

func (neighbors *Neighbors) Distance(idx int) float64

Distance return distance value at index `idx`.

func (*Neighbors) Distances

func (neighbors *Neighbors) Distances() *[]float64

Distances return slice of distance of each neighbours.

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) Replace

func (neighbors *Neighbors) Replace(idx int, row *tabula.Row, distance float64)

Replace neighbor at index `idx` with new row and distance value.

func (*Neighbors) Row

func (neighbors *Neighbors) Row(idx int) *tabula.Row

Row return pointer to row at index `idx`.

func (*Neighbors) Rows

func (neighbors *Neighbors) Rows() *[]*tabula.Row

Rows return all rows.

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) SelectWhere

func (neighbors *Neighbors) SelectWhere(idx int, val string) (newn Neighbors)

SelectWhere return all neighbors where row value at index `idx` is equal to string `val`.

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.

type Runtime

type Runtime struct {
	// AllNeighbors contain all neighbours
	AllNeighbors Neighbors

	// DistanceMethod define how the distance between sample will be
	// measured.
	DistanceMethod int

	// ClassIndex define index of class in dataset.
	ClassIndex int `json:"ClassIndex"`

	// K define number of nearest neighbors that will be searched.
	K int `json:"K"`
}

Runtime parameters for KNN processing.

func (*Runtime) ComputeEuclidianDistance

func (in *Runtime) ComputeEuclidianDistance(samples *tabula.Rows,
	instance *tabula.Row,
)

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

func (*Runtime) FindNeighbors

func (in *Runtime) FindNeighbors(samples *tabula.Rows, instance *tabula.Row) (
	kneighbors Neighbors,
)

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

Jump to

Keyboard shortcuts

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