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
Answer from wim on Stack Overflownumpy.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.
numpy - what is a reason to use ndarray instead of python array - Stack Overflow
What is the difference between <class 'numpy.ndarray'> and numpy.ndarray? - Stack Overflow
Numpy vs Ndarray speed?
What are the differences between Python Array, Numpy Array and Panda Dataframe? When do I use which?
NumPy and Python arrays share the property of being efficiently stored in memory.
NumPy arrays can be added together, multiplied by a number, you can calculate, say, the sine of all their values in one function call, etc. As HYRY pointed out, they can also have more than one dimension. You cannot do this with Python arrays.
On the other hand, Python arrays can indeed be appended to. Note that NumPy arrays can however be concatenated together (hstack(), vstack(),…). That said, NumPy arrays are mostly meant to have a fixed number of elements.
It is common to first build a list (or a Python array) of values iteratively and then convert it to a NumPy array (with numpy.array(), or, more efficiently, with numpy.frombuffer(), as HYRY mentioned): this allows mathematical operations on arrays (or matrices) to be performed very conveniently (simple syntax for complex operations). Alternatively, numpy.fromiter() might be used to construct the array from an iterator. Or loadtxt() to construct it from a text file.
There are at least two main reasons for using NumPy arrays:
- NumPy arrays require less space than Python lists. So you can deal with more data in a NumPy array (in-memory) than you can with Python lists.
- NumPy arrays have a vast library of functions and methods unavailable to Python lists or Python arrays.
Yes, you can not simply convert lists to NumPy arrays and expect your code to continue to work. The methods are different, the bool semantics are different. For the best performance, even the algorithm may need to change.
However, if you are looking for a Python replacement for Matlab, you will definitely find uses for NumPy. It is worth learning.
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