我有这个当前的(按设计工作)嵌套的for循环,该循环可获取一些感兴趣的数据并从中创建一个pandas数据框。为了减少运行时,我一直在尝试找出向量化是否可以很好地替代嵌套的for循环。有什么方法可以向量化这些循环吗?
for i, row in enumerate(ldamodel[corpus]):
row = sorted(row, key=lambda x: (x[1]), reverse=True)
# Get the Dominant topic, % Contribution and Keywords for each document
for j, (topic_num, prop_topic) in enumerate(row):
if j == 0: # Pull dominant topic
wp = ldamodel.show_topic(topic_num)
topic_keywords = ", ".join([word for word, prop in wp])
sent_topics_df = sent_topics_df.append(pd.Series([int(topic_num), ldamodel.get_document_topics(corpus[i]), topic_keywords]), ignore_index=True)
else:
break