美文网首页
文本型特征提取

文本型特征提取

作者: 晚来天欲雨 | 来源:发表于2018-03-23 15:26 被阅读0次

1. hash结构

from sklearn.feature_extraction import DictVectorizer

    measurements = [

    {'city':'dubai', 'temperature':33}, {'city':'London', 'temperature':12}, ,{'city':'San Fransisco', 'temperature':18}

]
vec = DictVectorizer()
vec.fit_transform(measurements).toarray()

# output
array([[ 0.,  0.,  1., 33.],
       [ 1.,  0.,  0., 12.],
       [ 0.,  1.,  0., 18.]])

vec.get_feature_names()
>>> ['city=London', 'city=San Fransisco', 'city=dubai', 'temperature']

2. 词袋模型

from sklearn.feature_extraction.text import CountVectorizer

vectorizer=CountVectorizer(min_df=1) # 至少出现一次
vectorizer

>>> CountVectorizer(analyzer='word', binary=False, decode_error='strict',
        dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',
        lowercase=True, max_df=1.0, max_features=None, min_df=1,
        ngram_range=(1, 1), preprocessor=None, stop_words=None,
        strip_accents=None, token_pattern='(?u)\\b\\w\\w+\\b',
        tokenizer=None, vocabulary=None)

corpus = [
    'This is the first document.',
     'This is the second second document.',
     'And the third one.',
     'Is this the first document?',
]
X=vectorizer.fit_transform(corpus)
X

>>> <4x9 sparse matrix of type '<class 'numpy.int64'>'
    with 19 stored elements in Compressed Sparse Row format>


vectorizer.get_feature_names()
>>> ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']

X.toarray()
>>> array([[0, 1, 1, 1, 0, 0, 1, 0, 1],
                 [0, 1, 0, 1, 0, 2, 1, 0, 1],
                 [1, 0, 0, 0, 1, 0, 1, 1, 0],
                 [0, 1, 1, 1, 0, 0, 1, 0, 1]], dtype=int64)

描述的文档会完全忽略文档中单词的相对位置

analyze("This is a text document to analyze.")
>>> ['this', 'is', 'text', 'document', 'to', 'analyze']

为了保留一些有序的信息,我们可以抽取2-grams的词汇,而非使用1-grams:

bigram_vectorizer=CountVectorizer(ngram_range=(1,2), token_pattern=r'\b\w+\b', min_df=1)
analyze=bigram_vectorizer.build_analyzer()
analyze('Bi-grams are cool!')

>>> ['bi', 'grams', 'are', 'cool', 'bi grams', 'grams are', 'are cool']

通过该vectorizer抽取的词汇表,比之前的方式更大,可以以local positioning patterns进行模糊编码:
X_2 = bigram_vectorizer.fit_transform(corpus).toarray()
X_2
>>> array([[0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0],
                 [0, 0, 1, 0, 0, 1, 1, 0, 0, 2, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0],
                 [1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0],
                 [0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1]],
      dtype=int64)

bigram_vectorizer.get_feature_names()

>>> ['and',
 'and the',
 'document',
 'first',
 'first document',
 'is',
 'is the',
 'is this',
 'one',
 'second',
 'second document',
 'second second',
 'the',
 'the first',
 'the second',
 'the third',
 'third',
 'third one',
 'this',
 'this is',
 'this the']

3. TF-IDF item weight

tf表示词频(term-frequency),idf表示inverse document-frequency ,tf–idf 表示tf * idf。它原先适用于信息检索(搜索引擎的ranking),同时也发现在文档分类和聚类上也很好用。

from sklearn.feature_extraction.text import TfidfTransformer

transformer = TfidfTransformer()
 transformer   
TfidfTransformer(norm=...'l2', smooth_idf=True, sublinear_tf=False,
                 use_idf=True)

counts = [[3, 0, 1],
...           [2, 0, 0],
...           [3, 0, 0],
...           [4, 0, 0],
...           [3, 2, 0],
...           [3, 0, 2]]
...
tfidf = transformer.fit_transform(counts)
 tfidf               
          
>>> <6x3 sparse matrix of type '<... 'numpy.float64'>'
    with 9 stored elements in Compressed Sparse ... format>

 tfidf.toarray()                        
>>> array([[ 0.85...,  0.  ...,  0.52...],
                 [ 1.  ...,  0.  ...,  0.  ...],
                 [ 1.  ...,  0.  ...,  0.  ...],
                 [ 1.  ...,  0.  ...,  0.  ...],
                 [ 0.55...,  0.83...,  0.  ...],
                 [ 0.63...,  0.  ...,  0.77...]])

4. BOW模型的限制

unigrams集(BOW)不能捕获句字和多个词的表示,会丢失掉词的顺序依存。另外,BOW模型不能解释可能的误拼(misspellings)或者词派生(word derivations)。

ngram_vectorizer = CountVectorizer(analyzer='char_wb', ngram_range=(2,2), min_df=1)
counts=ngram_vectorizer.fit_transform(['word', 'wprds'])
counts

>>> <2x9 sparse matrix of type '<class 'numpy.int64'>'
    with 11 stored elements in Compressed Sparse Row format>

ngram_vectorizer.get_feature_names()
[' w', 'd ', 'ds', 'or', 'pr', 'rd', 's ', 'wo', 'wp']

使用’char_wb’分析器,它可以在字符边界内创建n-grams的字符(两边使用空格补齐)。而‘char’分析器则可以基于词来创建n-grams。

相关文章

  • 文本特征提取(2)

    继上期文本特征提取一文以及文本的可读性探究后,继续推出文本特征提取二,从词集型、词袋型提取文本特征。 文本特征提取...

  • 文本型特征提取

    1. hash结构 2. 词袋模型 为了保留一些有序的信息,我们可以抽取2-grams的词汇,而非使用1-gram...

  • 文本特征提取

    在对文本数据进行处理时,很大一部分精力都用在数据集的特征提取上,因此记录一下常用的文本特征提取方法。 文本特征提取...

  • 文本特征提取

    文本特征提取 文本特征提取: 将文本数据转化成特征向量的过程 比较常用的文本特征表示法为词袋法 词袋法: 不考虑词...

  • CountVector基础功能的复现

    sklearn.feature_extraction.text 中有4种文本特征提取方法: CountVector...

  • 文本挖掘

    文本挖掘,指从大量文本集合中发现隐含的模式 。网络文本挖掘是对网上那个大量文本进行表示、特征提取、网络总结、分类、...

  • 暑期论文总结

    一、信息检索基础 信息检索基础之文本特征提取 文本挖掘的任务:从海量文档中发现隐含知识和模式 文本挖掘的特殊性:挖...

  • CountVectorize

    CountVectorizeCountVectorizer是属于常见的特征数值计算类,是一个文本特征提取方法。对于...

  • Spark机器学习实战 (十一) - 文本情感分类项目实战

    0 相关源码 将结合前述知识进行综合实战,以达到所学即所用。文本情感分类这个项目会将分类算法、文本特征提取算法等...

  • 文本表征:SoW、BoW、TF-IDF、Hash Trick、d

    一、文本特征 (一)基本文本特征提取 词语数量常,负面情绪评论含有的词语数量比正面情绪评论更多。 字符数量常,负面...

网友评论

      本文标题:文本型特征提取

      本文链接:https://www.haomeiwen.com/subject/vpdxcftx.html