I think that the cosine is fine
so we'll calculate it between this and
that line stat Quest
hello I'm Josh starmer and welcome to
stat Quest today we're going to talk
about the cosine similarity and it's
going to be clearly explained when you
build them and deploy in awesome stuff
and it's really fast you're using light
now
hoor
ay this stat Quest is also brought to
you by the letters a b and c a always b
b c curious always be curious
imagine we saw these sentences about the
1990 hit movie troll 2.
by I it's pretty easy to see that the
first three sentences are similar to
each other they all Express a positive
feeling towards the movie
in contrast this last sentence is from
someone who does not like troll 2.
so when we don't have many sentences
it's pretty easy to see which ones are
similar to each other and which ones are
different
however what would we do if we collected
all of the Twitter traffic for the last
month how would we determine
similarities and differences in the
tweets
in this case we can no longer rely on
doing things by eye and instead have to
get a computer to do it
this is where the cosine similarity
comes in handy
the cosine similarity is a relatively
easy to calculate metric that can tell
us how similar or different things are
bam
to get an understanding of how the
cosine similarity Works let's start with
a super simple example
here we want to know how similar the
phrase hello world is to hello
so the first thing that we do is make a
table for the words that appear in the
phrases hello and world
in the first phrase hello world we see
the word hello wants and we see the word
world once
in contrast in the second phrase hello
we only see the word hello one time
and we don't see the word world at all
now given this table of counts for each
word in each phrase we can create a
two-dimensional graph that has the
number of times we saw the word hello on
the x-axis and the number of times we
saw the word world on the y-axis
now since the first phrase hello world
had one occurrence of each word we can
plot a point for it in the center of the
graph
in contrast the point for the second
phrase hello is on the x-axis at 1.
now if we draw lines from the origin of
the graph 0 0 to the points we can see
that there is a 45 degree angle between
the two lines
and the cosine of a 45 degree angle the
cosine of 45 degrees is 0.71
and thus the cosine similarity between
the phrases hello world and hello is the
cosine of 45 degrees which equals 0.71
bam bam
note currently the second phrase is
hello
but what if it were hello hello hello
now the table has a three for hello
since it's in the phrase three times but
we still have a zero for the word world
in this case the point on the graph
representing hello hello hello
would be further out on the x-axis
however the angle between the two lines
would still be 45 degrees
and thus the cosine similarity would be
the exact same as what we got before the
cosine of 45 degrees equals 0.71
in other words the cosine similarity is
determined entirely by the angle between
the lines
and not by the lengths of the lines
bam now if both phrases are hello world
then they are exactly the same and we
end up plotting the dots that represent
the phrases on top of each other
creating a kind of Swampy green color
and the angle between the lines from the
origin and the points would be zero
degrees
and the cosine similarity is the cosine
of zero degrees which equals one
so when both phrases are exactly the
same the angle between them will be zero
degrees and the cosine similarity is one
in contrast if the phrases don't have
any words in common like here where the
first phrase is hello and the second
phrase is world
then the angle between the two phrases
will be 90 degrees
and the cosine similarity will be the
cosine of 90 degrees which equals zero
to summarize when two phrases have
absolutely nothing in common the cosine
similarity is zero
and when the phrases are the exact same
the cosine similarity is one
and when there is some overlap between
the two phrases but they are not exactly
the same the cosine similarity is
between 0 and 1.
double bam
now the way we've been Computing the
cosine similarity step one make a table
of the word counts step two plot the
points step three figure out the angle
and lastly step four calculate the
cosine of the angle
is pretty tedious
the good news is that there is a
relatively simple formula that can be
computed super quickly on a computer to
calculate the cosine similarity directly
from the table of word counts
dang that equation looks complicated
don't worry Squatch we'll go through it
one step at a time
thanks
these sigmas are shorthand for adding up
a bunch of stuff
the I is short for index and is used to
keep track of the word we are working on
I starts out set to one the first word
in the table in this case the first word
is hello
and N is the number of different words
in the phrases in this case n equals 2
because we have two words hello and
world
lastly A and B refer to the two phrases
in this case let's let a be the first
phrase hello world and B be the second
phrase hello
now when I equals 1 we plug in the word
counts for the first word hello
and when I equals 2 we plug in the word
counts for the second word world
and when we do the math we get 0.71
which is the same value we got when we
took the cosine of 45 degrees
bam
note when we only have two different
words in our table like we do here then
we can easily plot the points on a
two-dimensional graph because we can put
one word like hello on the x-axis the
First Dimension and the other word world
on the y-axis the second dimension
but when we have phrases with more than
two different words then we need more
than two Dimensions to plot the points
for example if we wanted to calculate
the cosine similarity for these two
phrases where the first phrase I love
Troll 2 is phrase a and the second
phrase I love Jim Kata is phrase B
then the table will have five different
words in it
and that means we would need a five
dimensional graph to plot the points and
I have no idea how to draw five
dimensional graph
the good news is that this is another
way that having the equation for the
cosine similarity can come in handy
rather than worry about how to draw a
five-dimensional graph we just plug the
numbers into the equation
then we do the math and we get 0.58
so the cosine similarity between these
two phrases is 0.58
triple bam
in summary the cosine similarity is a
relatively easy to calculate metric that
can tell us how similar or different
things are
note if you haven't already seen it Jim
Kata is an awesome 1985 movie about a
gymnast he uses the parallel bars to
defeat his enemies in Mortal Kombat bam
now it's time for some Shameless
self-promotion if you want to review
statistics and machine learning offline
check out the stackquest PDF study
guides and my book The stackquest
Illustrated guide to machine learning at
stackquest.org there's something for
everyone
hooray we've made it to the end of
another exciting stat Quest if you like
this stat Quest and want to see more
please subscribe and if you want to
support stat Quest consider contributing
to my patreon campaign becoming a
channel member buying one or two of my
original songs or a t-shirt or a hoodie
or just donate the links are in the
description below alright until next
time Quest on