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.
What are the differences between Python Array, Numpy Array and Panda Dataframe? When do I use which?
Rust ndarray vs. Python NumPy Performance?
I am very curious in what you find out. Although if you are interested in pursuing machine learning at all, you should do these projects in Python (even if you do them in Rust first). The entire ML industry is very heavily geared around Python, and ML teams are unlikely to know Rust. Often they are more math focused and less comfortable with programming syntax in general, so anything that eases communication friction is advisable.
That said, I am very interested in how well Rust handles common tasks that I might do with NumPy. I was just pondering porting a noise-based image generation Python script that uses NumPy over to Rust.
More on reddit.comWhat is the difference between <class 'numpy.ndarray'> and numpy.ndarray? - Stack Overflow
Numpy vs Ndarray speed?
As mentioned in the title, preferably a more ELI answer if possible. Thank you!
I am currently taking a machine learning course at the university that I attend, and it seems like an overwhelming majority of the class is using Python.
I decided to implement our first project in Rust, because I love Rust, and I have been really happy with the results using the ndarray crate. I've never used Python, though it seems like a clear winner in the machine-learning community.
After a quick google search, I can't find any comparisons on the performance of ndarray vs numpy.
I do see a nice document comparing the APIs: https://docs.rs/ndarray/0.12.1/ndarray/doc/ndarray_for_numpy_users/index.html
Does anyone have any experience with both?
I am very curious in what you find out. Although if you are interested in pursuing machine learning at all, you should do these projects in Python (even if you do them in Rust first). The entire ML industry is very heavily geared around Python, and ML teams are unlikely to know Rust. Often they are more math focused and less comfortable with programming syntax in general, so anything that eases communication friction is advisable.
That said, I am very interested in how well Rust handles common tasks that I might do with NumPy. I was just pondering porting a noise-based image generation Python script that uses NumPy over to Rust.
There are two standards for math API libraries – BLAS and Lapack. Between them these are to maths what OpenGL is to graphics.
Vendors make their own compatible implementations of these library APIs: Intel has the MKL, and even NVidia has CuBLAS.
There are also many open-source implementations, like GotoBLAS and Atlas.
Numpy wraps whichever BLAS library it finds on your machine. The features it offers are fairly bare-bones. As soon as you get into any decent sort of math – machine learning in my case – you need some of the features in Lapack which Scipy wraps and (significantly) augments.
I would expect Numpy to be as fast or faster than ndarry. Some of the BLAS implementations it wraps like GotoBLAS are super-mature and optimised, with chunks of handcrafted assembly.
Ndarray it seems has experimental support to delegate to native BLAS which may help.
For your purposes, you need to consider what your project needs to deliver. If it is a novel implementation of an existing machine-learning method, then Rust is great. If it is a broader project that uses machine learning tools, choosing Python maximises your chances of success.
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