引领先锋|基于深度学习的文本数据特征提取方法之Word2Vec(一)( 二 )
特征工程策略
让我们来看看处理文本数据并从中提取有意义的特征的一些高级策略 , 这些策略可用于下游的机器学习系统 。 我们将从加载一些基本的依赖项和设置开始 。
import pandas as pd import numpy as np import re import nltk import matplotlib.pyplot as plt pd.options.display.max_colwidth = 200 %matplotlib inline现在 , 我们将获取一些文档语料库 , 在这些文档语料库上执行所有的分析 。 对于其中一个语料库 , 我们将重用上一篇文章中的语料库 。 为了便于理解 , 我们将代码重新写一下 。
corpus = ['The sky is blue and beautiful.', 'Love this blue and beautiful sky!', 'The quick brown fox jumps over the lazy dog.', "A king's breakfast has sausages, ham, bacon, eggs, toast and beans", 'I love green eggs, ham, sausages and bacon!', 'The brown fox is quick and the blue dog is lazy!', 'The sky is very blue and the sky is very beautiful today', 'The dog is lazy but the brown fox is quick!'] labels = ['weather', 'weather', 'animals', 'food', 'food', 'animals', 'weather', 'animals']corpus = np.array(corpus) corpus_df = pd.DataFrame({'Document': corpus, 'Category': labels}) corpus_df = corpus_df[['Document', 'Category']]我们的玩具语料库由几个类别的文档组成 。 我们将在本文中使用的另一个语料库是The King James Version of the Bible , 可以从Project Gutenberg通过nltk中的corpus模块免费获得 。 我们将在下一节中加载它 。 在讨论特征工程之前 , 我们需要对本文进行预处理和规范化 。
文本预处理
可以有多种方法来清理和预处理文本数据 。 在上一篇文章中已经讲过了 。 由于本文的重点是特征工程 , 就像前面的文章一样 , 我们将重用简单的文本预处理程序 , 它的重点是删除特殊字符、额外的空格、数字、停止词和把语料库的大写转换为小写 。
wpt = nltk.WordPunctTokenizer() stop_words = nltk.corpus.stopwords.words('english')def normalize_document(doc): # lower case and remove special characters\whitespaces doc = re.sub(r'[^a-zA-Z\s]', '', doc, re.I|re.A) doc = doc.lower() doc = doc.strip() # tokenize document tokens = wpt.tokenize(doc) # filter stopwords out of document filtered_tokens = [token for token in tokens if token not in stop_words] # re-create document from filtered tokens doc = ' '.join(filtered_tokens) return docnormalize_corpus = np.vectorize(normalize_document)我们准备好了基本的预处理pipeline , 让我们首先将其应用于我们的玩具语料库 。
【引领先锋|基于深度学习的文本数据特征提取方法之Word2Vec(一)】 norm_corpus = normalize_corpus(corpus) norm_corpus Output ------ array(['sky blue beautiful', 'love blue beautiful sky', 'quick brown fox jumps lazy dog', 'kings breakfast sausages ham bacon eggs toast beans', 'love green eggs ham sausages bacon', 'brown fox quick blue dog lazy', 'sky blue sky beautiful today', 'dog lazy brown fox quick'], dtype='
from nltk.corpus import gutenberg from string import punctuationbible = gutenberg.sents('bible-kjv.txt') remove_terms = punctuation + '0123456789'norm_bible = [[word.lower() for word in sent if word not in remove_terms] for sent in bible] norm_bible = [' '.join(tok_sent) for tok_sent in norm_bible] norm_bible = filter(None, normalize_corpus(norm_bible)) norm_bible = [tok_sent for tok_sent in norm_bible if len(tok_sent.split()) > 2]print('Total lines:', len(bible)) print('\nSample line:', bible[10]) print('\nProcessed line:', norm_bible[10])
推荐阅读
- 引领时尚新时代|她是饶雪漫书中的模特,和鹿晗在一起被赞般配,笑起来碾压林允!
- 引领时尚新时代|传闻她被渣男骗钱骗大肚子,无奈生下孩子给母亲抚养,气质美上天
- 引领时尚新时代|她是典型中国好媳妇,却嫁给相识不到20天的男友,穿搭霸气侧漏
- 引领时尚新时代|她从小就被亲爸说是捡来的,出道10年没谈过恋爱,美得让人着迷
- 「宁静」宁静不管穿了个啥都能引领时髦,嫌T恤配牛仔裤不够炫,再加薄纱
- 引领先锋|扔员工证的工厂到底啥来头?营收超三千亿,与富士康、伟创力齐名
- 指数债券|美债收益率曲线的陡平之辨:基于供需视角的分析
- 新京报|摩登日记|引领潮流的时尚偶像麦当娜
- 山东沂水提倡彩礼费1万以内|临沂沂水倡导婚俗简约 举办集体婚礼引领风尚
- 【山东沂水提倡彩礼费1万以内】临沂沂水倡导婚俗简约 举办集体婚礼引领风尚
