简体   繁体   English

/:'Counter'和'int'的不受支持的操作数类型-单词袋

[英]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.

相关问题 * 不支持的操作数类型:&#39;int&#39; 和 &#39;NoneType&#39; - Unsupported operand type(s) for *: 'int' and 'NoneType' /不支持的操作数类型:“ str”和“ int”,但不同 - unsupported operand type(s) for /: 'str' and 'int' but different TypeError:+ =不支持的操作数类型:“ int”和“ instancemethod” - TypeError: unsupported operand type(s) for +=: 'int' and 'instancemethod' -=:&#39;str&#39;和&#39;int&#39;的不受支持的操作数类型 - unsupported operand type(s) for -=: 'str' and 'int' 类型错误:不支持 / 的操作数类型:&#39;tuple&#39; 和 &#39;int&#39; - TypeError: unsupported operand type(s) for /: 'tuple' and 'int' 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' TypeError:-:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'str' **或pow()不支持的操作数类型:“函数”和“整数” - unsupported operand type(s) for ** or pow(): 'function' and 'int' Python 不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; - Python unsupported operand type(s) for /: 'str' and 'int' TypeError:+不支持的操作数类型:“ int”和“ IntervalMap” - TypeError: unsupported operand type(s) for +: 'int' and 'IntervalMap'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM