Bag-of-words model: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Compvis (talk | contribs)
corrections related to the fact that a bag is a multiset, for which multiplicity is significant
Line 3: Line 3:
{{technical|date=February 2013}}
{{technical|date=February 2013}}


The '''bag-of-words model''' is a simplifying representation used in [[natural language processing]] and [[information retrieval]] (IR). In this model, a text (such as a sentence or a document) is represented as an unordered collection of words, disregarding grammar and even word order. Recently, the bag-of-words model has also been used for [[computer vision]].<ref name=sivic>{{cite conference
The '''bag-of-words model''' is a simplifying representation used in [[natural language processing]] and [[information retrieval]] (IR). In this model, a text (such as a sentence or a document) is represented as the [[multiset|bag (multiset)]] of its words, disregarding grammar and even word order but keeping multiplicity. Recently, the bag-of-words model has also been used for [[computer vision]].<ref name=sivic>{{cite conference
| first = Josef
| first = Josef
| last = Sivic
| last = Sivic
Line 61: Line 61:
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
[1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
[1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
[1, 1, 1, 1, 0, 1, 1, 1, 0, 0]
[1, 2, 1, 1, 0, 1, 1, 1, 0, 0]
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:20, 29 November 2013

The bag-of-words model is a simplifying representation used in natural language processing and information retrieval (IR). In this model, a text (such as a sentence or a document) is represented as the bag (multiset) of its words, disregarding grammar and even word order but keeping multiplicity. Recently, the bag-of-words model has also been used for computer vision.[1]

The bag-of-words model is commonly used in methods of document classification, where the (frequency of) occurrence of each word is used as a feature for training a classifier.

An early reference to "bag of words" in a linguistic context can be found in Zellig Harris's 1954 article on Distributional Structure.[2]

Example implementation

The following models a text document using bag-of-words.

Here are two simple text documents:

John likes to watch movies. Mary likes too.
John also likes to watch football games.

Based on these two text documents, a dictionary is constructed as:

{
    "John": 1,
    "likes": 2,
    "to": 3,
    "watch": 4,
    "movies": 5,
    "also": 6,
    "football": 7,
    "games": 8,
    "Mary": 9,
    "too": 10
}

which has 10 distinct words. And using the indexes of the dictionary, each document is represented by a 10-entry vector:

[1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
[1, 2, 1, 1, 0, 1, 1, 1, 0, 0]

where each entry of the vectors refers to count of the corresponding entry in the dictionary (this is also the histogram representation). This vector representation does not preserve the order of the words in the original sentences. This kind of representation has several successful applications, for example email filtering.[1]

Term weighting

In the example above, the document vectors contain term frequencies. In both IR and text classification, it is common to weigh terms by various schemes, the most of popular of which is tf–idf. For the specific purpose of classification, supervised alternatives have been developed that take into account the class label of a document.[3]

Hashing trick

A common alternative to the use of dictionaries is the hashing trick, where words are directly mapped to indices with a hashing function.[4] By mapping words to indices directly with a hash function, no memory is required to store a dictionary. Hash collisions are typically dealt with by using freed-up memory to increase the number of hash buckets. In practice, hashing greatly simplifies the implementation of bag-of-words models and improves their scalability.

Example usage: spam filtering

In Bayesian spam filtering, an e-mail message is modeled as an unordered collection of words selected from one of two probability distributions: one representing spam and one representing legitimate e-mail ("ham"). Imagine that there are two literal bags full of words. One bag is filled with words found in spam messages, and the other bag is filled with words found in legitimate e-mail. While any given word is likely to be found somewhere in both bags, the "spam" bag will contain spam-related words such as "stock", "Viagra", and "buy" much more frequently, while the "ham" bag will contain more words related to the user's friends or workplace.

To classify an e-mail message, the Bayesian spam filter assumes that the message is a pile of words that has been poured out randomly from one of the two bags, and uses Bayesian probability to determine which bag it is more likely to be.

See also

References

  1. ^ a b Sivic, Josef (April, 2009). "Efficient visual search of videos cast as text retrieval" (PDF). IEEE TRANSACTIONS ON PATTERN ANALYSIS AND MACHINE INTELLIGENCE, VOL. 31, NO. 4. IEEE. pp. 591–605. {{cite conference}}: Check date values in: |date= (help); Unknown parameter |booktitle= ignored (|book-title= suggested) (help)
  2. ^ Harris, Zellig (1954). "Distributional Structure". Word. 10 (2/3): 146–62. And this stock of combinations of elements becomes a factor in the way later choices are made ... for language is not merely a bag of words but a tool with particular properties which have been fashioned in the course of its use
  3. ^ Youngjoong Ko (2012). "A study of term weighting schemes using class information for text classification". SIGIR'12. ACM. {{cite conference}}: Unknown parameter |booktitle= ignored (|book-title= suggested) (help)
  4. ^ Weinberger, K. Q. (2009). "Feature hashing for large scale multitask learning," (PDF). Proceedings of the 26th Annual International Conference on Machine Learning: 1113–1120. {{cite journal}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)