Numpy error: Kesalahan saat menjalankan numpy.mean (num) (di mana num adalah array) [ditutup]


0

Saya baru mengenal ilmu data dan baru saja menginstal numpydi sistem. Jadi, saya terjebak di suatu tempat ketika saya menjalankan numpy.mean(num)fungsi untuk menghitung rata-rata dari array angka. Jadi, adakah yang bisa menyarankan solusinya?

Saya menggunakan Ubuntu 17.04. Berikut ini adalah kode untuk kesalahan:

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'

Jawaban:


1

Itu karena num = {1,2,3,4,5,6,7}itu bukan "array angka" - itu satu set .

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

sedangkan

>>> num = (1,2,3,4,5,6,7)
>>> type(num)
<type 'tuple'>
>>> numpy.mean(num)
4.0
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.