Quickstart tutorial
[0]https://docs.scipy.org/doc/numpy-1.15.1/user/quickstart.html
[1]CoLab example: goo.gl/S75cL6
Prerequisites
You should know a bit of Python, take a look at the https://docs.python.org/3/tutorial/
You must also have some software installed on your computer, visit https://scipy.org/install.html for instructions.
The Basics
NumPy’s main object is the homogeneous multidimensional array. A homogeneous multi-dimensional array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy, dimensions are called axes.
For example, the coordinates of a point in 3D space[1, 2, 1]has one axis. That axis has 3 elements in it.
In other words, that axis has a length of 3.
In the example below, the array has 2 axes.
The first axis has a length of 2, the second axis has a length of 3.
[[1.,0.,0.],
[0.,1.,2.]]
NumPy’s array class is calledndarray
. It is also known by the aliasarray
. Note thatnumpy.array
is not the same as the Standard Python Library classarray.array
, which only handles one-dimensional arrays and offers less functionality. The more important attributes of anndarray
object are:
ndarray.ndim
the number of axes (dimensions) of the array.
ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with
_n _rows and _m _columns,shape
will be(n,m)
. The length of theshape
tuple is therefore the number of axes,ndim.
ndarray.size
the total number of elements of the array. This is equal to the product of the elements ofshape
.ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of typefloat64
has itemsize
8 (=64/8), while one of typecomplex32
hasitemsize
4 (=32/8). It is equivalent tondarray.dtype.itemsize
ndarray.data
the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.
Below are some example codes to illustrate the functions associated to dimensions: Ndim, shape, size, dtype, itemsize, data.
Array Creation
There are several ways to create arrays.
For example, an array can be created from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences.
A frequent error consists in calling
array
with multiple numeric arguments, rather than providing a single list of numbers as an argument.
array
transforms sequences of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.
The type of the array can also be explicitly specified at creation time:
NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, which is an expensive operation.
The functionzeros
creates an array full of zeros, the functionones
creates an array full of ones, and the functionempty
creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array isfloat64
.
Printing Arrays
Printing an array in NumPy is similar to printing a nested lists.
The layout difference between printing an array and nested lists are
1 the last axis is printed from left to right,
2 the second-to-last is printed from top to bottom,
3 the rest are also printed from top to bottom, with each slice separated from the next by an empty line.
One-dimensional arrays are printed as rows, bi-dimensionals as matrices and tri-dimensionals as lists of matrices.
If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners:
To disable this behaviour and force NumPy to print the entire array, you can change the printing options using
set_printoptions
.np.set_printoptions(threshold=np.nan)
Basic Operations
Arithmetic operators on arrays apply
elementwise
. A new array is created and filled with the result.
Unlike in many matrix languages, the product operator * operates elementwise in NumPy arrays. The matrix product can be performed using the @ operator (in python >=3.5) or the dot function or method:
Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the ndarray class.
By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the axis parameter you can apply an operation along the specified axis of an array: