zoukankan      html  css  js  c++  java
  • 机器学习入门-文本数据-构造Ngram词袋模型 1.CountVectorizer(ngram_range) 构建Ngram词袋模型

    函数说明:

      1 CountVectorizer(ngram_range=(2, 2)) 进行字符串的前后组合,构造出新的词袋标签

      参数说明:ngram_range=(2, 2) 表示选用2个词进行前后的组合,构成新的标签值

    Ngram模型表示的是,对于词频而言,只考虑一个词,这里我们在CountVectorizer统计词频时,传入ngram_range=(2, 2)来构造新的词向量的组合

    好比一句话'I like you'

    如果ngram_range = (2, 2)表示只选取前后的两个词构造词组合 :词向量组合为:’I like‘ 和 ’like you‘ 

    如果ngram_range = (1, 3) 表示选取1到3个词做为组合方式: 词向量组合为: 'I', 'like', 'you', 'I like', 'like you', 'I like you' 构成词频标签

    代码:

      第一步:构造Dataframe格式,并数组化数据

      第二步:构造函数进行分词和去除停用词,并使用空格进行串接,为了分词做准备

      第三步:np.vectorize 向量化函数,并调用函数进行分词和去除停用词

      第四步:使用CountVectorizer(ngram_range(2, 2)) 进行文本的词向量拼接

    import pandas as pd
    import numpy as np
    import re
    import nltk #pip install nltk
    
    
    corpus = ['The sky is blue and beautiful.',
              'Love this blue and beautiful sky!',
              'The quick brown fox jumps over the lazy dog.',
              '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', 'animals', 'weather', 'animals']
    
    # 第一步:构建DataFrame格式数据
    corpus = np.array(corpus)
    corpus_df = pd.DataFrame({'Document': corpus, 'categoray': labels})
    
    # 第二步:构建函数进行分词和停用词的去除
    # 载入英文的停用词表
    stopwords = nltk.corpus.stopwords.words('english')
    # 建立词分割模型
    cut_model = nltk.WordPunctTokenizer()
    # 定义分词和停用词去除的函数
    def Normalize_corpus(doc):
        # 去除字符串中结尾的标点符号
        doc = re.sub(r'[^a-zA-Z0-9s]', '', string=doc)
        # 是字符串变小写格式
        doc = doc.lower()
        # 去除字符串两边的空格
        doc = doc.strip()
        # 进行分词操作
        tokens = cut_model.tokenize(doc)
        # 使用停止用词表去除停用词
        doc = [token for token in tokens if token not in stopwords]
        # 将去除停用词后的字符串使用' '连接,为了接下来的词袋模型做准备
        doc = ' '.join(doc)
    
        return doc
    
    # 第三步:向量化函数和调用函数
    # 向量化函数,当输入一个列表时,列表里的数将被一个一个输入,最后返回也是一个个列表的输出
    Normalize_corpus = np.vectorize(Normalize_corpus)
    # 调用函数进行分词和去除停用词
    corpus_norm = Normalize_corpus(corpus)
    print(corpus_norm)

    # 第四步:使用ngram_range构造组合的词袋标签
    from sklearn.feature_extraction.text import  CountVectorizer
    CV = CountVectorizer(ngram_range=(2, 2))
    CV.fit(corpus_norm)
    vocs = CV.get_feature_names()
    print(vocs)
    corpus_array = CV.transform(corpus_norm).toarray()
    corpus_norm_df = pd.DataFrame(corpus_array, columns=vocs)
    print(corpus_norm_df.head())

                                                                         部分的组合结果

  • 相关阅读:
    PHP排序问题
    正则表达式——获取指定IP的物理地址
    两款网站页面翻译插件
    正则表达式应用之提炼歌词
    PHP 读取网页文件
    PHP正则表达式——匹配多行
    正则表达式——获取指定IP的物理地址(二)
    用百度音乐Widget从此不在为找MP3外链而烦恼
    I Saw Thee Weep
    POJ 3281 Dining
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/10324709.html
Copyright © 2011-2022 走看看