# KNN

The KNN module implements a K Nearest Neighbors System. It has inputs for simultaneous learning and choosing of neighbors. The underlying data structure used is a KD-Tree.

## Example XML definition

A simple example

<module class = "KNN" name = "KNN" />

## Parameters

Name | Description | Type | Default |
---|---|---|---|

class | The class name for the module; must be "KNN". | string | |

name | The name of this instance of the module. | string | |

k | How many neighbors to look for. | int | 5 |

distance_type | Type of distance calculation. Euclidian is the usual sqrt(sum of squares), manhattan is just the addition of differences in all the dimensions. | choices:euclidian manhattan | euclidian |

auto_rebuild | Automatically check if some sub tree can be balanced after an insert. | bool | true |

minimum_tree_size_for_rebuild | Will not auto rebuild smaller sub trees than this. | int | 100 |

unbalanced_tree_size_ratio_limit | Defines what is considered unbalanced. The ratio means ratio between the sizes of the left and right sub trees of a tree. | float | 2.4 |

verbose | Print some info. | bool | false |

check_for_clones | Dont insert an example if it is already in the tree. | bool | false |

## Module Connections

### Inputs

Name | Description |
---|---|

INPUT | The coordinates of a target to look for neighbors for. |

T_INPUT | Training input. The coordinates of an example to learn/store. |

T_OUTPUT | Training output. The class values of the example to learn/store |

LEARN | Learns from T_INPUT/T_OUTPUT if this is > 0. |

### Outputs

Name | Description |
---|---|

INPUT_TABLE | A matrix of the coordinates of the neighbors that were found around the last INPUT. |

OUTPUT_TABLE | A matrix of the class values of the neighbors that were found around the last INPUT. |

DISTANCE_TABLE | An array of the distances of the neighbors that were found around the last INPUT. NOTE: When it was not possible to find K neighbors (the tree was smaller than size K nodes) the distances for the neighbors that were not found is set to -1.0 |

## Author

Alexander Kolodziej

LUCS

## Files

KNN.h

KNN.cc

KD_Tree.ccc

KD_Tree.h

KNN.ikc