我遇到了这个属性错误,我卡在如何处理推文中出现的浮点值。流式推文必须被转换为小写并进行分词,因此我使用了 split 函数。
有人能帮我应对这个问题吗,提供任何解决方法或解决方案吗..?
以下是我遇到的错误...
AttributeError Traceback (most recent call last)
<ipython-input-28-fa278f6c3171> in <module>()
1 stop_words = []
----> 2 negfeats = [(word_feats(x for x in p_test.SentimentText[f].lower().split() if x not in stop_words), 'neg') for f in l]
3 posfeats = [(word_feats(x for x in p_test.SentimentText[f].lower().split() if x not in stop_words), 'pos') for f in p]
4
5 trainfeats = negfeats+ posfeats
AttributeError: 'float' object has no attribute 'lower'
这是我的代码
p_test = pd.read_csv('TrainSA.csv')
stop_words = [ ]
def word_feats(words):
return dict([(word, True) for word in words])
l = [ ]
for f in range(len(p_test)):
if p_test.Sentiment[f] == 0:
l.append(f)
p = [ ]
for f in range(len(p_test)):
if p_test.Sentiment[f] == 1:
p.append(f)
negfeats = [(word_feats(x for x in p_test.SentimentText[f].lower().split() if x not in stop_words), 'neg') for f in l]
posfeats = [(word_feats(x for x in p_test.SentimentText[f].lower().split() if x not in stop_words), 'pos') for f in p]
trainfeats = negfeats+ posfeats
print len(trainfeats)
import random
random.shuffle(trainfeats)
print(len(trainfeats))
p_train = pd.read_csv('TrainSA.csv')
l_t = []
for f in range(len(p_train)):
if p_train.Sentiment[f] == 0:
l_t.append(f)
p_t = []
for f in range(len(p_train)):
if p_train.Sentiment[f] == 1:
p_t.append(f)
print len(l_t)
print len(p_t)
我尝试了很多方法,但仍然无法让他们使用lower和split函数。
p_test.SentimentText[f]
是一个浮点数而不是一个字符串。你不能在一个浮点数上调用lower()
方法。 - Kevin