Numpy quick start

Numpy quick start

Before You Begin

Note
Make sure you installed NumPy in your system, if not, please take a look at Install scipy

The Basics

NumPy’s main object is the homogeneous multidimensional array. It 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, so we say it has a length of 3. In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.

[[ 1., 2., 0.],
 [ 0., 5., 3.]]

Array in NumPy is called ndarray which have some important objects:

  • ndarray.ndim the number of axes (dimensions) of the array.
  • ndarray.shape the dimensions of the array.
  • ndarray.size the total number of elements of the array.
  • ndarray.dtype an object describing the type of the elements in the array.
  • ndarray.itemsize the size in bytes of each element of the array.
  • ndarray.data the buffer containing the actual elements of the array.

Example

>>> import numpy as np
>>> a = np.arange(20).reshape(4,5)
>>> a
array([[ 0,  1,  2,  3,  4],
      [ 5,  6,  7,  8,  9],
      [10, 11, 12, 13, 14],
      [15, 16, 17, 18, 19]])
>>> a.shape
(4, 5)
>>> a.ndim
2
>>> a.size
20
>>> a.dtype
dtype('int32')
>>> a.itemsize
4
>>> a.data
<memory at 0x000001BEC6B93CF0>
>>>

Array creatation

>>> import numpy as np
>>> a = np.array([3,4,5,6])
>>> a
array([3, 4, 5, 6])
>>> b = np.array([(1,2,3), (4,5,6)])
>>> b
array([[1, 2, 3],
       [4, 5, 6]])
>>> c = np.array( [ [3,4], [5,6] ], dtype=complex )
>>> c
array([[3.+0.j, 4.+0.j],
       [5.+0.j, 6.+0.j]])
>>> d = np.zeros(5,6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: data type not understood
>>> d = np.zeros((5,6))
>>> d
array([[0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.]])
>>>

Basic Operations

Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.

>>> a = np.array( [50,60,70,80] )
>>> b  = np.array( [20,30,40,50] )
>>> c = a + b
>>> c
array([ 70,  90, 110, 130])
>>> d = a - b
>>> d
array([30, 30, 30, 30])
>>>

Subarrays as no-copy views

>>> import numpy as np
>>> x = np.random.randint(10, size=(3, 4))
>>> x
array([[7, 7, 8, 0],
       [3, 3, 9, 2],
       [7, 2, 2, 0]])
>>> x_sub = x[:2, :2]
>>>
>>> x_sub
array([[7, 7],
       [3, 3]])
>>> x_sub[0, 0] = 1000
>>> x
array([[1000,    7,    8,    0],
       [   3,    3,    9,    2],
       [   7,    2,    2,    0]])
>>>

To create copy of a array, call .copy() method

>>> x_sub_copy = x[:2, :2].copy()
>>> x_sub_copy[0, 0] = 123

>>> x_sub_copy
array([[123,   7],
       [  3,   3]])
>>> x
array([[1000,    7,    8,    0],
       [   3,    3,    9,    2],
       [   7,    2,    2,    0]])
>>>

Concatenation of arrays

>>> x = np.array([4,5,6])
>>> y = np.array([3,6,2,3,4,5])
>>> z = np.concatenate([x,y])
>>> z
array([4, 5, 6, 3, 6, 2, 3, 4, 5])
>>>

Splitting of arrays

>>> x = np.array([1,2,3,4,2,3,12,3,1,2,2,5,3,5,3,6,2,2,2])
>>> x
array([ 1,  2,  3,  4,  2,  3, 12,  3,  1,  2,  2,  5,  3,  5,  3,  6,  2,
        2,  2])
>>> x1, x2, x3 = np.split(x, [5,6])
>>> x1, x2, x3
(array([1, 2, 3, 4, 2]), array([3]), array([12,  3,  1,  2,  2,  5,  3,  5,  3,  6,  2,  2,
2]))
>>>
Last modified October 4, 2020