NumPy
NumPy is a Python package with well-optimized C core.
NumPy provides powerful N-dimensional array computing through the using of concepts of vectorization, indexing, and broadcasting. Besides, Numpy also offers comprehensive mathematical functions for numerical computing.
Fast and versatile, the NumPy vectorization, indexing, and broadcasting concepts are the de-facto standards of array computing today.
Installation
The only prerequisite for installing NumPy is Python itself.
Conda
# Best practice, use an environment rather than install in the base env
conda create -n my-env
conda activate my-env
# If you want to install from conda-forge
conda config --env --add channels conda-forge
# The actual install command
conda install numpy
PIP
pip install numpy
Anaconda
The installation of Anaconda will also installs Numpy and some other useful tools and packages.
Numpy Objects
Working data of NumPy is stored in an N-dimensional array object.
Array Objects
ndarray Object
The N-dimensional array,
ndarray, is a multidimensional container of items of the same type and size. Different
ndarrays can share th same data as usual. That is an
ndarray can be a
view to another
ndarray. The typical characteristics of a
ndarray are
dtype and
shape
Examples
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=[[1,2,3],[4,5,6],[7,8,9]]
>>> x
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> type(x)
<class 'list'>
>>> import numpy as np
>>> x=np.array(x)
>>> type(x)
<class 'numpy.ndarray'>
>>> x.dtype
dtype('int32')
>>> x.shape
(3, 3)
>>>
Array Scalars
A array scalar is the basic array unit element of a
ndarray.
Array scalars have the same attributes and methods as ndarrays.
Examples
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> x.shape
(3, 3)
>>> x.dtype
dtype('int32')
>>> x[0,0]
1
>>> x[0,0].shape
()
>>> x[0,0].dtype
dtype('int32')
>>>
Data type objects
A data type object is used to describe how the bytes in the fixed-size block of memory corresponding to an array item of a
ndarray should be interpreted.
Examples
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x=np.array([3])
>>> x.dtype
dtype('int32')
>>>
>>> np.int32
<class 'numpy.int32'>
>>>
>>> dt=np.dtype('int32')
>>> dt.name
'int32'
>>> dt.itemsize
4
>>> dt.byteorder
'='
>>> dt.kind
'i'
>>>
Examples
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> dt = np.dtype([('string', np.unicode_, 16), ('numberpair', np.float64, (2,))
])
>>> dt
dtype([('string', '<U16'), ('numberpair', '<f8', (2,))])
>>> dt['string']
dtype('<U16')
>>> dt['numberpair']
dtype(('<f8', (2,)))
>>>
>>> x = np.array([('asdf', (1,2)), ('hjkl', (3,4))], dtype=dt)
>>> x.shape
(2,)
>>> x.dtype
dtype([('string', '<U16'), ('numberpair', '<f8', (2,))])
>>> type(x)
<class 'numpy.ndarray'>
>>> x[0]
('asdf', [1., 2.])
>>> x[0].shape
()
>>> x[0].dtype
dtype([('string', '<U16'), ('numberpair', '<f8', (2,))])
>>> type(x[0])
<class 'numpy.void'>
>>> x[0]['string']
'asdf'
>>> x[0]['string'].shape
()
>>> x[0]['string'].dtype
dtype('<U4')
>>> type(x[0]['string'])
<class 'numpy.str_'>
>>>
Constants
NumPy includes some common constants, such as infinity, e, pi, for floating-point arithmetic
Source and Reference
- https://numpy.org/
- https://numpy.org/install/
- https://numpy.org/doc/stable/reference/index.html
- https://numpy.org/doc/stable/index.html
- https://en.wikipedia.org/wiki/NumPy