###困っていること
トピックモデルのライブラリgensimを使っているのですが、
gensimの関数である
gensim.corpora.dictionary.filter_extremes()関数についての質問です。
この関数の引数,no_belowとno_aboveはそれぞれ
no_below:出現回数no_below回以下の単語を無視
no_above:全体のno_above以上に出現した単語を無視(no_above = 0.3 ➝ 30%)
という認識で使っているのですが、パラメータを調整する際に
no_above = 1としたところ、私が実装したプログラムでは単語が一定数含まれていました。
逆にno_above = 0とすると単語がなくなったというエラー(no terms error)が出ます。
この関数の使い方を調べてみても値について詳しく書かれているものがなく、
どのような値に設定すればよいのか悩んでいます。
no_belowとno_aboveがそれぞれ何を表しているのか教えていただきたいです。
実装中のプログラムに問題があるとも考えられるので、
以下に記します。
python
1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4""" 5トピックモデル(入力:novel_setの全ての.txtファイル) 6各ファイルの管理可能 7形態素解析対象:名詞(代名詞,非自立名詞,数,固有名詞を除く) 8NGワード:あり 9""" 10 11import time 12import glob 13import MeCab 14from gensim import corpora, models, similarities 15 16def morphological_analysis(file_list, NG_WORDS): 17 mecab = MeCab.Tagger ("-Ochasen") 18 19 texts = [] 20 21 for file in file_list: 22 f = open(file, "r",encoding = "utf-8") 23 text = f.read() 24 f.close() 25 #形態素解析(文字列を改行位置で区切って分割) 26 chunks = mecab.parse(text).splitlines() 27 28 #絞り込み 29 sels = [] 30 for chunk in chunks: 31 cols = chunk.split('\t') 32 if len(cols) >= 4: 33 parts = cols[3].split('-') 34 #指定した品詞で始まっている場合 → true 35 if parts[0].startswith('名詞'): 36 #代名詞,非自立名詞,固有名詞,数を含めない 37 if parts[1] in ['代名詞','非自立','固有名詞','数']: 38 continue 39 #NGワードを含めない 40 if cols[2] in NG_WORDS: 41 continue 42 #形態素を追加 43 sels.append(cols[2]) 44 texts.append(sels) 45 return texts 46 47t0 = time.time() 48 49#file_list[file_no]:ファイル番号file_noのファイル名 50file_list = glob.glob('novel_set/*.txt') 51 52NG_WORDS = ['一','二','三','四','五','六','七','八','九','ー','-'] 53 54#形態素解析 55texts = morphological_analysis(file_list, NG_WORDS) 56dictionary = corpora.Dictionary(texts) 57print('辞書単語数(加工前):%d'%len(dictionary)) 58 59#辞書加工 60""" 61no_below:出現回数no_below回以下の単語を無視 62no_above:全体のno_above以上に出現した単語を無視(no_above = 0.3 ➝ 30%) 63""" 64dictionary.filter_extremes(no_below = 3,no_above = 1) 65 66#コーパス作成 67corpus = [dictionary.doc2bow(text) for text in texts] 68 69t1 = time.time() 70 71""" 72num_topics:トピック数 73""" 74#LDAモデルに投入 75lda = models.LdaModel(corpus = corpus, id2word = dictionary, num_topics = 6) 76 77t2 = time.time() 78 79print(lda) 80 81#各トピックにおける各トークンの出現確率を係数で表示 82for topic in lda.show_topics(-1): 83 print(topic) 84 85print('ファイル数:%d'%len(file_list)) 86 87corpus_time = t1 - t0 88print('コーパス生成時間:%f秒'%corpus_time) 89 90lda_time = t2 -t1 91print('LDAモデル生成時間:%f秒'%lda_time) 92 93total_time = t2 - t0 94print('合計時間:%f秒'%total_time)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。