###前提・実現したいこと
word_cont[0],[1]にint(w.split(":")[1])をインクリメントし、
score1,score2を計算したい。
###発生している問題・エラーメッセージ
word_count[class_label] += int(w.split(":")[1])
のすぐ下でword_countを出力すると正常にインクリメントされているが、
score0を計算する前にword_countを出力すると、ずっと[0,0]のままになってしまう。
socreをそれぞれ計算する際は、インクリメントされた値で計算したい。
###該当のソースコード
Python
1 with open(train_data_path,"r") as f: 2 # condprob[(単語id,leg2P(w|neg),log2P(w|pos)),()...] 3 id = 0 4 global word_count 5 for word in V: 6 # print(word,end=" ") 7 # print(id) 8 # 行番号 i 9 for i,line in enumerate(f): 10 11 class_label = -2 12 line_w = line.split(" ") 13 #print(word) 14 if len(line_w) != 2: 15 line_w[-1] = line_w[-1].replace("\n","") 16 if line_w[0] == "-1": 17 class_label = 0 18 else: 19 class_label = 1 20 # 単語番号 j 21 for j,w in enumerate(line_w[1:]): 22 # print(w) OK 23 #print(word,w.split(":")[0]) 24 #print(w.split(":")[0]) 25 if w.split(":")[0] == word: 26 #print("◯") 27 #print(word) 28 # print(w) OK 29 word_count[class_label] += int(w.split(":")[1]) 30 #score += int(w.split(":")[1]) 31 # print(w,i) 32 # word_count[class_label] += 1 OK 33 # #print(word_count) OK 34 # #print(w.split(":")[1]) OK 35 # print(word_count[0] , "before") OK 36 score0 = score1 = 0 37 #print(word_count,id) 38 score0 = math.log2((word_count[0] + σ) / (neg_all_word + len(V)*σ)) 39 score1 = math.log2((word_count[1] + σ) / (pos_all_word + len(V)*σ)) 40 #print(word_count) 41 #word_count = [0,0] 42 43 condprob.append((id,score0,score1)) 44 id += 1 45
###補足情報(言語/FW/ツール等のバージョンなど)
コードの一番上で開いているファイルを添付しています↓
https://pastebin.com/EEtSr5Le
Python 3.6.3
試したこと
コメントになっているようにトレースしていきました。
V にはtrain_data_pathに出現している単語の辞書(set()しているため重複なし)です。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。