Numpy 2: Indexing , Slicing

Numpy is also incredibly fast, as it has bindings to C libraries. First importing numpy as alias it as np to use np instead of numpy to make to code shorter.

In [1]:
import numpy as np
		

Lets create a two dimensional array. First we are creating two dimensional array then converting it to numpy array.

In [2]:
python_list = [[1,2,3],[4,5,6]]
		numpy_arr = np.array(python_list)
		print(python_list)
		print("*" * 30)
		print(numpy_arr)
		

[[1, 2, 3], [4, 5, 6]]
		******************************
		[[1 2 3]
		 [4 5 6]]
		

See the difference between a general python list and numpy two dimensional array.
Now We are declaring numpy_arr to arr1 and arr2 to square of the arr1. We can perform any mathmatical operations between two numpy arrays like below.

In [3]:
arr1 = numpy_arr
		arr2 = arr1*arr1
		print(arr1)
		print("*" * 30)
		print(arr2)
		print("*" * 30)
		print(arr2-arr1)
		print("*" * 30)
		print(arr2/arr1)
		print("*" * 30)
		print(arr2-arr2)
		

[[1 2 3]
		 [4 5 6]]
		******************************
		[[ 1  4  9]
		 [16 25 36]]
		******************************
		[[ 0  2  6]
		 [12 20 30]]
		******************************
		[[ 1.  2.  3.]
		 [ 4.  5.  6.]]
		******************************
		[[0 0 0]
		 [0 0 0]]
		

Lets see some Built in methods

Arange

Return evenly spaced values within a given interval. It can have three parameters (first value, last value,increment/decrement)

In [4]:
arr1 = np.arange(20)#from 0 to 19 
		arr2 = np.arange(20,40)#from 20 to 39
		arr3 = np.arange(20,40,3)#starting from 20 and increment by 3 ending at 39
		print(arr1)
		print(arr2)
		print(arr3)
		

[ 0  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 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
		[20 23 26 29 32 35 38]
		

Indexing

In [6]:
arr = np.arange(1,11)#creating a simple array
		arr
		

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

In [7]:
arr[1]
		

Out[7]:
2

Lets print values 5 6 7

In [9]:
arr[4:7] # arr[row,column+1]
		

Out[9]:
array([5, 6, 7])

Broadcasting

Numpy arrays are different from a normal Python list because of their ability to broadcast:

In [10]:
#Setting a value with index range (Broadcasting)
		arr[0:5]=100

		#Show
		arr
		

Out[10]:
array([100, 100, 100, 100, 100,   6,   7,   8,   9,  10])

In [11]:
# Reset array, we'll see why I had to reset in  a moment
		arr = np.arange(0,11)

		#Show
		arr
		

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

Look carefully

In [12]:
#Important notes on Slices
		slice_of_arr = arr[0:6]

		#Show slice
		slice_of_arr
		

Out[12]:
array([0, 1, 2, 3, 4, 5])

In [13]:
#Change Slice
		slice_of_arr[:]=99

		#Show Slice again
		slice_of_arr
		

Out[13]:
array([99, 99, 99, 99, 99, 99])

Now note the changes also occur in our original array!

In [14]:
arr
		

Out[14]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

Data is not copied, it’s a view of the original array! This avoids memory problems!

In [16]:
#To get a copy, need to be explicit
		arr_copy = arr.copy()

		arr_copy
		

Out[16]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

In [ ]: