我正在创建一个巨大的张量,它包含了数以百万计的单词三元组及其计数。例如,单词triple就是(word0, link, word1)
。这些单词三元组收集在一个字典中,其中的值是它们各自的计数,例如(word0, link, word1): 15
。想象一下,我有数百万这样的三倍。在我计算了这些事件之后,我尝试做其他的计算,这就是我的python脚本被卡住的地方。这里有一部分代码是永恒的:
big_tuple = covert_to_tuple(big_dict)
pdf = pd.DataFrame.from_records(big_tuple)
pdf.columns = ['word0', 'link', 'word1', 'counts']
total_cnts = pdf.counts.sum()
for _, row in pdf.iterrows():
w0, link, w1 = row['word0'], row['link'], row['word1']
w0w1_link = row.counts
# very slow
w0_link = pdf[(pdf.word0 == w0) & (pdf.link == link)]['counts'].sum()
w1_link = pdf[(pdf.word1 == w1) & (pdf.link == link)]['counts'].sum()
p_w0w1_link = w0w1_link / total_cnts
p_w0_link = w0_link / total_cnts
p_w1_link = w1_link / total_cnts
new_score = log(p_w0w1_link / (p_w0_link * p_w1_link))
big_dict[(w0, link, w1)] = new_score
我分析了我的脚本,似乎下面两行
w0_link = pdf[(pdf.word0 == w0) & (pdf.link == link)]['counts'].sum()
w1_link = pdf[(pdf.word1 == w1) & (pdf.link == link)]['counts'].sum()
分别取49%和49%计算时间的百分比。这些行试图找到(word0, link)
和(word1, link)
的计数。所以,像这样访问pdf需要很多时间?我能做些什么来优化它吗?你知道吗
请检查我的解决方案-我在计算中优化了一些东西(希望没有错误:)
新分数的表达式为
相关问题 更多 >
编程相关推荐