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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

314閲覧

pythonでTFIDFの処理を並列処理するには

kohekoh

総合スコア140

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/07/06 09:30

編集2017/07/06 11:42

pythonについてです

タイトル通り、TFIDFの処理を並列処理したいと考えています
普通にTFIDFをするぶんにはできるのですが、TFIDFしたいと考えているファイルが大きくて
並列化しないととてもじゃないけど終わらないということでやっています

コードを書いているところで、エラーが出たので投稿しました

現在以下のコードを書いていて
エラーがでます

python

1import nltk 2import numpy as np 3import json 4#import nltk_exa as nl 5import time 6import multiprocessing as mp 7 8def subcalc(p): #p = 0~8 9 word = [] 10 proc = 8 11 f = open("word0_a.txt") 12 line = f.readline() 13 while line: 14 line = f.readline() 15 word.append(line.replace("[","").replace("]","").replace(",","").replace("\"","").split()) 16 f.close() 17 word.pop() 18 print(word) 19 20 ini = len(word) * p / proc 21 fin = len(word) * (p+1) / proc 22 23 subdoc = [] 24 lists = [] 25 proc = 8 26 collection = nltk.TextCollection(word) 27 t1 = time.time() 28 29 for do in range(ini, fin): 30 wo=[] 31 for term in set(do): 32 if(collection.tf_idf(term, do) > 0): 33 wo.append([term,collection.tf_idf(term, do)]) #ここも上のサイトにのってる 34 #print(wo) 35 wo.sort(key=lambda x:x[1]) 36 wo.reverse() 37 slice1 = np.array(wo[:20]) 38 lists = slice1[:,0] 39 subdoc.append(list(lists)) 40 #print(doc[0]) 41 t2 = time.time() 42 print('processing time1(一回のfor文): ' + str(t2 - t1) + '(sec)') 43 del wo 44 45 return subdoc 46 47def tfidf(): 48 49 proc = 8 50 pool = mp.Pool(proc) 51 doc = pool.map(subcalc, range(8)) 52 t3 = time.time() 53 print(doc) 54 print('processing time2(終わり): ' + str(t3 - t1) + '(sec)') 55 56if __name__ == "__main__": 57 tfidf() 58 59------------------------------------------------------------- 60実行結果 61 62doc = pool.map(subcalc, range(8)) 63TypeError: 'float' object cannot be interpreted as an integer 64

word0_a.txt(一部抜粋)

["portions", "Sandwich", "top", "Rick", "come", "parents", "ONLY", "Brother", "advice", "My", "lunch", "text", "Sundays", "fish", "people", "friendly", "bit", "Fish", "giant", "Mom", "drinks", "Dad", "Sebak", "Pittsburgh", "CASH", "Open-Faced", "late", "afternoon", "staff", "ATM", "accurate", "several", "dishes", "recommendations", "Reuben", "early", "massive", "Mondays", "times", "affordable", "sandwich", "place", "Saturday", "prepared", "DELICIOUS", "Things", "today", "large", "time", "Steak", "sure", "recommendation", "good", "meal", "pretty", "Oh"] ["busy", "table", "portions", "taste", "ages", "service", "happy", "Rib", "Sandwiich", "years", "more", "while", "menu", "text", "people", "friendly", "fresh", "-Flounder", "little", "huge", "value", "lot", "Special", "delicious", "everything", "style", "nothing", "Pittsburgh", "girls", "movie", "reviews", "St", "few", "ways", "-Fish", "ambiance", "glad", "times", "slow", "Worth", "food", "Norfolk", "place", "miss", "prepared", "prices", "complaints", "-Salmon", "kitchen", "AWESOME", "hit", "Everything", "low", "large", "gem", "sure", "different", "-Prime", "good", "Most", "busier"]

どのように改善すればよろしいですか
よろしくお願いします

ちなみに並列化しないバージョンはこれです

python

1import nltk 2import numpy as np 3import json 4import nltk_exa as nl 5import time 6 7#def tfidf(word): 8def tfidf(): 9 10 word = [] 11 f = open("word0_a.txt") 12 line = f.readline() 13 while line: 14 line = f.readline() 15 word.append(line.replace("[","").replace("]","").replace(",","").replace("\"","").split()) 16 f.close() 17 word.pop() 18 #tfidfの計算 19 doc = [] 20 lists = [] 21 collection = nltk.TextCollection(word) #サイトにのっていた 22 t1 = time.time() 23 for do in word: 24 wo=[] 25 for term in set(do): 26 if(collection.tf_idf(term, do) > 0): 27 wo.append([term,collection.tf_idf(term, do)]) #ここも上のサイトにのってる 28 #print(wo) 29 wo.sort(key=lambda x:x[1]) 30 wo.reverse() 31 #print(wo) 32 slice1 = np.array(wo[:20]) #先頭の文字から終了インデックスまでが抽出 33 lists = slice1[:,0] #[:]は戦闘から終了のインデックスまで抽出と、slice1の0番目を格納 34 doc.append(list(lists)) #listsが文字列だから、リストに格納 35 #print(doc[0]) 36 t2 = time.time() 37 #print('processing time1(一回のfor文): ' + str(t2 - t1) + '(sec)') 38 del wo 39 40 print(doc) 41 t3 = time.time() 42 print('processing time2(終わり): ' + str(t3 - t1) + '(sec)') 43 44if __name__ == "__main__": 45 tfidf() 46

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

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

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

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

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

guest

回答2

0

自己解決

http://kesin.hatenablog.com/entry/20110825/1314270919

このサイトを参考にしてできました
ご迷惑をおかけしました

投稿2017/07/06 12:15

kohekoh

総合スコア140

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

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

0

「普通にTFIDFをするぶんにはできる」とありますが、並列処理以前にsubcalc(7) を実行したとき単体でも動かないですよね?

range(ini, fin) で浮動小数点を range() に入れていたり、set(do) で整数を set() に入れていたりとオブジェクトの型がめちゃくちゃです。

一気に書いて動いた!/動かない!をやっても今回のようにデバッグが大変だと思いますので、1ステップずつ動作を確かめながら開発することをおすすめします。

投稿2017/07/06 10:59

miyahan

総合スコア3095

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

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

kohekoh

2017/07/06 11:32

subcalc自体は並列化をするときに新たに加えたものです 特にrangeやsetのところは参考にしたサイトによせて書いているので そこらへんの書き方がまずかったみたいです ありがとうございます
kohekoh

2017/07/06 11:43

動くコードをのせました どう変えればいいでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問