[英]unsupported operand type(s) for /: 'Counter' and 'int' - Bag of Words
I'm attempting to plot my bag of word for selecting a suitable number of words to use to train my regression algorithm. 我试图绘制我的单词袋,以选择适当数量的单词来训练我的回归算法。 When I try to plot it though, I get an error.
当我尝试绘制它时,出现错误。
This is my code for making the bag of words: 这是我用来制作单词袋的代码:
df['BOW'] = df.Review2.str.split().apply(Counter)
df['BOW'].head()
This is the code for the plotting (I use seaborn
): 这是绘图的代码(我使用
seaborn
):
sns.distplot(df['BOW'].sum())
and this is the error: 这是错误:
TypeError: unsupported operand type(s) for /: 'Counter' and 'int'
TypeError:/不支持的操作数类型:“ Counter”和“ int”
this is how my bag of words looks like: 这是我的一堆单词的样子:
0 {'good': 2, 'need': 1, 'change': 1, 'virgils':...
0 {'好':2,'需要':1,'变更':1,'守夜':...
1 {'new': 3, 'favorite': 2, 'give': 1, 'delightf...
1 {'new':3,'favorite':2,'give':1,'delightf ...
2 {'red': 3, 'sauce': 2, 'favorite': 1, 'enjoy':...
2 {'red':3,'sauce':2,'favorite':1,1,'enjoy':...
3 {'quality': 1, 'fantastic': 1, '1800s': 1, '21...
3 {'quality':1,'fantastic':1,'1800s':1,'21 ...
4 {'red': 1, 'first': 1, 'time': 1, 'try': 1, 'l...
4 {'red':1,'first':1,'time':1,'try':1,'l ...
Any insight is appreciated! 任何见解表示赞赏!
You're generating Counter
objects. 您正在生成
Counter
对象。
>>> Counter('some words here are some repeated words'.split())
Counter({'words': 2, 'repeated': 1, 'some': 2, 'here': 1, 'are': 1})
While you can sum()
counter objects, ... 虽然可以
sum()
计数器对象,但是...
>>> Counter('aaabb') + Counter('abc')
Counter({'a': 4, 'b': 3, 'c': 1})
... you can't divide them to calculate average: ...您无法将它们除以计算平均值:
>>> (Counter('aaabb') + Counter('abc')) / 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'Counter' and 'int'
That's what your plotting library seems to be doing? 那就是您的绘图库正在做什么? Maybe you want to remove that column from the graph....
也许您想从图形中删除该列...。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.