5
Similarity and Distance For many different problems we need to quantify how close two objects are. Examples: For an item bought by a customer, find other similar items. Group together the customers of a site so that similar customers are shown the same ad. Group together web documents so that you can separate the ones that talk about politics and the ones that talk about sports. Find all the near-duplicate mirrored web documents. Find credit card transactions that are very different from previous transactions. To solve these problems we need a definition of similarity, or distance. The definition depends on the type of data that we have.

6
Similarity Numerical measure of how alike two data objects are. A function that maps pairs of objects to real values. Higher when objects are more alike. Often falls in the range [0,1], sometimes in [-1,1] Desirable properties for similarity 1. s(p, q) = 1 (or maximum similarity) only if p = q. (Identity). 2. s(p, q) = s(q, p) for all p and q. (Symmetry).

7
Similarity between sets Consider the following documents Which ones are more similar? How would you quantify their similarity? apple releases new ipod apple releases new ipad new apple pie recipe

11
Similarity between vectors documentAppleMicrosoftObamaElection D D D D Documents (and sets in general) can also be represented as vectors How do we measure the similarity of two vectors? We could view them as sets of words. Jaccard Similarity will show that D4 is different form the rest. But all pairs of the other three documents are equally similar. We want to capture how well the two vectors are aligned.

12
Example Documents D1, D2 are in the “same direction”. Document D3 is on the same plane as D1, D2. Document D3 is orthogonal to the rest. documentAppleMicrosoftObamaElection D D D D apple microsoft {Obama, election}

13
Example Documents D1, D2 are in the “same direction”. Document D3 is on the same plane as D1, D2. Document D4 is orthogonal to the rest. documentAppleMicrosoftObamaElection D11/32/300 D21/32/300 D32/31/300 D4001/32/3 apple microsoft {Obama, election}

14
Cosine Similarity Sim(X,Y) = cos(X,Y) The cosine of the angle between X and Y. If the vectors are aligned (correlated) angle is zero degrees and cos(X,Y)=1. If the vectors are orthogonal (no common coordinates) angle is 90 degrees and cos(X,Y) = 0. Cosine is commonly used for comparing documents, where we assume that the vectors are normalized by the document length.

17
Distance Numerical measure of how different two data objects are A function that maps pairs of objects to real values. Lower when objects are more alike. Higher when two objects are different. Minimum distance is 0, when comparing an object with itself. Upper limit varies.

19
Triangle Inequality Triangle inequality guarantees that the distance function is well-behaved. The direct connection is the shortest distance. It is useful also for proving useful properties about the data.

20
Distances for real vectors L p norms are known to be distance metrics

23
L p distances for sets We can apply all the L p distances to the cases of sets of attributes, with or without counts, if we represent the sets as vectors. E.g., a transaction is a 0/1 vector. E.g., a document is a vector of counts.

24
Similarities into distances

25
25 Why Jaccard Distance Is a Distance Metric JDist(x,x) = 0 since JSim(x,x) = 1 JDist(x,y) = JDist(y,x) by symmetry of intersection. JDist(x,y) > 0 since intersection of X,Y cannot be bigger than the union. Triangle inequality: Follows from the fact that JSim(X,Y) is the probability of randomly selected element from the union of X and Y to belong to the intersection.

26
26 Hamming Distance Hamming distance is the number of positions in which bit-vectors differ. Example: p 1 = p 2 = d(p 1, p 2 ) = 2 because the bit-vectors differ in the 3 rd and 4 th positions. The L 1 norm for the binary vectors. Hamming distance between two vectors of categorical attributes is the number of positions in which they differ. Example: x = (married, low income, cheat), y = (single, low income, not cheat) d(x,y) = 2

27
27 Why Hamming Distance Is a Distance Metric d(x,x) = 0 since no positions differ. d(x,y) = d(y,x) by symmetry of “different from.” d(x,y) > 0 since strings cannot differ in a negative number of positions. Triangle inequality: changing x to z and then to y is one way to change x to y. For binary vectors if follows from the fact that L 1 norm is a metric.

28
Distance between strings How do we define similarity between strings? Important for recognizing and correcting typing errors and analyzing DNA sequences. weird wierd intelligentunintelligent AthenaAthina

29
29 Edit Distance for strings The edit distance of two strings is the number of inserts and deletes of characters needed to turn one into the other. Example: x = abcde ; y = bcduve. Turn x into y by deleting a, then inserting u and v after d. Edit distance = 3. Minimum number of operations can be computed using dynamic programming. Common distance measure for comparing DNA sequences.

30
30 Why Edit Distance Is a Distance Metric d(x,x) = 0 because 0 edits suffice. d(x,y) = d(y,x) because insert/delete are inverses of each other. d(x,y) > 0: no notion of negative edits. Triangle inequality: changing x to z and then to y is one way to change x to y. The minimum is no more than that.

31
31 Variant Edit Distances Allow insert, delete, and mutate. Change one character into another. Minimum number of inserts, deletes, and mutates also forms a distance measure. Same for any set of operations on strings. Example: substring reversal or block transposition OK for DNA sequences. Example: character transposition is used for spelling.

33
Why is similarity important? We saw many definitions of similarity and distance. How do we make use of similarity in practice? What issues do we have to deal with?

34
APPLICATIONS OF SIMILARITY: RECOMMENDATION SYSTEMS

35
An important problem Recommendation systems When a user buys an item (initially books) we want to recommend other items that the user may like. When a user rates a movie, we want to recommend movies that the user may like. When a user likes a song, we want to recommend other songs that they may like. A big success of data mining Exploits the long tail How Into Thin Air made Touching the Void popular.

40
Approach Map items into a feature space: For movies: Actors, directors, genre, rating, year,… Challenge: make all features compatible. For documents? To compare items with users we need to map users to the same feature space. How? Take all the movies that the user has seen and take the average vector Other aggregation functions are also possible. Recommend to user C the most similar item i computing similarity in the common feature space Distributional distance measures also work well.

41
Limitations of content-based approach Finding the appropriate features e.g., images, movies, music. Overspecialization Never recommends items outside user’s content profile. People might have multiple interests. Recommendations for new users How to build a profile?

42
Collaborative filtering Harry Potter 1 Harry Potter 2 Harry Potter 3 TwilightStar Wars 1 Star Wars 2 Star Wars 3 A451 B554 C245 D33 Two users are similar if they rate the same items in a similar way. Recommend to user C, the items liked by many of the most similar users.

47
User-User Collaborative Filtering Consider user c. Find set D of other users whose ratings are most “similar” to c’s ratings. Estimate user’s ratings based on ratings of users in D using some aggregation function. Advantage: for each user we have small amount of computation.

48
Item-Item Collaborative Filtering We can transpose (flip) the matrix and perform the same computation as before to define similarity between items Intuition: Two items are similar if they are rated in the same way by many users. Better defined similarity since it captures the notion of genre of an item Users may have multiple interests. Algorithm: For each user c and item i Find the set D of most similar items to item i that have been rated by user c. Aggregate their ratings to predict the rating for item i. Disadvantage: we need to consider each user-item pair separately.

49
Pros and cons of collaborative filtering Works for any kind of item No feature selection needed. New user problem. New item problem. Sparsity of rating matrix Cluster-based smoothing?