【发布时间】:2021-08-09 10:14:45
【问题描述】:
您好,我目前正在进行语义推文分析,并希望使用 Numpy Vectorization 提高我的代码运行时间。
我曾尝试增强我的代码一段时间,但没有成功。 我可以将循环迭代中的公式输入到函数中并通过 Numpy.vectorize 应用它吗?
ss = SentimentIntensityAnalyzer()
for index, row in tw_list["full_text"].iteritems():
score = ss.polarity_scores(row)
neg = score["neg"]
neu = score["neu"]
pos = score["pos"]
comp = score["compound"]
if neg > pos:
tw_list.loc[index, "sentiment"] = "negative"
elif pos > neg:
tw_list.loc[index, "sentiment"] = "positive"
else:
tw_list.loc[index, "sentiment"] = "neutral"
tw_list.loc[index, "neg"] = neg
tw_list.loc[index, "neu"] = neu
tw_list.loc[index, "pos"] = pos
tw_list.loc[index, "compound"] = comp
【问题讨论】:
-
Afaik,矢量化不会提高代码速度,因为它是一种伪装的 for 循环。
标签: pandas algorithm dataframe numpy semantics