【发布时间】:2021-08-23 23:32:01
【问题描述】:
我正在尝试获取预测的情绪分数并确定文本是正面的还是负面的。但是在预测值时,我得到一个分数数组序列并引发以下错误。
import json
f = open(("/content/trending_tweets.json"), "r+")
data = f.read()
for x in data.split("\n"):
strlist = "[" + x + "]"
datalist = json.loads(strlist)
for y in datalist:
f = open('/content/user_lookup_data.json', 'a', encoding='utf-8')
print(y["user"]["screen_name"])
screen_name = ('@' + y["user"]["screen_name"])
file_name ='/content/user_timeline/' + screen_name + '_tweets.csv'
user_timeline_data = pd.read_csv(file_name, sep='\t', lineterminator='\n',encoding='latin')
user_timeline_data = (user_timeline_data['tweet'])
print(len(user_timeline_data))
df = pd.DataFrame(columns=['Text', 'Sentiment'])
for index, row in user_timeline_data.iteritems():
sequence = tokenizer.texts_to_sequences(row)
test = pad_sequences(sequence, maxlen=max_len)
pred = model.predict(test)
if pred[index] > 0.5:
df.loc[index, ['Text']] = row
df.loc[index, ['Sentiment']] = 'Positive'
print(df.shape)
print(pred)
else:
df.loc[index, ['Text']] = row
df.loc[index, ['Sentiment']] = 'Negative'
print(df.shape)
print(pred)
df.to_csv('sentiment_'+ screen_name +'.csv', index=False)
错误信息
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-68-274fe2f3a8c0> in <module>()
18 test = pad_sequences(sequence, maxlen=max_len)
19 pred = model.predict(test)
---> 20 if pred[index] > 0.5:
21 df.loc[index, ['Text']] = row
22 df.loc[index, ['Sentiment']] = 'Positive'
IndexError: index 54 is out of bounds for axis 0 with size 48
如果有人可以帮助我,那就太好了
【问题讨论】:
标签: python pandas dataframe keras sentiment-analysis