lecture 3 : term weighting

52
Introduction to Information Retrieval Lecture 3 : Term Weighting 楊楊楊楊楊 楊楊楊楊楊楊楊 [email protected] 楊楊楊楊楊楊楊 Introduction to Information Retrieval 楊楊楊楊 楊 Ch 6 1

Upload: rumor

Post on 06-Feb-2016

93 views

Category:

Documents


0 download

DESCRIPTION

Lecture 3 : Term Weighting. 楊立偉教授 台灣科大資管系 wyang @ntu.edu.tw 本投影片修改自 Introduction to Information Retrieval 一書之 投影片 Ch 6. Ranked Retrieval. Ranked retrieval. Boolean retrieval return d ocuments either match or don’t. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Lecture 3 : Term Weighting

楊立偉教授台灣科大資管系[email protected]

本投影片修改自 Introduction to Information Retrieval 一書之投影片 Ch 6

1

Page 2: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Ranked Retrieval

2

Page 3: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

3

Ranked retrieval

Boolean retrieval return documents either match or don’t.

Good for expert users with precise understanding of their

needs and of the collection.

Not good for the majority of users

Most users are not capable of writing Boolean queries

Most users don’t want to go through 1000s of results.

This is particularly true of web search.

3

Page 4: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

4

Problem with Boolean search

Boolean queries often result in either too few (=0) or too

many (1000s) results.

Example query : [standard user dlink 650]

→ 200,000 hits

Example query : [standard user dlink 650 no card found]

→ 0 hits

It takes a lot of skills to give a proper Boolean query.

4

Page 5: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

5

Ranked retrieval

With ranking, large result sets are not an issue.

More relevant results are ranked higher than less relevant

results.

The user may decide how many results he/she wants.

5

Page 6: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

6

Scoring as the basis of ranked retrieval

Assign a score to each query-document pair, say in [0, 1], to

measure how well document and query “match”.

If the query term does not occur in the document: score

should be 0.

The more frequent the query term in the document, the

higher the score

6

Page 7: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

7

Basic approach : using Jaccard coefficient

Example

What is the query-document match score that the Jaccard

coefficient computes for:

Query: “ides of March”

Document “Caesar died in March”

JACCARD(q, d) = 1/6

7

Page 8: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

8

3 drawbacks of the basic approach

1. It doesn’t consider term frequency

(how many occurrences a term has).

2. Rare terms are more informative than frequent terms.

Jaccard does not consider this information.

3. Need a more sophisticated way of normalizing for the length of a document.

use (cosine)

instead of |A ∩ B|/|A ∪ B| (Jaccard) for length normalization.

8

Page 9: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

9

Example

Cosine

Jaccard|A ∩ B|/|A ∪ B|

9

32 /2C

3 / 2C

C}B,{A,Y

B}{A,X

C

J

Jaccard favors more overlappingthan length normalization.

Page 10: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Term Frequency

10

Page 11: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

11

Binary incidence matrix

Each document is represented as a binary vector {0, 1}∈ |V|.

11

Anthony and Cleopatra

Julius Caesar

The Tempest

Hamlet Othello Macbeth . . .

ANTHONYBRUTUS CAESARCALPURNIACLEOPATRAMERCYWORSER. . .

1110111

1111000

0000011

0110011

0010011

1010010

Page 12: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

12

Count matrix

Each document is now represented as a count vector N∈ |V|.

12

Anthony and Cleopatra

Julius Caesar

The Tempest

Hamlet Othello Macbeth . . .

ANTHONYBRUTUS CAESARCALPURNIACLEOPATRAMERCYWORSER. . .

1574

2320

5722

73157227

10000

0000031

0220081

0010051

1000085

Page 13: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

13

Bag of words model

Do not consider the order of words in a document. John is quicker than Mary , and

Mary is quicker than John are represented the same way.

Note: Positional index can distinguish the order.

13

Page 14: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

14

Term frequency tf The term frequency tft,d of term t in document d is defined

as the number of times that t occurs in d. Use tf when computing query-document match scores. But Relevance does not increase proportionally with term

frequency. Example

A document with tf = 10 occurrences of the term is more relevant than a document with tf = 1 occurrence of the term, but not 10 times more relevant.

14

Page 15: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

15

Log frequency weighting The log frequency weight of term t in d is defined as follows

tft,d → wt,d : 0 → 0, 1 → 1, 2 → 1.3, 10 → 2, 1000 → 4, etc.

Why use log ? 在數量少時 , 差 1 即差很多;但隨著數量越多,差 1 的影響變

得越小

tf-matching-score(q, d) = t∈q∩d (1 + log tft,d )15

Page 16: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

16

Exercise

Compute the Jaccard matching score and the tf matching score for the following query-document pairs.

q: [information on cars] d: “all you have ever wanted to know about cars”jaccard = 1 / 11, tf = 1+log1

q: [information on cars] d: “information on trucks, information on planes, information on trains”jaccard = 2 / 6, tf = (1+log3) + (1+log3)

q: [red cars and red trucks] d: “cops stop red cars more often”jaccard = 2 / 8, tf = (1+log1) + (1+log1)

16

Page 17: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

TF-IDF Weighting

17

Page 18: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

18

Frequency in document vs. Frequency in collection

In addition to term frequency (the frequency of the term in

the document), we also want to use the frequency of the

term in the collection for weighting and ranking.

18

Page 19: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

19

Desired weight for rare terms

Rare terms are more informative than frequent terms.

Consider a term in the query that is rare in the collection

(e.g., ARACHNOCENTRIC).

A document containing this term is very likely to be relevant.

→ We want high weights for rare terms like

ARACHNOCENTRIC.

19

Page 20: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

20

Desired weight for frequent terms

Frequent terms are less informative than rare terms.

Consider a term in the query that is frequent in the

collection (e.g., GOOD, INCREASE, LINE).

→ common term or 無鑑別力的詞

20

Page 21: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

21

Document frequency

We want high weights for rare terms like ARACHNOCENTRIC.

We want low (positive) weights for frequent words like

GOOD, INCREASE and LINE.

We will use document frequency to factor this into

computing the matching score.

The document frequency is the number of documents in

the collection that the term occurs in.

21

Page 22: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

22

idf weight dft is the document frequency, the number of documents

that t occurs in. dft is an inverse measure of the informativeness of term t. We define the idf weight of term t as follows:

(N is the number of documents in the collection.) idft is a measure of the informativeness of the term.

[log N/dft ] instead of [N/dft ] to balance the effect of idf

(i.e. use log for both tf and df)

22

Page 23: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

23

Examples for idf Compute idft using the formula:

23

term dft idft

calpurniaanimalsundayflyunderthe

1100

100010,000

100,0001,000,000

643210

Page 24: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

24

Collection frequency vs. Document frequency

Collection frequency of t: number of tokens of t in the collection

Document frequency of t: number of documents t occurs in Document/collection frequency weighting is computed

from known collection, or estimated需進行全域統計或採估計值

Which word is a more informative ?

24

word collection frequency document frequency

INSURANCETRY

1044010422

39978760

Page 25: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Example

• cf 出現次數 與 df 文件數。差異範例如下:

Word cf 出現總次數 df 出現文件數

ferrari 1042217 ← 較高的稀有性 ( 高資訊量 )

insurance 104403997

Page 26: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

26

tf-idf weighting The tf-idf weight of a term is the product of its tf weight

and its idf weight.

tf-weight idf-weight Best known weighting scheme in information retrieval

Note: the “-” in tf-idf is a hyphen, not a minus signAlternative names: tf.idf , tf x idf

26

Page 27: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

27

Summary: tf-idf

Assign a tf-idf weight for each term t in each document d:

The tf-idf weight . . .

. . . increases with the number of occurrences within a

document. (term frequency)

. . . increases with the rarity of the term in the collection.

(inverse document frequency)

27

Page 28: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

28

Exercise: Term, collection and document frequency

Relationship between df and cf? Relationship between tf and cf? Relationship between tf and df?

28

Quantity Symbol Definition

term frequency

document frequency

collection frequency

tft,d

dft

cft

number of occurrences of t indnumber of documents in thecollection that t occurs intotal number of occurrences oft in the collection

Page 29: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Vector Space Model

29

Page 30: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

30

Binary incidence matrix

Each document is represented as a binary vector {0, 1}∈ |V|.

30

Anthony and Cleopatra

Julius Caesar

The Tempest

Hamlet Othello Macbeth . . .

ANTHONYBRUTUS CAESARCALPURNIACLEOPATRAMERCYWORSER. . .

1110111

1111000

0000011

0110011

0010011

1010010

Page 31: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

31

Count matrix

Each document is now represented as a count vector N∈ |V|.

31

Anthony and Cleopatra

Julius Caesar

The Tempest

Hamlet Othello Macbeth . . .

ANTHONYBRUTUS CAESARCALPURNIACLEOPATRAMERCYWORSER. . .

1574

2320

5722

73157227

10000

0000031

0220081

0010051

1000085

Page 32: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

32

Binary → count → weight matrix

Each document is now represented as a real-valued vector of tf idf weights R∈ |V|.

32

Anthony and Cleopatra

Julius Caesar

The Tempest

Hamlet Othello Macbeth . . .

ANTHONYBRUTUS CAESARCALPURNIACLEOPATRAMERCYWORSER. . .

5.251.218.59

0.02.851.511.37

3.186.102.541.54

0.00.00.0

0.00.00.00.00.0

1.900.11

0.01.0

1.510.00.0

0.124.15

0.00.0

0.250.00.0

5.250.25

0.350.00.00.00.0

0.881.95

Page 33: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

33

Documents as vectors

Each document is now represented as a real-valued vector of tf-idf weights R∈ |V|.

So we have a |V|-dimensional real-valued vector space. Terms are axes of the space. Documents are points or vectors in this space.

Each vector is very sparse - most entries are zero. Very high-dimensional: tens of millions of dimensions when

apply this to web (i.e. too many different terms on web)

33

Page 34: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

34

Queries as vectors Do the same for queries: represent them as vectors in the

high-dimensional space Rank documents according to their proximity to the query

proximity = similarity ≈ negative distance Rank relevant documents higher than

nonrelevant documents

34

Page 35: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Vector Space Model

• 將文件透過一組詞與其權重,將文件轉化為空間中的向量(或點),因此可以– 計算文件相似性或文件距離

– 計算文件密度

– 找出文件中心

– 進行分群(聚類)

– 進行分類(歸類)

Page 36: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Vector Space Model• 假設只有 Antony 與 Brutus 兩個詞,文件可以向量表示如下

D1: Antony and Cleopatra = (13.1, 3.0)

D2: Julius Caesar = (11.4, 8.3)

• 計算文件相似性:以向量夾角表示用內積計算13.1x11.4 + 3.0 x 8.3

• 計算文件幾何距離:

term B

term A

*

*

D1

D2

θ

d

22 )3.80.3()4.111.13(

Page 37: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Applications of Vector Space Model• 分群 ( 聚類 ) Clustering :由最相近的文件開始合併• 分類 ( 歸類 ) Classification :挑選最相近的類別

• 中心 Centroid

可做為群集之代表或做為文件之主題

• 文件密度了解文件的分布狀況

term B

term A

**

**

**

*

**

*

*

*

*

**

***

**

*

*

*

*

* **

* ****

**

*

*

*

Cluster 1

Cluster 2

Cluster 3

centroid中心

待歸群(類)文件

Page 38: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Issues about Vector Space Model (1)

• 詞之間可能存有相依性,非垂直正交 (orthogonal)

– 假設有兩詞 tornado, apple 構成的向量空間,

D1=(1,0) D2=(0,1) ,其內積為 0 ,故稱完全不相似

– 但當有兩詞 tornado, hurricane 構成的向量空間,

D1=(1,0) D2=(0,1) ,其內積為 0 ,但兩文件是否真的不相似?

– 當詞為彼此有相依性 (dependence)

• 挑出正交(不相依)的詞

• 將維度進行數學轉換(找出正交軸)

Page 39: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Issues about Vector Space Model (2)• 詞可能很多,維度太高,讓內積或距離的計算變得很耗時

– 常用詞可能自數千至數十萬之間,造成高維度空間( 運算複雜度呈指數成長 , 又稱 curse of dimensionality)

– 常見的解決方法• 只挑選具有代表性的詞( feature selection )• 將維度進行數學轉換( latent semantic indexing )

Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth

Antony 13.1 11.4 0.0 0.0 0.0 0.0

Brutus 3.0 8.3 0.0 1.0 0.0 0.0

Caesar 2.3 2.3 0.0 0.5 0.3 0.3

Calpurnia 0.0 11.2 0.0 0.0 0.0 0.0

Cleopatra 17.7 0.0 0.0 0.0 0.0 0.0

mercy 0.5 0.0 0.7 0.9 0.9 0.3

worser 1.2 0.0 0.6 0.6 0.6 0.0

documentas a vector

termas axes

the dimensionality is 7

Page 40: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

40

Use angle instead of distance

Rank documents according to angle with query For example : take a document d and append it to itself.

Call this document d′. d ′ is twice as long as d. “Semantically” d and d′ have the same content. The angle between the two documents is 0, corresponding

to maximal similarity . . . . . . even though the Euclidean distance between the two

documents can be quite large.

40

Page 41: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

41

From angles to cosines

The following two notions are equivalent.

Rank documents according to the angle between query and

document in decreasing order

Rank documents according to cosine(query,document) in

increasing order

Cosine is a monotonically decreasing function of the angle

for the interval [0◦, 180◦]

41

Page 42: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

42

Cosine

42

Page 43: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

43

Length normalization A vector can be (length-) normalized by dividing each of its

components by its length – here we use the L2 norm:

This maps vectors onto the unit sphere . . .

. . . since after normalization:

As a result, longer documents and shorter documents have weights of the same order of magnitude.

Effect on the two documents d and d ′ (d appended to itself) : they have identical vectors after length-normalization.

43

Page 44: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

44

Cosine similarity between query and document

qi is the tf-idf weight of term i in the query.

di is the tf-idf weight of term i in the document. | | and | | are the lengths of and This is the cosine similarity of and . . . . . . or,

equivalently, the cosine of the angle between and

44

Page 45: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

45

Cosine for normalized vectors

For normalized vectors, the cosine is equivalent to the dot product or scalar product.

(if and are length-normalized).

45

Page 46: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

46

Cosine similarity illustrated

46

Page 47: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

47

Cosine: Example

term frequencies (counts)

47

term SaS PaP WHAFFECTIONJEALOUSGOSSIPWUTHERING

11510

20

58700

2011

638

How similar are these novels?

SaS: Sense and Sensibility 理性與感性PaP:Pride and Prejudice 傲慢與偏見WH: Wuthering Heights 咆哮山莊

Page 48: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

48

Cosine: Example

term frequencies (counts) log frequency weighting

(To simplify this example, we don't do idf weighting.)

48

term SaS PaP WHAFFECTIONJEALOUSGOSSIPWUTHERING

3.062.0

1.300

2.761.85

00

2.302.041.782.58

term SaS PaP WHAFFECTIONJEALOUSGOSSIPWUTHERING

11510

20

58700

2011

638

Page 49: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

49

Cosine: Example

log frequency weighting log frequency weighting & cosine normalization

49

term SaS PaP WH

AFFECTIONJEALOUSGOSSIPWUTHERING

3.062.0

1.300

2.761.85

00

2.302.041.782.58

term SaS PaP WHAFFECTIONJEALOUSGOSSIPWUTHERING

0.7890.5150.335

0.0

0.8320.5550.00.0

0.5240.4650.4050.588

cos(SaS,PaP) ≈ 0.789 0.832 + 0.515 0.555 + 0.335 0.0 + 0.0 0.0 ≈ ∗ ∗ ∗ ∗0.94.

cos(SaS,WH) ≈ 0.79 cos(PaP,WH) ≈ 0.69 Why do we have cos(SaS,PaP) > cos(SaS,WH)?

Page 50: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

50

Computing the cosine score

50

Page 51: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

51

Ranked retrieval in the Vector Space Model

Represent the query as a weighted tf-idf vector

Represent each document as a weighted tf-idf vector

Compute the cosine similarity between the query vector and

each document vector

Rank documents with respect to the query

Return the top K (e.g., K = 10) to the user

51

Page 52: Lecture 3 : Term Weighting

Introduction to Information RetrievalIntroduction to Information Retrieval

Conclusion

• Ranking search results is important (compared with

unordered Boolean results)

• Term frequency

• tf-idf ranking: best known traditional ranking scheme

• Vector space model: One of the most important formal

models for information retrieval

(along with Boolean and probabilistic models)52