嗨,我目前正在做一个语义推文分析,想用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发布于 2021-05-20 22:35:24
您可以使用apply函数,而不是遍历dataframe中的行。
def get_sentiments(text):
score = ss.polarity_scores(text)
neg = score["neg"]
neu = score["neu"]
pos = score["pos"]
comp = score["compound"]
if neg > pos:
sentiment = "negative"
elif pos > neg:
sentiment = "positive"
else:
sentiment = "neutral"
return sentiment,neg,neu,pos,comp
tw_list[["sentiment","neg","neu","pos","comp"]] = tw_list["full_text"].apply(get_sentiments,result_type='broadcast')这应该会提高性能。
https://stackoverflow.com/questions/67615623
复制相似问题