Erro numpy: Erro ao executar numpy.mean (num) (onde num é um array) [closed]

0

Eu sou novo em ciência de dados e instalei numpy no sistema. Então, eu fiquei em um lugar quando eu corri a função numpy.mean(num) para calcular a média da matriz de números. Então, alguém pode sugerir a solução.

Estou usando o Ubuntu 17.04. A seguir está o código para erros:

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'
    
por vaibhav kaushik 19.11.2017 / 17:20

1 resposta

1

Isso porque num = {1,2,3,4,5,6,7} não é uma "matriz de números" - é um conjunto .

>>> 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'

enquanto

>>> num = (1,2,3,4,5,6,7)
>>> type(num)
<type 'tuple'>
>>> numpy.mean(num)
4.0
    
por steeldriver 19.11.2017 / 17:28