我是数据科学的新手,刚刚安装numpy
系统。因此,当我运行numpy.mean(num)
函数来计算数字数组的平均值时,我卡在了一个地方。所以,有人能提出解决方案吗?
我使用的是 Ubuntu 17.04。以下是错误代码:
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> num = {1,2,3,4,5,6,7}
>>> import numpy
>>> numpy.mean(num)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/vaibhav/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2909, in mean
out=out, **kwargs)
File "/home/vaibhav/.local/lib/python2.7/site-packages/numpy/core/_methods.py", line 82, in _mean
ret = ret / rcount
TypeError: unsupported operand type(s) for /: 'set' and 'int'
>>> numpy.median(num)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/vaibhav/.local/lib/python2.7/site-packages/numpy/lib/function_base.py", line 4102, in median
overwrite_input=overwrite_input)
File "/home/vaibhav/.local/lib/python2.7/site-packages/numpy/lib/function_base.py", line 4016, in _ureduce
r = func(a, **kwargs)
File "/home/vaibhav/.local/lib/python2.7/site-packages/numpy/lib/function_base.py", line 4160, in _median
return mean(part[indexer], axis=axis, out=out)
File "/home/vaibhav/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2909, in mean
out=out, **kwargs)
File "/home/vaibhav/.local/lib/python2.7/site-packages/numpy/core/_methods.py", line 82, in _mean
ret = ret / rcount
TypeError: unsupported operand type(s) for /: 'set' and 'int'
答案1
这是因为num = {1,2,3,4,5,6,7}
这不是“数字数组”——而是放。
>>> num = {1,2,3,4,5,6,7}
>>> type(num)
<type 'set'>
>>> numpy.mean(num)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2885, in mean
out=out, keepdims=keepdims)
File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 72, in _mean
ret = ret / rcount
TypeError: unsupported operand type(s) for /: 'set' and 'int'
然而
>>> num = (1,2,3,4,5,6,7)
>>> type(num)
<type 'tuple'>
>>> numpy.mean(num)
4.0