NLP---NLTK学习和初识word2vec + kaggle项目Bag of Words Meets Bags of Popcorn(bag _of_words_model)

 

 

全体stopwords列表 http://www.ranks.nl/stopwords

 

 

Word2Vect 实例:

https://www.zybuluo.com/hanxiaoyang/note/472184

 

Kaggle项目:https://www.kaggle.com/c/word2vec-nlp-tutorial

Bag-of-words 模型

之前教研室有个小伙伴在做文本方面的东西,经常提及词袋模型,只知道是文本表示的一种,可是最近看的关于CV的论文中也出现BoW模型,就很好奇BoW到底是个什么东西。

BoW起始可以理解为一种直方图统计,开始是用于自然语言处理和信息检索中的一种简单的文档表示方法。 和histogram 类似,BoW也只是统计频率信息,并没有序列信息。而和histogram不同的是,histogram一般统计的某个区间的频数,BoW是选择words字典,然后统计字典中每个单词出现的次数。
比如下面两个文档

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

首先可以找出两篇文档中单词的并集,作为dictionary

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

那么两篇文档统计出来的BoW 向量就是
[1,2,1,1,1,0,0,0,1,1]
[1,1,1,1,0,1,1,1,0,0]

 

 

实例

对影评数据做预处理,大概有以下环节:

  1. 去掉html标签
  2. 移除标点
  3. 切分成词/token
  4. 去掉停用词
  5. 重组为新的句子
  6. example = BeautifulSoup(raw_example, 'html.parser').get_text()
    display(example, '去掉HTML标签的数据')
  7. example_letters = re.sub('[^a-zA-Z]', ' ', example)
    display(example_letters, '去掉标点的数据')
  8. words = example_letters.lower().split()
    display(words, '纯词列表数据')
  9. from nltk.corpus import stopwords
    # stopwords = {}.fromkeys([ line.rstrip() for line in open('../stopwords.txt')])
    stopwords = stopwords.words("english")
    words_nostop = [w for w in words if w not in stopwords]
    display(words_nostop, '去掉停用词数据')
  10. vectorizer = CountVectorizer(max_features = 5000) 
    train_data_features = vectorizer.fit_transform(df.clean_review).toarray()
    train_data_features.shape
    vectorizer.get_feature_names()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值