我正在为股市分析创建情绪分析代码。这是代码的核心:
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()
产生以下输出:
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 模型,因此我需要将分数设为数字。我知道 usingprobability = sentence.labels[0].score
只给我们分数,但这意味着没有办法区分陈述是积极的还是消极的。有没有办法在归类为负数的分数后面添加一个“-”(否定)符号。例如 - NEGATIVE (0.9855) = -9855
。这将确保信息是数字的并且是有用的。