首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在负面flair情绪分析中添加'-‘符号

在负面flair情绪分析中添加'-‘符号
EN

Stack Overflow用户
提问于 2021-03-27 06:21:50
回答 1查看 95关注 0票数 0

我正在为股票市场分析创建一个情绪分析代码。这是代码的核心:

代码语言:javascript
复制
import flair
flair_sentiment = flair.models.TextClassifier.load('en-sentiment')
columns = ['ticker', 'date', 'time', 'headline']
parsed_and_scored_news = pd.DataFrame(parsed_news, columns=columns)
sentiment = []
for head in parsed_and_scored_news['headline']:
    s = flair.data.Sentence(head)
    flair_sentiment.predict(s)
    total_sentiment = s.labels
    sentiment.append(total_sentiment)
    scores_df = pd.DataFrame(sentiment)
    parsed_and_scored_news = parsed_and_scored_news.join(scores_df, rsuffix='_right')
    
# Convert the date column from string to datetime
parsed_and_scored_news['date'] = pd.to_datetime(parsed_and_scored_news.date).dt.dateparsed_and_scored_news.head()

将生成以下输出:

代码语言:javascript
复制
    ticker     date      time              headline                                    0
0   AMZN    2021-03-26  02:37PM Tech stocks are going to do vey well going for...   POSITIVE (0.9986)
1   AMZN    2021-03-26  01:17PM Amazon mocked idea its drivers urinated in bot...   NEGATIVE (0.9855)
2   AMZN    2021-03-26  01:11PM ThredUp CEO on IPO day: Dont tax resale and Am...   NEGATIVE (0.6743)
3   AMZN    2021-03-26  12:54PM Why this retailer is seeing a triple-digit sal...   POSITIVE (0.9597)
4   AMZN    2021-03-26  12:07PM How to secure your smart home camera                POSITIVE (0.9981)

因为我想将数据输入到ML模型中,所以我需要分数是数字。我知道使用probability = sentence.labels[0].score只能给出分数,但这意味着没有办法将一个语句是否是肯定的归类为否定的。有没有办法在被归类为负分的分数后面加一个‘-’(否定)符号?例如- NEGATIVE (0.9855) = -9855。这将确保信息既是数字的也是有用的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-27 10:01:53

这段代码对我来说很有效:

代码语言:javascript
复制
sentiment = []
sentiment_score =[]
for head in parsed_and_scored_news['headline']:
    s = flair.data.Sentence(head)
    flair_sentiment.predict(s)
    total_sentiment = s.labels[0].value
    total_sentiment_score = s.labels[0].score
    sentiment.append(total_sentiment)
    sentiment_score.append(total_sentiment_score)
scores_df = pd.DataFrame(sentiment)
scores_df_1 = pd.DataFrame(sentiment_score)
parsed_and_scored_news = parsed_and_scored_news.join(scores_df, rsuffix='_right')
parsed_and_scored_news = parsed_and_scored_news.join(scores_df_1, rsuffix='_right')

st = parsed_and_scored_news['0_right'].tolist()
count = -1
for item in parsed_and_scored_news['0']:
    count = count+1
    if item == 'NEGATIVE':
        lst[count] = 0-lst[count]
    
scores_final = pd.DataFrame(lst)
parsed_and_scored_news = parsed_and_scored_news.join(scores_final, rsuffix='_final')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66824999

复制
相关文章

相似问题

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