首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代行的矢量化替代:语义分析

迭代行的矢量化替代:语义分析
EN

Stack Overflow用户
提问于 2021-05-20 15:14:27
回答 1查看 49关注 0票数 1

嗨,我目前正在做一个语义推文分析,想用Numpy Vectorization改善我的代码运行时间。

我尝试了一段时间来增强我的代码,但没有成功。我可以在函数的循环迭代中输入公式,然后通过Numpy.vectorize应用它吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-05-20 22:35:24

您可以使用apply函数,而不是遍历dataframe中的行。

代码语言:javascript
复制
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')

这应该会提高性能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67615623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档