Clustering Algorithms Applications Hierarchical Clustering k Means 1

  • Slides: 33
Download presentation
Clustering Algorithms Applications Hierarchical Clustering k -Means 1

Clustering Algorithms Applications Hierarchical Clustering k -Means 1

The Problem of Clustering u. Given a set of points, with a notion of

The Problem of Clustering u. Given a set of points, with a notion of distance between points, group the points into some number of clusters, such that: Intra-cluster distances are minimized Inter-cluster distances are maximized 2

Example: Clustering CD’s u. Intuitively: music divides into categories, and customers prefer a few

Example: Clustering CD’s u. Intuitively: music divides into categories, and customers prefer a few categories. w But what are categories really? u. Represent a CD by the customers who bought it. u. Similar CD’s have similar sets of customers, and vice-versa. 3

The Space of CD’s u. Think of a space with one dimension for each

The Space of CD’s u. Think of a space with one dimension for each customer. w Values in a dimension may be 0 or 1 only. u. A CD’s point in this space is x 2, …, xk), where xi = 1 iff the i customer bought the CD. th ( x 1 , 4

Space of CD’s – (2) u. For Amazon, the dimension count is tens of

Space of CD’s – (2) u. For Amazon, the dimension count is tens of millions. u. An alternative: use minhashing/LSH to get Jaccard similarity between “close” CD’s. u 1 minus Jaccard similarity can serve as a (non-Euclidean) distance. 5

Example: Clustering Documents u. Represent a document by a vector (x 1, x 2,

Example: Clustering Documents u. Represent a document by a vector (x 1, x 2, …, xk), where xi = 1 iff the i th word (in some order) appears in the document. u. Documents with similar sets of words may be about the same topic. 6

Example: DNA Sequences u. Objects are sequences of {C, A, T, G}. u. Distance

Example: DNA Sequences u. Objects are sequences of {C, A, T, G}. u. Distance between sequences is edit distance, w Minimum number of inserts and deletes needed to turn one into the other. u. Note there is a “distance, ” but no convenient space in which points “live. ” 7

Distance Measures u. A Euclidean space has some number of real -valued dimensions. w

Distance Measures u. A Euclidean space has some number of real -valued dimensions. w Based on locations of points in such a space. u. A Non-Euclidean distance is based on properties of points, but not their “location” in a space. 8

Axioms of a Distance Measure ud is a distance measure if it is a

Axioms of a Distance Measure ud is a distance measure if it is a function from pairs of objects to real numbers such that: 1. d(x, y) > 0. 2. d(x, y) = 0 iff x = y. 3. d(x, y) = d(y, x). 4. d(x, y) < d(x, z) + d(z, y) (triangle inequality ). 9

Some Euclidean Distances u L 2 norm : d(x, y) = square root of

Some Euclidean Distances u L 2 norm : d(x, y) = square root of the sum of the squares of the differences between x and y in each dimension. u L 1 norm : sum of the differences in each dimension. w Manhattan distance = distance if you had to travel along coordinates only. y = (9, 8) L 2 -norm: dist(x, y) = (42+32) =5 5 4 x = (5, 5) 3 L 1 -norm: dist(x, y) = 4+3 = 7 10

Non-Euclidean Distances u. Jaccard distance for sets = 1 minus ratio of sizes of

Non-Euclidean Distances u. Jaccard distance for sets = 1 minus ratio of sizes of intersection and union. u. Cosine distance = angle between vectors from the origin to the points in question. u. Edit distance = number of inserts and deletes to change one string into another. 11

Jaccard Distance for Sets (Bit-Vectors) u. Example: p 1 = 10111; p 2 =

Jaccard Distance for Sets (Bit-Vectors) u. Example: p 1 = 10111; p 2 = 10011. w Size of intersection = 3; size of union = 4, Jaccard similarity (not distance) = 3/4. w d(x, y) = 1 – (Jaccard similarity) = 1/4. 12

Why J. D. Is a Distance Measure ud(x, x) = 0 because x x

Why J. D. Is a Distance Measure ud(x, x) = 0 because x x = x x. ud(x, y) = d(y, x) because union and intersection are symmetric. ud(x, y) > 0 because |x y| < |x y|. ud(x, y) < d(x, z) + d(z, y) trickier. . . (1 - |x z|/|x z|) + (1 - |y z|/|y z|) 1 - |x y|/|x y| w Remember: |a b|/|a b| = probability that minhash(a) = minhash(b) 1 - |a b|/|a b| = prob. that minhash(a) minhash(b). w Claim: prob[mh(x) mh(y)] prob[mh(x) mh(z)] + prob[mh(z) mh(y)] w Proof: Whenever mh(x) mh(y), at least one of mh(x) mh(z) 13 and mh(z) mh(y) must be true.

Clustering with JD {a, b, d, e} {a, b, c} {d, e, f} {b,

Clustering with JD {a, b, d, e} {a, b, c} {d, e, f} {b, c, e, f} Similarity threshold = 1/3; distance < 2/3 14

Cosine Distance u. Think of a point as a vector from the origin (0,

Cosine Distance u. Think of a point as a vector from the origin (0, 0, …, 0) to its location. u. Two points’ vectors make an angle, whose cosine is the normalized dot-product of the vectors: p 1. p 2/|p 2||p 1|. w Example: p 1 = 00111; p 2 = 10011. w p 1. p 2 = 2; |p 1| = |p 2| = 3. w cos( ) = 2/3; is about 48 degrees. • d (p 1, p 2) = = arccos(p 1. p 2/|p 2||p 1|) 15

Why C. D. Is a Distance Measure ud(x, x) = 0 because arccos(1) =

Why C. D. Is a Distance Measure ud(x, x) = 0 because arccos(1) = 0. ud(x, y) = d(y, x) by symmetry. ud(x, y) > 0 because angles are chosen to be in the range 0 to 180 degrees. u. Triangle inequality: physical reasoning. If I rotate an angle from x to z and then from z to y, I can’t rotate less than from x to y. 16

Edit Distance u. The edit distance of two strings is the number of inserts

Edit Distance u. The edit distance of two strings is the number of inserts and deletes of characters needed to turn one into the other. Example ux = abcde ; y = bcduve. u. Turn x into y by deleting a, then inserting u and v after d. w d(x, y) = 3. u. Or, longest common subsequence: LCS(x, y) = bcde. u. Note: d(x, y) = |x| + |y| - 2|LCS(x, y)| = 5 + 6 – 2*4 = 3 = edit dist. 17

Why Edit Distance Is a Distance Measure ud(x, x) = 0 because 0 edits

Why Edit Distance Is a Distance Measure ud(x, x) = 0 because 0 edits suffice. ud(x, y) = d(y, x) because insert/delete are inverses of each other. ud(x, y) > 0: no notion of negative edits. u. Triangle inequality: changing x to z and then to y is one way to change x to y. 18

Methods of Clustering u. Hierarchical (Agglomerative): w Initially, each point in cluster by itself.

Methods of Clustering u. Hierarchical (Agglomerative): w Initially, each point in cluster by itself. w Repeatedly combine the two “nearest” clusters into one. u. Point Assignment: w Maintain a set of clusters. w Place points into their “nearest” cluster. 19

Hierarchical Clustering u Two important questions: 1. How do you determine the “nearness” of

Hierarchical Clustering u Two important questions: 1. How do you determine the “nearness” of clusters? 2. How do you represent a cluster of more than one point? 20

Hierarchical Clustering – (2) u. Key problem: as you build clusters, how do you

Hierarchical Clustering – (2) u. Key problem: as you build clusters, how do you represent the location of each cluster, to tell which pair of clusters is closest? u. Euclidean case: each cluster has a centroid = average of its points. w Measure intercluster distances by distances of centroids. 21

Example (5, 3) o (1, 2) o x (1. 5, 1. 5) x (1,

Example (5, 3) o (1, 2) o x (1. 5, 1. 5) x (1, 1) o (2, 1) o (0, 0) x (4. 7, 1. 3) o (4, 1) x (4. 5, 0. 5) o (5, 0) 22

And in the Non-Euclidean Case? u. The only “locations” we can talk about are

And in the Non-Euclidean Case? u. The only “locations” we can talk about are the points themselves. w I. e. , there is no “average” of two points. u. Approach 1: clustroid = point “closest” to other points. w Treat clustroid as if it were centroid, when computing intercluster distances. 23

“Closest” Point? u Possible meanings: 1. Smallest maximum distance to the other points. 2.

“Closest” Point? u Possible meanings: 1. Smallest maximum distance to the other points. 2. Smallest average distance to other points. 3. Smallest sum of squares of distances to other points. 4. Etc. , etc. 24

Example clustroid 1 2 6 3 4 5 clustroid intercluster distance 25

Example clustroid 1 2 6 3 4 5 clustroid intercluster distance 25

Other Approaches to Defining “Nearness” of Clusters u. Approach 2: intercluster distance = minimum

Other Approaches to Defining “Nearness” of Clusters u. Approach 2: intercluster distance = minimum of the distances between any two points, one from each cluster. u. Approach 3: Pick a notion of “cohesion” of clusters, e. g. , maximum distance from the clustroid. w Merge clusters whose union is most cohesive. 26

k – Means Algorithm(s) u. Assumes Euclidean space. u. Start by picking k, the

k – Means Algorithm(s) u. Assumes Euclidean space. u. Start by picking k, the number of clusters. u. Initialize clusters by picking one point per cluster. w Example: pick one point at random, then k -1 other points, each as far away as possible from the previous points. 27

Populating Clusters 1. For each point, place it in the cluster whose current centroid

Populating Clusters 1. For each point, place it in the cluster whose current centroid it is nearest. 2. After all points are assigned, fix the centroids of the k clusters. 3. Optional: reassign all points to their closest centroid. w Sometimes moves points between clusters. 28

Example: Assigning Clusters 2 Reassigned points 4 6 7 5 x 3 1 x

Example: Assigning Clusters 2 Reassigned points 4 6 7 5 x 3 1 x 8 Clusters after first round 29

Getting k Right u. Try different k, looking at the change in the average

Getting k Right u. Try different k, looking at the change in the average distance to centroid, as k increases. u. Average falls rapidly until right k, then changes little. Average distance to centroid Best value of k k 30

Example: Picking k Too few; many long distances to centroid. x x x x

Example: Picking k Too few; many long distances to centroid. x x x x xx x x x x x x 31

Example: Picking k Just right; distances rather short. x x x x xx x

Example: Picking k Just right; distances rather short. x x x x xx x x x x x x 32

Example: Picking k Too many; little improvement in average x distance. x x x

Example: Picking k Too many; little improvement in average x distance. x x x xx x xx x x x 33