如何从文本数据中获取词袋? [关闭]

2024-01-11

我正在使用大型文本数据集来研究预测问题。我正在实施词袋模型。 http://en.wikipedia.org/wiki/Bag-of-words_model

获得词袋的最佳方式应该是什么?现在,我有tf-idf http://en.wikipedia.org/wiki/Tf%E2%80%93idf各种单词的数量以及单词数量太大,无法用于进一步的作业。如果我使用 tf-idf 标准,获取词袋的 tf-idf 阈值应该是多少?或者我应该使用其他一些算法。我正在使用Python。


使用集合.计数器类 http://docs.python.org/dev/library/collections#collections.Counter

>>> import collections, re
>>> texts = ['John likes to watch movies. Mary likes too.',
             'John also likes to watch football games.']
>>> bagsofwords = [collections.Counter(re.findall(r'\w+', txt))
                   for txt in texts]
>>> bagsofwords[0]
Counter({'likes': 2, 'watch': 1, 'Mary': 1, 'movies': 1, 'John': 1, 'to': 1, 'too': 1})
>>> bagsofwords[1]
Counter({'watch': 1, 'games': 1, 'to': 1, 'likes': 1, 'also': 1, 'John': 1, 'football': 1})
>>> sumbags = sum(bagsofwords, collections.Counter())
>>> sumbags
Counter({'likes': 3, 'watch': 2, 'John': 2, 'to': 2, 'games': 1, 'football': 1, 'Mary': 1, 'movies': 1, 'also': 1, 'too': 1})
>>> 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从文本数据中获取词袋? [关闭] 的相关文章

随机推荐