Won't You be my Nearest Neighbor?

Post on 16-Jul-2015

713 views 2 download

Tags:

transcript

Nearest Neighbor

An Explanation of Nearest Neighbor and how to use it to get to

know your data in Matlab

Match Making for Soda Lovers

Sw

eetness

Sw

eetness

Carbonation

Sw

eetness

Carbonation

Price

Sw

eetness

Carbonation

Price

Acidity

Sw

eetness

Carbonation

Price

Acidity

Caffeination

Sw

eetness

Carbonation

Price

Acidity

Caffeination

Etc.

Sw

eetness

Carbonation

Price

Acidity

Caffeination

Etc.

Etc.

Sw

eetness

Carbonation

Price

Acidity

Caffeination

Etc.

Etc.

Etc.

Sweetness

Carbonation

Price

Soda ID Sweetness Carbonation Price

Soda 1 3.5 4.3 $1.00

Soda 2 5.1 4.1 $1.50

Soda 3 4.2 2.2 $2.00

Soda 4 3.1 4.2 $0.75

Soda 5 6 3.0 $0.50

Sweetness

Carbonation

Price

xlsread(filename)csvread(filename)

Sweetness Carbonation Price

3.5 4.3 $1.00

5.1 4.1 $1.50

4.2 2.2 $2.00

3.1 4.2 $0.75

6 3.0 $0.50

Soda Brand

Soda 1

Soda 2

Soda 3

Soda 4

Soda 5

[SODAS, BRANDS] = xlsread(filename)

*

***

**

*

*

**

***

***

**

*

*

**

***

***

**

*

*

**

***

***

**

*

*

**

***

The Basic Math

Differencein Sweetness

Differencein Carbonation

Differencein Price

Sweetness

Carbona

tion

a

b

A^2 + b^2 = c^3

Distance:(in lots of dimensions)

MatlabdsearchnN-Dimensional nearest point search

Syntaxk = dsearchn(SODAS,IDEALS)

rangesearchFind all neighbors specified distance using KDTreeSearcher object

Syntax[idx,D]= rangesearch(SODATREE,IDEALS,r)

MatlabdsearchnN-Dimensional nearest point search

Syntaxk = dsearchn(SODAS,IDEALS)

knnsearchFind k-nearest neighbors using data

Syntax[IDX,D] = knnsearch(SODATREE,IDEALS,r)

For single nearestpoint

For More Neighbors(requires a tree)

Easy One Match SearchdsearchnN-Dimensional nearest point search

SyntaxI = dsearchn(SODAS,IDEALS)

Harder Multi Match SearchBuilding a Tree

Sweetness Carbonation Price

3.5 4.3 $1.00

5.1 4.1 $1.50

4.2 2.2 $2.00

3.1 4.2 $0.75

6 3.0 $0.50

SODATREE = kdtreesearcher(SODAS,'distance')

All the Sodas

Sweetness Carbonation Price

3.5 4.3 $1.00

5.1 4.1 $1.50

4.2 2.2 $2.00

rangesearchFind all neighbors specified distance using KDTreeSearcher object

Syntax[I,D]= rangesearch(SODATREE,IDEALS,r)

**r

Harder Multi Match SearchGetting neighbors

Ideal Sodas