Factsheet
numpy.array is just a convenience function to create an ndarray; it is not a class itself.
You can also create an array using numpy.ndarray, but it is not the recommended way. From the docstring of numpy.ndarray:
Arrays should be constructed using
array,zerosorempty... The parameters given here refer to a low-level method (ndarray(...)) for instantiating an array.
Most of the meat of the implementation is in C code, here in multiarray, but you can start looking at the ndarray interfaces here:
https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py
numpy.array is a function that returns a numpy.ndarray object.
There is no object of type numpy.array.
There are several differences:
- You can append elements to a list, but you can't change the size of a ´numpy.ndarray´ without making a full copy.
- Lists can containt about everything, in numpy arrays all the elements must have the same type.
- In practice, numpy arrays are faster for vectorial functions than mapping functions to lists.
- I think than modification times is not an issue, but iteration over the elements is.
- Numpy arrays have many array related methods (´argmin´, ´min´, ´sort´, etc).
I prefer to use numpy arrays when I need to do some mathematical operations (sum, average, array multiplication, etc) and list when I need to iterate in 'items' (strings, files, etc).
I read from various links and resources and summarize my understanding as below:
The main benefits of using numpy arrays should be smaller memory consumption and better runtime behavior.
One of the main advantages of NumPy is its advantage in time compared to standard Python.
Good Explanation : https://www.python-course.eu/numpy.php
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.
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:
https://numpy.org/doc/1.22/user/basics.creation.html