Ludwig-Maximilians-Universitaet Muenchen 0.06.205 Institute for Informatics Prof. Dr. Volker Tresp Gregor Jossé Johannes Niedermayer Machine Learning and Data Mining Summer 205 Exercise Sheet Presentation of Solutions to the Exercise Sheet on the 08.07.205 Exercise - Document Distance Consider four documents from a document dataset, which has been mapped onto an lexicon of size M = 00 w.r.t. word frequency x i,j,2,... }. Let A denote the lexicon itself, i.e. j,...,m} : x A,j =. Let B be a document containing only the first word of A (x B, = j 2,...,M} : x B,j = 0). Let C contain the first 50 words of A, and, finally, let D contain the th to 60th word twice. a) Compute the pairwise distance of vectors A, B, C, D, w.r.t. the following distance measures: dist eucl (x, y) = ( M (x j Y j ) 2) /2 dist simple (x, y) = M i= ( I(x j = y j )) i= dist simple00 (x, y) = M F dist cos (x, y) = dist pearson (x, y) = ( I(x j = y j )) i= xt y x y xt ỹ x ỹ where I(condition) is the indicator function which is iff the condition is fulfilled and 0 otherwise, F is the number of components in which both vectors are 0, and x := x mean(x). b) How do the distances change if it is also known that the first fifty words are contained in 750 of the total N = 000 documents in the set, while all other words only appear in 5 documents? Remark: You know the term frequency, which measures the absolute frequency of words in a document. When there is additional information about the global frequency of a term (i.e., is it common or rare among all documents), it should also be taken into account. This is often done using the inverse document frequency: ( ) N idf j = log n j where N is the number of documents and n j is the number of documents in which the word j occurs. The measures tf and idf are often combined by multiplication: tf idf = tf idf. What does this measure reflect? Use this measure for this exercise.
Es handelt sich hier bei den x i,j um Term Frequencies tf (tf i,j : wie oft taucht Wort j in Dokument i auf?) Die Dokument-Vektoren sehen also also folgendermaßen aus: a) Distanzberechnung A = (x,..., x 00 ) = (,..., ) i = B = (x,..., x 00 ), mit x i = i [,50] C = (x,..., x 00 ), mit x i = 2 i [,60] D = (x,..., x 00 ), mit x i = Euklidische Distanz dist eukl (A, B) = A B = (ai b i ) 2 : dist eukl (X, X) = 0 dist eukl (A, B) = A B = (ai b i ) 2 = ( ) 2 + 99 ( 0) 2 = 99 = 9,95 dist eukl (A, C) = 50 ( ) 2 + 50 ( 0) 2 = 50 = 5 2 7,7 dist eukl (A, D) = 50 ( 2) 2 + (0 + 0) ( 0) 2 = 00 = 0 dist eukl (B, C) = ( ) 2 + 9 ( 0) 2 + 50 (0 0) 2 = 9 = 7 dist eukl (B, D) = ( 0) 2 + 9 (0 0) 2 + 50 (2 0) 2 = 20,77 dist eukl (C, D) = 0 ( 0) 2 + 0 (2 ) 2 + 0 (2 0) 2 + 0 (0 0) 2 = 0 = 9,9 2
Indikatorfunktion I(X) = Simple Distanz: dist simple (A, B) = M X = bzw X = true ( I(a j = b j )) j= dist simple (X, X) = ( I(x j = x j )) = 0 dist simple (A, B) = 00 00 j= ( I(a j = b j )) = [( I( = )) + 99 ( I( = 0))] = 00 = 99 [ + 99 ( 0)] = 00 00 =.99 dist simple (A, C) = 50 [50 ( I( = )) + 50 ( I( = 0))] = 00 00 =.5 dist simple (A, D) = 00 [50 ( I( = 0)) + 50 ( I( = 2))] = 00 00 = dist simple (B, C) = 9 [ ( I( = )) + 9 ( I(0 = )) + 50 ( I(0 = 0))] = 00 00 =.9 dist simple (B, D) = 5 [ ( I( = 0)) + 9 ( I(0 = 0)) + 50 ( I(0 = 2))] = 00 00 =.5 dist simple (C, D) = [0 ( I( = 0)) + 0 ( I( = 2)) + 0 ( I(0 = 2)+ 00 +0 ( I(0 = 0))] = 60 00 =.6 Simple 00 Distanz: dist simple00 (A, B) = M F j= ( I(a j = b j )) mit F := Anzahl der Variablen in denen beide Vektoren übereinstimmend gleich 0 sind. dist simple00 (X, X) = ( I(x j = x j )) = 0 dist simple00 (A, B) = 00 0 [( I( = )) + 99 ( I( = 0))] = dist simple(a, B) =.99 gleiches gilt für die anderen dist simple00 (A, X) dist simple00 (B, C) = [ ( I( = )) + 9 ( I( = 0)) + 50 ( I(0 = 0))] = 00 50 = 9 50 =.98 dist simple00 (B, D) = [( I( = 0)) + 9 ( I(0 = 0)) + 50 ( I(0 = 2))] = 00 9 = 5 5 = dist simple00 (C, D) = [0 ( I( = 0)) + 0 ( I( = 2)) + 0 ( I(0 = 2))+ 00 0 +0 ( I(0 = 0))] = 60 60 =
Cosinusdistanz: dist cos (A, B) = cos(a,b) = dist cos (X, X) = XT X X T X 2 = 0 AT B A B. dist cos (A, B) = = /0 =.9 00 2 2 50 dist cos (A, C) = = 5 00 50 2 5 2 = 2 =.29 50 2 dist cos (A, D) = 0 50 2 = 00 2 0 5 2 2 = 2 =.29 und zwar weil: dist cos (ax, by ) = dist cos (X, Y ) für positive Skalare a,b dist cos (B, C) = 2 5 2 = 00 0 5 2 2 = 2.86 dist cos (B, D) = 0 const = 0 2 dist cos (C, D) = 5 2 0 2 = 80 00 =.2 Pearsondistanz: dist pearson (A, B) = dist cos (Ã, B) = dist cos (A mean(a), B mean(b)) = (A mean(a))t (B mean(b)) (A T mean(a)) (B mean(b)). dist pearson (X, X) = dist cos ( X, X) = 0 dist pearson (A, X) = NA, weil à = 0 dist pearson (B, C) = dist pearson (B, D) =.0 dist pearson (C, D) =..95 9.005 + 50.005.99 2 + 99.0 2 50.5 2 + 50.5 =.5 2.99995 5.899 Noch zur Pearson-Distanz: Sind alle Elemente eines Vektors gleich, werden die Elemente des Vektors durch die Mittelwertbereinigung auf 0 gesetzt. Die Lï 2nge des Vektors ( x ) ist damit Null. Der Nenner der Kosinusdistanz wird dann ebenfalls 0 (Division by Zero). Semantisch wï 2re das der Winkel eines Vektors zu einem Punkt (Ursprung). Beispiel: A = (2, 2), x = 2 à = (2 x,...) = (0,0) A = 0 Sinn der ï 2 bung: unterschiedliche Distanzmaï 2e ergeben unterschiedliche Distanzen, welche Distanz die richtige ist, muss ï 2ber Experimente und/oder Spezialwissen entschieden werden.
Zusammenfassung: Euklidische Distanz [0, ] Simple Distanz [0,] A 0.000 9.950 7.07 0.000 A 0.000 0.990 0.500.000 B 9.950 0.000 7.000.77 B 0.990 0.000 0.90 0.50 C 7.07 7.000 0.000 9.87 C 0.500 0.90 0.000 0.600 D 0.000.77 9.87 0.000 D.000 0.50 0.600 0.000 Simple 00 Distanz [0,] Kosinusdistanz [0,2] A 0.000 0.990 0.500.000 B 0.990 0.000 0.980.000 C 0.500 0.980 0.000.000 D.000.000.000 0.000 Pearson Distanz [0, 2] A NaN NaN NaN NaN B NaN 0.000 0.899.0 C NaN 0.899 0.000 0.00 D NaN.0 0.00 0.000 A 0.000 0.900 0.29 0.29 B 0.900 0.000 0.859.000 C 0.29 0.859 0.000 0.200 D 0.29.000 0.200 0.000 b) Wenn Informationen über die globalen Häufigkeiten der einzelnen Wörter bekannt sind, sollten diese mit den Dokumentenvektoren verrechnet werden: üblich ist die inverse document frequency: ( ) idf j = log N. nj N ist die Anzahl aller Dokumente, n j die Anzahl aller Dokumente in denen Wort j vorkommt. Hieraus wird dann die tfidf gebildet: tfidf i,j =tf i,j idf j. In unserem Beispiel haben die ersten 50 Wörter eine idf j von log ( ) ( 000 750 = log ) 0,288, die anderen Wörter (j 5,...,00}) haben eine idf j von log ( ) 000 5 = log(200) 5,298. Wir erhalten also neue Merkmalsvektoren: log ( ) A = (x,..., x 00 ) =, mit x i = i =,...,50} log (200) sonst log ( ) B = (x,..., x 00 ), mit x i = i = log ( ) C = (x,..., x 00 ), mit x i = i [,50] 2 log ( ) i [,50] D = (x,..., x 00 ), mit x i = 2 log (200) i [5,60] Da also alle Merkmalsvektoren insgesamt heruntergewichtet wurden, ist zu erwarten, dass die Distanzen insgesamt abnehmen werden: 5
Distanzen: Euklidische Distanz A 0.000 7.59 7.65 7.520 B 7.59 0.000 2.0.708 C 7.65 2.0 0.000.57 D 7.520.708.57 0.000 Simple 00 Distanz A 0.000 0.990 0.500.000 B 0.990 0.000 0.980.000 C 0.500 0.980 0.000.000 D.000.000.000 0.000 Pearson Distanz A -0.000.0 2.000 0.7 B.0 0.000 0.899.02 C 2.000 0.899 0.000.266 D 0.7.02.266 0.000 Simple Distanz A 0.000 0.990 0.500.000 B 0.990 0.000 0.90 0.50 C 0.500 0.90 0.000 0.600 D.000 0.50 0.600 0.000 Kosinusdistanz A 0.000 0.992 0.96 0.55 B 0.992 0.000 0.859.000 C 0.96 0.859-0.000 0.90 D 0.55.000 0.90-0.000 Unterschiede: Euklidische Distanz: durch die Gewichtung deutliche Unterschiede in der Höhe der Distanzen auch die paarweisen Distanzen haben sich relativ zueinander verschoben. Beispiel AC, CD: ursprünglich ist AC < CD; jetzt umgekehrt. Simple Distanz / -00 Distanz: jeweils kein Einfluss klar, es werden lediglich Indikatorvektoren vergleichen, und da die Gewichtung hier keinen Einfluss auf die Wertidentitäten hatte (muss nicht immer der Fall sein), gibt es keine Änderungen. Kosinusdistanz: Starke Änderungen: da die Gewichtungen sich meist auf den Winkel der Vektoren auswirken nur BC und BD sind nicht betroffen. Pearsondistanz: Keine NaNs mehr, da A jetzt nicht Varianz 0 hat. Durch die Normalisierung hier auch ein Unterschied BD im Vergleich mit der Kosinusdistanz. (BC wird nicht beeinflusst, da die Gewichtungen sich nur auf Terme 0 auswirken.) 6