I have been doing some calculations using numpy arrays and have arrived at the question of what is the difference between:
<class 'numpy.ndarray'>
and numpy.ndarray
I have noticed that the following operation only works on <class 'numpy.ndarray'>
:
(arrray == 1).sum()
Why is that so?
CodePudding user response:
There's no difference; they're identical.
numpy.ndarray
is the actual type of numpy arrays; <class 'numpy.ndarray'>
is the string represention ot numpy.ndarray
:
>>> import numpy as np
>>> a = np.array([1, 2, 3])
array([1, 2, 3])
>>> print(type(a) == np.ndarray)
True
>>> np.ndarray
<class 'numpy.ndarray'>
>>> print(type(a))
<class 'numpy.ndarray'>
>>> str(type(a))
"<class 'numpy.ndarray'>"
>>> repr(type(a))
"<class 'numpy.ndarray'>"
Python interpreters such as IPython and Jupyter (which underneath are actually the same thing) will trim of the <class '...' >
part and only show the type itself when you enter the type the into interpreter, e.g. ipython
:
$ ipython
Python 3.9.9 (main, Nov 21 2021, 03:23:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
In [2]: np.ndarray
Out[2]: numpy.ndarray
...versus python
(the builtin interpreter):
$ python3
Python 3.9.9 (main, Nov 21 2021, 03:23:44)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.ndarray
<class 'numpy.ndarray'>
...but they're the exact same type.
CodePudding user response:
I wonder if you are confusing numpy arrays and python lists. I/we often talk about a numpy
array, meaning actually an object of class/type np.ndarray
.
In [144]: a = [1, 2, 3] # a list
In [145]: b = np.array(a) # an array
In [146]: type(a), type(b)
Out[146]: (list, numpy.ndarray)
Your expression works with the array, but not the list:
In [147]: (b == 1).sum()
Out[147]: 1
In [148]: (a == 1).sum()
Traceback (most recent call last):
Input In [148] in <module>
(a == 1).sum()
AttributeError: 'bool' object has no attribute 'sum'
In [149]: b == 1
Out[149]: array([ True, False, False])
In [150]: a == 1
Out[150]: False
Note that I created b
with np.array()
. There is a np.ndarray
function, but we don't usually use it - it's a low level creator that most of us don't need. A useful starting page: