NumPy

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

In this notebook we'll try various numpy methods and in the process learn more about NumPy.

Installation

Please follow this link

Importing NumPy

Once numpy is installed, we can import it in our file


In [1]:
import numpy as np

NumPy Arrays

In NumPy, strictly 1-D arrays are known as vectors and 2-D arrays are known as matrices (a matrix can also have only one row or one column). With NumPy arrays we can get access to various pre-written functions from NumPy.

Creating NumPy Arrays

There are various ways to create NumPy Arrays. Some of them are listed below.

  1. From a Python List

In [2]:
list = ['a', 'b', 'c', 'd']
list


Out[2]:
['a', 'b', 'c', 'd']

In [3]:
np.array(list)


Out[3]:
array(['a', 'b', 'c', 'd'], dtype='<U1')

In [4]:
list_matrix = [[1, 2, 3, 4, 5, 6, 7, 8, 9]]
list_matrix


Out[4]:
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]

In [5]:
np.array(list_matrix)


Out[5]:
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
  1. arange method

    arange ( starting_number, ending_number_plus_one, step? )

    Returns evenly spaced values within a given interval. step is optional


In [6]:
np.arange(1, 11)


Out[6]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

In [7]:
np.arange(5, 60, 5)


Out[7]:
array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55])
  1. zeros method

    zeros ( shape )

    Returns a new array of given shape and type, filled with zeros.


In [8]:
np.zeros(10)


Out[8]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [9]:
np.zeros((5, 5))


Out[9]:
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.]])
  1. ones method

    ones ( shape )

    Returns a new array of given shape and type, filled with ones.


In [10]:
np.ones(10)


Out[10]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

In [11]:
np.ones((5,5))


Out[11]:
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
  1. linspace

    linspace ( starting_number, ending_number, number_of_elements_in_array )

    Returns evenly spaced numbers over a specified interval.


In [12]:
np.linspace(10, 20, 5)


Out[12]:
array([10. , 12.5, 15. , 17.5, 20. ])

In [13]:
np.linspace(100, 101, 10)


Out[13]:
array([100.        , 100.11111111, 100.22222222, 100.33333333,
       100.44444444, 100.55555556, 100.66666667, 100.77777778,
       100.88888889, 101.        ])
  1. eye

    eye ( number_of_rows )

    Returns a 2-D array with ones on the diagonal and zeros elsewhere. (Returns and identity matrix)


In [14]:
np.eye(4)


Out[14]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

Random

  1. rand

    rand( shape )

    Returns random values in a given shape.


In [15]:
np.random.rand(5)


Out[15]:
array([0.77319868, 0.26329287, 0.73144465, 0.84149091, 0.2036499 ])

In [16]:
np.random.rand(3,3)


Out[16]:
array([[0.60726994, 0.42617981, 0.86102196],
       [0.86543814, 0.30908303, 0.24178713],
       [0.38308208, 0.61029731, 0.28254035]])

In [17]:
np.random.rand(2,3,4)


Out[17]:
array([[[0.16941154, 0.68912473, 0.43966093, 0.98221689],
        [0.99432939, 0.42056898, 0.25871228, 0.3741148 ],
        [0.30504666, 0.76178429, 0.71000702, 0.06467588]],

       [[0.24162657, 0.49613392, 0.97700371, 0.60832675],
        [0.59610994, 0.95313682, 0.46022336, 0.37118844],
        [0.73586936, 0.4774315 , 0.97240164, 0.55395152]]])
  1. randn

    randn ( shape )

    Returns a sample (or samples) from the "standard normal" distribution.


In [18]:
np.random.randn(3)


Out[18]:
array([0.02496177, 0.08214048, 0.28732458])

In [19]:
np.random.randn(4,4)


Out[19]:
array([[-0.22611994, -0.0807919 , -0.32115941,  0.33847872],
       [-1.21235511,  0.73971392, -0.00640343, -0.58285227],
       [-0.2409442 ,  0.21120981,  0.8262523 , -1.034613  ],
       [-0.55126158,  1.56535933, -0.686484  ,  0.06669024]])
  1. randint

    randint( low, high?, size? )

    Returns random integers from low (inclusive) to high (exclusive).


In [20]:
np.random.randint(5)


Out[20]:
3

In [21]:
np.random.randint(1,11)


Out[21]:
3

In [22]:
np.random.randint(1, 100, 10)


Out[22]:
array([18, 25, 83, 41, 29, 99, 16, 24, 50, 75])

In [23]:
np.random.randint(1, 100, (4, 4))


Out[23]:
array([[74, 27, 76, 99],
       [25, 92, 86, 45],
       [28, 13, 82, 52],
       [41, 32, 94, 84]])

Array Attributes

max, min, argmax, argmin


In [24]:
list = np.arange(1,10)

In [25]:
list.max()


Out[25]:
9

In [26]:
list.min()


Out[26]:
1

In [27]:
list.argmax()


Out[27]:
8

In [28]:
list.argmin()


Out[28]:
0

Reshape, Shape and Ravel

  1. reshape

    reshape ( shape )

    Returns an array containing the same data with a new shape.


In [29]:
reshape_list = np.arange(1,10)
reshape_list


Out[29]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

In [30]:
reshape_list.reshape(3,3)


Out[30]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
  1. Shape

    Its an attribute which returns the shape (in a tuple) of the array.


In [31]:
shape_list = np.arange(1,10)
shape_list


Out[31]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

In [32]:
shape_list.shape


Out[32]:
(9,)

In [33]:
shape_list.reshape(3,3)


Out[33]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [34]:
shape_list.reshape(3,3).shape


Out[34]:
(3, 3)
  1. ravel

    ravel ( )

    Returns a flattened array. (does not return a new copy)


In [35]:
ravel_list = np.arange(1,26).reshape(5,5)
ravel_list


Out[35]:
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])

In [36]:
ravel_list = ravel_list.ravel()
ravel_list


Out[36]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25])
  1. flatten

    flatten ( )

    Returns a copy of the array collapsed into one dimension.


In [37]:
flatten_list = np.arange(1,26).reshape(5,5)
flatten_list


Out[37]:
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])

In [38]:
flatten_list = flatten_list.flatten()
flatten_list


Out[38]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25])

dtype

Its an attribute which returns the data type of the object


In [39]:
list = np.arange(1,11)
list.dtype


Out[39]:
dtype('int64')

In [40]:
list = np.array(['a', 'b', 'c'])
list.dtype


Out[40]:
dtype('<U1')

Selection

  1. index-based selection

    For normal 1-D lists


In [41]:
selection_list = np.arange(1,26)
selection_list


Out[41]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25])

In [42]:
selection_list[9]


Out[42]:
10

In [43]:
selection_list[24]


Out[43]:
25
For multi dimensional lists

In [44]:
selection_list = selection_list.reshape(5, 5)
selection_list


Out[44]:
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])

In [45]:
selection_list[2:,1:]


Out[45]:
array([[12, 13, 14, 15],
       [17, 18, 19, 20],
       [22, 23, 24, 25]])

In [46]:
selection_list[1:4,2:4]


Out[46]:
array([[ 8,  9],
       [13, 14],
       [18, 19]])
  1. comparison selectors

In [47]:
selection_list = selection_list.ravel()
selection_list


Out[47]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25])

In [48]:
selection_list[selection_list>10]


Out[48]:
array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25])

Universal Functions

NumPy comes with many universal functions. Some of it are in the next cells.


In [49]:
uni_list = np.arange(1,11)
uni_list


Out[49]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

In [50]:
np.square(uni_list)


Out[50]:
array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100])

In [51]:
np.sin(uni_list)


Out[51]:
array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 , -0.95892427,
       -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849, -0.54402111])

In [52]:
np.log(uni_list)


Out[52]:
array([0.        , 0.69314718, 1.09861229, 1.38629436, 1.60943791,
       1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509])

In [53]:
np.log10(uni_list)


Out[53]:
array([0.        , 0.30103   , 0.47712125, 0.60205999, 0.69897   ,
       0.77815125, 0.84509804, 0.90308999, 0.95424251, 1.        ])

In [54]:
np.isfinite(uni_list)


Out[54]:
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True])