NumPy

Why NumPy ?

NumPy is an acronym for "Numeric Python" or "Numerical Python"

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • A powerful N-dimensional array object (ndarray) - efficiently implemented multi-dimensional arrays
  • Array oriented computing - sophisticated (broadcasting) functions
  • Tools for integrating C/C++ and Fortran code
  • Designed for scientific computation - useful linear algebra, Fourier transform, and random number capabilities

In [ ]:
# import NumPy library
# This library is bundled along with anaconda distribution
# np alias is the standard convention

import numpy as np

numpy array (ndarray)

  • A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers

  • ndarray.ndim - the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.

  • ndarray.shape - the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension.

  • ndarray.size - the total number of elements of the array. This is equal to the product of the elements of shape.

  • ndarray.dtype - an object describing the type of the elements in the array.


In [ ]:
%%timeit
temp_list = range(100000)
temp_list1 = [ x*2 for x in temp_list]

In [ ]:
%%timeit
temp_array = np.arange(100000)
temp_array1 = temp_array*2

In [ ]:
# ndarray can be created for regular python list or tupple
mylist = [2,5,8,15,25]
array = np.array(mylist)

In [ ]:
type(array)

In [ ]:
array.shape

In [ ]:
array[0]

In [ ]:
array[0:3]

In [ ]:
array.dtype

In [ ]:
array.ndim

In [ ]:
# dtype can be mentioned while creating an array
array2 = np.array(mylist,dtype=np.float)

In [ ]:
array2

In [ ]:
array2.dtype

In [ ]:
# creating a 5 X 3 multi dimensional array
marray = np.arange(15).reshape(5,3)

In [ ]:
marray

In [ ]:
marray.ndim

In [ ]:
marray.shape

In [ ]:
marray.size

In [ ]:
# ravel function generates a flattens 
marray.ravel()

In [ ]:
# reshape can be used to change the shape of an array
marray.ravel().reshape(3,5)

In [ ]:
marray.shape

Array basic operations


In [ ]:
# multiplying a scalr and ndarray
print(marray*2)

In [ ]:
marray

In [ ]:
# inplace change
# there are certain operations that will modify the object inplace like one below
marray += 10

In [ ]:
marray

In [ ]:
# Guess - what would be the result of the following
marray > 15

In [ ]:
arr_A = np.array( [ [2,3], [4,5] ] )
arr_B = np.array( [ [1,1], [2,1] ] )

In [ ]:
# * operates element wise
arr_A * arr_B

In [ ]:
# dot is used for matrix multiplication
# np.dot(arr_A,arr_B) also works
arr_A.dot(arr_B)

Array Slicing


In [ ]:
marray[0]

In [ ]:
marray[0,1]

In [ ]:
marray

In [ ]:
marray[:,1:3]

In [ ]:
marray[0:3,:]

In [ ]:
marray[1:3,1:]

Broadcasting

  • The term Broadcasting describes how numpy treats arrays with different shapes during arithmetic operations.
  • Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.
  • Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python.

In [ ]:
marray

In [ ]:
marray + 5

<img src="images/fig_broadcast_visual_1.png" alt="Broadcasting" height="500" width="500", align="left">

Exercises


In [ ]:
# Exercise - 1
# Construct  3 by 3 ndarray with 5 as diagonal elemet and 1 as remaining elements
# [[5, 1, 1][1,5,1][1,1,5]]
# Tip : explore np.ones and np.eye functions
# the dtype should be int

In [ ]:
# Exercise
# try following array slicing
a = np.arange(0,60).reshape(6,10)[0:6,0:6]


In [ ]:
a

NumPy functions used for performing computations

  • np.sum
  • np.std
  • np.mean
  • np.max
  • np.min

In [ ]:
# np.NaN is a datatype - Not a Number
np.NaN?

In [ ]:
np.random.seed(0)
arr_c = np.random.random(15).reshape((5,3))
arr_c

In [ ]:
arr_c.sum()

In [ ]:
arr_c.min()

In [ ]:
arr_c.max()

In [ ]:
arr_c.mean()

In [ ]:
arr_c.mean(axis=0)

In [ ]:
arr_c.mean(axis=1)

In [ ]: