Python based on Baidu intelligent cloud platform stock information sentiment analysis
All codes and data addresses are as follows: Python based on Baidu Smart Cloud Platform Stock Information Sentiment Analysis
本文章详细内容如下:
Article Directory
import the corresponding package
1. Import library
import pandas as pd
import datetime
from aip import AipNlp
import codecs
2. Set account secret key
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
3. Import data
df = pd.read_csv('600519.csv')
del df["Unnamed: 0"]
df["date"] = pd.to_datetime(df["create time"].apply(lambda x: "2023-"+ x[:5]))
data= df[["date","title"]].sort_values(by="date").reset_index(drop=True)
data
4. Data Merging
newvdata = data.groupby('date').agg(lambda x:list(x)) #按日期分组,把同一天的评论并到一起
newvdata
5. Baidu Natural Language Processing Sentiment Analysis
sitems=client.sentimentClassify(text)['items'][0]#情感分析
for i in range(newvdata.shape[0]):
print('正在处理第{}条,还剩{}条'.format(i,newvdata.shape[0]-1))
dates=newvdata.index[i]
for view in newvdata.title[i]:
print(view)
get_sentiments(view,dates)
6. Analysis results
sent_df = pd.read_csv("./sentiment.csv",header=None)
sent_df.columns = ["date","positive_prob", "confidence", "sentiment"]
sent_df.date = pd.to_datetime(sent_df.date)
sent_df = sent_df.sort_values(by="date")
group_df = sent_df.groupby('date')["positive_prob"].mean()
group_df = pd.DataFrame(group_df)
group_df
group_df.plot(figsize=(20, 5))
7. Get stock data
import tushare as ts
# 茅台股票价格数据获取
stock_df = ts.get_k_data('600519',start='2023-01-03',end='2023-07-12')
stock_df["pct_change"] = stock_df["close"].pct_change()*10
stock_df = stock_df.dropna()
stock_df = stock_df.set_index('date') # 设置日期为索引
stock_df.index = pd.to_datetime(stock_df.index)
stock_df
all_data = stock_df.join(group_df, how="left")
all_data = all_data.fillna(method="ffill")
all_data
all_data[["pct_change", "positive_prob"]].plot(figsize=(20, 5))