cs349 measuring distance - nucs-349-fall21.github.io

36
Machine Learning Measuring Distance 1 Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Upload: others

Post on 18-Mar-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cs349 measuring distance - nucs-349-fall21.github.io

Machine Learning

Measuring Distance

1Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 2: cs349 measuring distance - nucs-349-fall21.github.io

Why measure distance?

• Clustering requires distance measures.

• Local methods require a measure of “locality”

• Search engines require a measure of similarity

2Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 3: cs349 measuring distance - nucs-349-fall21.github.io

What is a “metric”?

• A function of two values with these four qualities.

)inequality (triangle ),(),(),((symmetry) ),(),(

negative)-(non 0),(ty)(reflexivi iff 0),(

zxdzydyxdxydyxd

yxdyxyxd

³+=³

==

3Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 4: cs349 measuring distance - nucs-349-fall21.github.io

What is a norm ||v|| ?

• Loosely, it is a function that applies a positive value to all vectors (except the 0 vector) in a vector space.

• 3 properties:

4

For all a ∈ F and u,v ∈ V, a function p :V→ Fp(av) =| a | p(v) (positive scalability)p(u) = 0 iff u is the zero vector p(u)+ p(v) ≥ p(u+ v) (triangle inequality)

Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 5: cs349 measuring distance - nucs-349-fall21.github.io

2 definitions (AKA why this is confusing)

• A vector normassigns a strictly positive value to all vectors v in a vector space….except the 0 vector, which has a 0 assigned to it. (see previous slide)

| 𝑣 | ≥ 0

• A normal vectorA vector is normal to another object if they are perpendicular to each other. So, a normal vector is perpendicular to the tangent plane of a surface at some point P.

5Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 6: cs349 measuring distance - nucs-349-fall21.github.io

Metric == Norm??

• Every norm determines a metric.Given a normed vector space, we can make a metric by saying

𝑑(𝐮, 𝐯) ≡ 𝐮 − 𝐯• Some metrics determine a norm.

If the metric is on a vector space, you can define a norm by saying…

𝐮 ≡ 𝑑(𝐮, 𝟎)6Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 7: cs349 measuring distance - nucs-349-fall21.github.io

Euclidean Distance• What people intuitively think of as “distance”• Is it a metric? • Is it a norm?

v

Dimension 1

Dim

ensi

on 2

u

7

𝑑 𝐮, 𝐯 = 𝑢! − 𝑣! " + 𝑢" − 𝑣" "

Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 8: cs349 measuring distance - nucs-349-fall21.github.io

Generalized Euclidean Distance

Bryan Pardo, Machine Learning: EECS 349 Fall 2013

n = the number of dimensions

8

𝑑 𝐮, 𝐯 = .!

"

𝑢! − 𝑣! #$/#

𝐮 = 𝑢!, 𝑢", 𝑢#, … , 𝑢$𝐯 = 𝑣!, 𝑣", 𝑣#, … , 𝑣$

Where…

𝐮, 𝐯 ∈ 𝓡$

Page 9: cs349 measuring distance - nucs-349-fall21.github.io

Lp norms

{ }1,0 and 1 :Distance Hamming

2 :DistanceEuclidean norm Lx

1 :DistanceManhattan norm Lx

||),(

22

11

/1

1

Î=

===

===

úû

ùêë

é-= å

=

ii

pn

i

pii

,yx p

p

p

yxyxd !!

• Lp norms are all special cases of this:

p changes the norm

9Bryan Pardo, Machine Learning: EECS 349 Spring 2019

𝑑 𝐮, 𝐯 = -%

$

𝑢% − 𝑣% &!/&

𝐮 𝟏

𝐮 𝟐

∀𝑖, 𝑢%, 𝑣% ∈ 0,1

Page 10: cs349 measuring distance - nucs-349-fall21.github.io

Weighting Dimensions

• Put point in the cluster with the closest center of gravity• Which cluster should the red point go in?• How do I measure distance in a way that gives the “right”

answer for both situations?

10Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 11: cs349 measuring distance - nucs-349-fall21.github.io

Weighted Norms

• You can compensate by weighting your dimensions….

This lets you turn your circle of equal-distance into an elipse with axes parallel to the dimensions of the vectors.

11Bryan Pardo, Machine Learning: EECS 349 Spring 2019

𝑑 𝐮, 𝐯 = .!

"

𝑤! 𝑢! − 𝑣! &$/&

Page 12: cs349 measuring distance - nucs-349-fall21.github.io

Cosine Similarity 𝑠 𝐮, 𝐯

Sometimes, you don’t want to think about magnitude of a vector, just the direction.

12

𝑠 𝐮, 𝐯 =𝐮𝑻𝐯

||𝐮||"||𝐯||"

Bryan Pardo, Machine Learning: EECS 349 Spring 2019

||𝐮||! = 𝑑 𝐮, 𝟎 = ."#$

%

𝑢" − 0 !

= ∑!"#$ $! %!

∑!"#$ $!&' % ∑!"#

$ %!&' %

Page 13: cs349 measuring distance - nucs-349-fall21.github.io

Cosine Distance

13Bryan Pardo, Machine Learning: EECS 349 Spring 2019

𝑐𝑜𝑠𝑖𝑛𝑒 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑑 𝐮, 𝐯 = 1 − 𝑠 𝐮, 𝐯

Cosine similarity goes as low as -1 and maximizes at 1, when x == y.

To make it a distance measure (but still not a metric), make sure goes down when things are more similar and that the most similar pair gets a distance of 0

𝑠 𝐮, 𝐯 =𝐮𝑻𝐯

||𝐮||"||𝐯||"

Page 14: cs349 measuring distance - nucs-349-fall21.github.io

Cosine Distance

• What is the distance to the 0 vector?

• What is the distance between 2 vectors with the same angle, but different magnitudes?

• How do these things relate to the definition of being a metric?

14Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 15: cs349 measuring distance - nucs-349-fall21.github.io

Pearson Correlation Coefficient

• Measure of correlation between two variables• Related to, but not identical to cosine similarity• Pearson correlation coefficient

Range (-1, 1)A perfect positive correlation: 1A perfect negative correlation: -1

In Python, >> import scipy.stats>> scipy.stats.pearsonr(array1, array2)

Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 16: cs349 measuring distance - nucs-349-fall21.github.io

Pearson Sample Correlation 𝑟𝐱𝐲

16

Mean: 𝜇𝐱 =∑!"#$ (!%

Bryan Pardo, Machine Learning: EECS 349 Spring 2019

𝑟𝐱𝐲 =∑!)$" 𝑥! − 𝜇* 𝑦! − 𝜇+

∑!)$" 𝑥! − 𝜇* # ∑!)$" 𝑦! − 𝜇+#

Page 17: cs349 measuring distance - nucs-349-fall21.github.io

Example correlations

Image from Wikipedia: contribured by DenisBoigelot 17

Divide by 0error

Page 18: cs349 measuring distance - nucs-349-fall21.github.io

Pearson vs Cosine

18

𝑠 𝐮, 𝐯 = ∑!"#$ -!./ 0!./

∑!"#$ -!./ % ∑!"#

$ 0!./ %

Pearson Correlation Coefficient

Cosine Similarity, where we add in some 0s, so the relationship becomes clear

𝑟𝐱𝐲 =∑!)$" 𝑥! − 𝜇* 𝑦! − 𝜇+

∑!)$" 𝑥! − 𝜇* # ∑!)$" 𝑦! − 𝜇+#

Bryan Pardo, Machine Learning: EECS 349 Spring 2019

Page 19: cs349 measuring distance - nucs-349-fall21.github.io

Metric, or not?

• Driving distance with 1-way streets

• Categorical Stuff : – Is distance (Jazz to Blues to Rock) no less

than distance (Jazz to Rock)?

Bryan Pardo, Machine Learning: EECS 349 Fall 2013

x

y

19

Page 20: cs349 measuring distance - nucs-349-fall21.github.io

Categorical Variables• Consider feature vectors for genre & vocals:

– Genre: {Blues, Jazz, Rock, Zydeco}– Vocals: {vocals,no vocals}

s1 = {rock, vocals}s2 = {jazz, no vocals}s3 = { rock, no vocals}

• Which two songs are more similar?

Bryan Pardo, Machine Learning: EECS 349 Fall 2013 20

Page 21: cs349 measuring distance - nucs-349-fall21.github.io

One Solution:Hamming distance

s1 = {rock, vocals}s2 = {jazz, no_vocals}s3 = { rock, no_vocals}

0 0 1 0 1

0 1 0 0 0

0 0 1 0 0

Blues Jazz ZydecoRock Vocals

Hamming Distance = number of bits different between binary vectors

Bryan Pardo, Machine Learning: EECS 349 Fall 2013 21

Page 22: cs349 measuring distance - nucs-349-fall21.github.io

Hamming Distance

Bryan Pardo, Machine Learning: EECS 349 Fall 2013

})1,0{( and},...,,y

,,...,, where

||),(

21

21

1

Î"=<

>=<

-=å=

ii

n

n

n

iii

,yxiyyyxxxx

yxyxd

!

!

!!

22

Page 23: cs349 measuring distance - nucs-349-fall21.github.io

Defining your own distance(an example)

Quote Frequency

Bryan Pardo, Machine Learning: EECS 349 Fall 2013

How often does artist x quote artist y?

Let’s build a distance measure!

Beethoven Beatles Kanye

Beethoven 7 0 0

Beatles 4 5 0

Kanye ? 1 2

23

Page 24: cs349 measuring distance - nucs-349-fall21.github.io

Defining your own distance(an example)

Beethoven Beatles Kanye

Beethoven 7 0 0

Beatles 4 5 0

Kanye ? 1 2

Bryan Pardo, Machine Learning: EECS 349 Fall 2013

åÎ

-=

=

Artistszf

f

f

zxQyxQ

yxd

yxQ

),(),(

1),( Distance

in table value),(frequency Quote

24

Page 25: cs349 measuring distance - nucs-349-fall21.github.io

Missing data

• What if, for some category, on some examples, there is no value given?

• Approaches:– Discard all examples missing the category– Fill in the blanks with the mean value– Only use a category in the distance measure if

both examples give a value

Bryan Pardo, Machine Learning: EECS 349 Fall 2013 25

Page 26: cs349 measuring distance - nucs-349-fall21.github.io

(one way of) handling missing attributes

úû

ùêë

é

-=

îíì

=

åå =

=

n

iiin

ii

iii

yxwn

nyxd

yxw

1

1

),(),(

else 1,defined are and both if ,0

f!!

Bryan Pardo, Machine Learning: EECS 349 Fall 2013 26

A scaling factor that adds weight to the distance, as there are fewer attributes used

A distance measure that works on individual attributes

Page 27: cs349 measuring distance - nucs-349-fall21.github.io

One more distance measure

27Bryan Pardo, Machine Learning: EECS 349 Fall 2013

• Kullback–Leibler (KL) divergence– a non-symmetric measure of the difference

between two probability distributions– not a metric, since it is not symmetric– Here’s the definition of KL divergence for

discrete probability distributions P and Q

DKL (P ||Q) = ln P(i)Q(i)!

"#

$

%&

i∑ P(i)

Page 28: cs349 measuring distance - nucs-349-fall21.github.io

KL Divergence as Cross Entropy

28Bryan Pardo, Machine Learning: EECS 349 Fall 2013

DKL (P ||Q) = ln P(i)Q(i)!

"#

$

%&

i∑ P(i)

= ln(P(i))− ln(Q(i)( )i∑ P(i)

= P(i)lnP(i)−i∑ P(i)lnQ(i)

i∑

Page 29: cs349 measuring distance - nucs-349-fall21.github.io

Edit Distance • Query = string from finite alphabet• Target = string from finite alphabet• Cost of Edits = Distance

C A G E D

C E A E D

- -

Target:

Query:Bryan Pardo, Machine Learning: EECS 349 Fall 2013 29

Page 30: cs349 measuring distance - nucs-349-fall21.github.io

Levenshtein edit distance

Mi-1,j-1 Mi-1,j

Mi,j-1 Mi,j

µ(si,qj ) =0 if si = qj

1 otherwise

!"#

$#

30

Insertion

Deletion

Substitution

Mi, j = min

Mi−1, j +1

Mi, j−1 +1

Mi−1, j−1 + µ(si ,qj )

⎨⎪⎪

⎩⎪⎪

M 0,0 = 0 3 possible operations

Page 31: cs349 measuring distance - nucs-349-fall21.github.io

Pseudocode of Levenshtein (after Wagner and Fischer)

Bryan Pardo, Machine Learning: EECS 349 Fall 201631

return int LevenshteinDistance(char s[1..m], char t[1..n], deletionCost, insertionCost, substitutionCost)

// A standard approach is to set deletionCost = insertionCost = substitutionCost = 1

declare int M[0..m, 0..n] // M has (m+1) by (n+1) valuesfor i from 0 to m

M[i, 0] := i*deletionCost // distance of any 1st string to an empty 2nd stringfor j from 0 to n

M[0, j] := j*insertionCost // distance of any 2nd string to an empty 1st string

for j from 1 to n for i from 1 to m

if s[i] = t[j] thenM[i, j] := M[i-1, j-1] // no operation cost, because they match

elseM[i, j] := minimum(M[i-1, j] + deletionCost,

M[i, j-1] + insertionCost, M[i-1, j-1] + substitutionCost)

return M[m,n]

Page 32: cs349 measuring distance - nucs-349-fall21.github.io

Working through an example

F R O G

0 1,-,- 2,-,- 3,-,- 4,-,-

D -,-,1 2,1,2 2,2,3 3,3,4 4,4,5

O -,-,2 3,2,2 3,2,3 3,2,4 3,4,5

G -,-,3 4,3,3 4,3,3 4,3,3 4,2,4

Bryan Pardo, Machine Learning: EECS 349 Fall 2016 32

Mi, j = min

Mi−1, j +1

Mi, j−1 +1

Mi−1, j−1 + µ(si ,qj )

⎨⎪⎪

⎩⎪⎪

µ(si,qj ) =0 if si = qj

1 otherwise

!"#

$#

M 0,0 = 0

Mi-1,j-1 Mi-1,j

Mi,j-1 Mi,jEdit distance is 2 edits

Page 33: cs349 measuring distance - nucs-349-fall21.github.io

Working through an example

Bryan Pardo, Machine Learning: EECS 349 Fall 2016 33

• The final edit cost is the lowest value calculated for the lower right-hand corner of the matrix.

• Tracing a path from the lower right to the beginning shows 2 minimal-cost alignments, each with 1 substitution and one deletion:

FROG FROGD-OG -DOG

F R O G

0 1,-,- 2,-,- 3,-,- 4,-,-

D -,-,1 2,1,2 2,2,3 3,3,4 4,4,5

O -,-,2 3,2,2 3,2,3 3,2,4 3,4,5

G -,-,3 4,3,3 4,3,3 4,3,3 4,2,4

del F

del R

sub D for F

sub D for R

O = O

G= G

Edit distance is 2 edits

Page 34: cs349 measuring distance - nucs-349-fall21.github.io

(Somewhat more) General Edit Distance

Mi, j =min

Mi−1, j +µ(−,qj )

Mi, j−1 +µ(si,−)

Mi−1, j−1 +µ(si,qj )

"

#$$

%$$

Insert

Delete

Match

Bryan Pardo, Machine Learning: EECS 349 Fall 201334

µ(si ,qj ) = whatever you want.The distance between si and qj on a keyboard?The probability of substituting si for qj?

Page 35: cs349 measuring distance - nucs-349-fall21.github.io

Final notes on edit distance

• Used in many applications– Gene sequence matching (google: BLAST)– Spell checking– Music melody matching

• There are many variants of the algorithms• The parameter weights strongly affect

performance• You need to pick the algorithm and

parameters that make sense for your problem.

35

Page 36: cs349 measuring distance - nucs-349-fall21.github.io

Some take-away thoughts

• Many machine learning methods are helped by having a distance measure

• Some methods require metrics• Not all measures are metrics• Some common distance measures:

“P-norms”: Euclidean, Manhattan“Edit distance”: LevenshteinKL DivergenceMahalanobis

Bryan Pardo, Machine Learning: EECS 349 Fall 2013 36