Numpy Primer

Getting acquainted with Numpy

Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.

Why learn Numpy?

In the Introductory Course on Machine learning, we will be using numpy to build the models introduced in Andrew Ng’s Machine Learning course and solve the excercises. Note that octave is used in that course instead. Since most of the existing machine learning frameworks today are based on python (Tensorflow, Theano, etc), I figured it would be practical to use numpy to understand these basic machine learning models. Numpy is also easy to learn and experiment with and it plays well with the Tensorflow and Theano.

Lets start

We will be working with arrays. Arrays are numpy objects of type ndarray. They contain elements of type dtype and have a particular shape.

Numpy arrays from python lists

importnumpyasnp# a list of numbersx_list=[1,2,3,4,5]printtype(x_list)# an array of numbersx=np.array(x_list)y=np.array([6,7,8,9])printtype(x),type(y)printx,x.shape,x.dtype# nested listsnes_list=[[1,2,3],[4,5,6],[7,8,9]]nes_array=np.array(nes_list)

Array Indexing

# lets create a 4x4 matrixx=np.arange(16).reshape(4,4)printx# To get the element 4 at position (2,2); printx[1,1]# index starts from 0# To get the element 10 at position (3,3); printx[2,2]# To get row 2printx[1]# To get column 3printx[:,2]# To get elements 6,10,14printx[1:,2]# To get elements 13,14,15printx[-1,1:]# Boolean indexing / Conditionalprintx>7printx%2==0printx[x<7]printx[x%2!=0]

Broadcasting

x=np.arange(16).reshape([4,4])# array[1] is broadcasted to shape [4,4] of xprintx+1# consider 2 arrays of shapes : [4,4] and [4,]a=np.random.randint(0,10,[4,4])b=np.arange(4)printa*b# here b is broadcasted to all the rows of a

Array Masking

'''
Array masking is the name of a special method of selection available in numpy by means of a boolean mask, it allows to extract data out of an array based on certain condition.
'''arr=np.arange(9).reshape([3,3])printarr# create a random boolean arraymask1=np.random.choice([True,False],[3,3]).astype(np.bool)printmask1# mask over arrayprintarr[mask1]# using conditions to create maskdiv_4_mask=(arr%4==0)printdiv_4_maskprintarr[div_4_mask]# Efficient masking using np.putmask() function# using a <divisible by 3> masknp.putmask(arr,arr%3==0,0)printarr# there is an option to operate on the elements that satisfy# the condition <divisible by 3># enter the operation as parameter 3; say multiply by 10arr=np.arange(9).reshape([3,3])printarr# if divisible by 3 multiply by 10np.putmask(arr,arr%3==0,arr*10)printarr