質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

6638閲覧

自然言語処理:エラー箇所と改善方法を教えてください

pekeuto

総合スコア19

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/02/18 07:43

編集2019/02/18 07:57

発生している問題・エラーメッセージ

Traceback (most recent call last): File "test2.py", line 59, in <module> train_vecs = vectorizer.fit_transform(train_data) File "C:\Users\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 1381, in fit_transform X = super(TfidfVectorizer, self).fit_transform(raw_documents) File "C:\Users\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 869, in fit_transform self.fixed_vocabulary_) File "C:\Users\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 811, in _count_vocab raise ValueError("empty vocabulary; perhaps the documents only" ValueError: empty vocabulary; perhaps the documents only contain stop words

該当のソースコード

python

1import glob 2import time 3from sklearn.model_selection import train_test_split 4from janome.tokenizer import Tokenizer 5from sklearn.feature_extraction.text import TfidfVectorizer 6from sklearn.naive_bayes import MultinomialNB 7from sklearn.ensemble import RandomForestClassifier 8import random 9 10 for c_name, c_id in category.items(): 11 files = glob.glob("./text/{c_name}/{c_name}*.txt".format(c_name=c_name)) 12 13 text = '' 14 for file in files: 15 with open(file, 'r') as f: 16 lines = f.read().splitlines() 17 url = lines[0] 18 datetime = lines[1] 19 subject = lines[2] 20 body = "".join(lines[3:]) 21 text = subject + body 22 23 docs.append(text) 24 labels.append(c_id) 25 26 return docs, labels 27 28docs, labels = load_livedoor_news_corpus() 29 30 31 32random.seed() 33indices = list(range(len(docs))) 34random.shuffle(indices) 35 36train_data = [docs[i] for i in indices[0:7000]] 37train_labels = [labels[i] for i in indices[0:7000]] 38test_data = [docs[i] for i in indices[7000:]] 39test_labels = [labels[i] for i in indices[7000:]] 40 41vectorizer = TfidfVectorizer() 42train_vecs = vectorizer.fit_transform(train_data) 43test_vecs = vectorizer.transforn(test_data) 44 45clf = MultinomiaINB 46model = clf.fit(train_vecs,train_labels) 47print(clf.score(train_vecs,train_labels)) 48print(clf.score(test_vecs,test_labels)) 49 50clf = RandomForestClassifier 51model = clf.fit(train_vecs,train_labels) 52print(clf.score(train_vecs,train_labels)) 53print(clf.score(test_vecs,test_labels)) 54 55def tokenize(text): 56 tokens = t.tokenize(','.join(text)) 57 noun = [] 58 for token in tokens: 59 partOfSpeech = token.part_of_speech.split(',')[0] 60 61 if partOfSpeech == "名詞": 62 noun.append(token.surface) 63 if partOfSpeech == "動詞": 64 noun.append(token.surface) 65 if partOfSpeech == "形容詞": 66 noun.append(token.surface) 67 if partOfSpeech == "形容動詞": 68 noun.append(token.surface) 69 70 return noun 71

empty vocabulary; perhaps the documents only contain stop words
空の語彙。 おそらく文書にはストップワードしか含まれていない

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

エラー部分を解決することができました

投稿2019/02/18 07:58

pekeuto

総合スコア19

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問