03 Numpy
03 Numpy
03 Numpy
March 4, 2024
1 Importing Numpy
[99]: import numpy as np
1.1 Arrays
Why we use Numpy array and not just a list?
• Memory efficient
• Easily expandable to N-dimensions
• Speed of calculations of numpy array
• Broadcasting operations and functions
[101]: type(lst)
[101]: list
[102]: type(arr)
[102]: numpy.ndarray
[104]: np.array(mat)
1
1.2 Built-in Methods to create arrays
[105]: #arange: return evenly spaces values within a given interval
np.arange(0,10)
[106]: np.arange(0,11,3) # here the last column denotes the step size
[108]: np.zeros((3,4))
[109]: np.ones((4,2))
np.eye(5)
2
2 Random
[112]: #rand: create array of given shape and populates it with random samples from a␣
↪uniform distribution over [0,1)
np.random.rand(9)
[113]: np.random.rand(3,4)
[114]: #randn: return a sample from the standard normal distribution i.e numbers will␣
↪be distributed around 0
np.random.randn(10)
[115]: np.random.randn(5,4)
[116]: 8
## Also, everytime we run this command, different set of random numbers will be␣
↪generated, so if we want that everytime the same
3
2.1 Seed : generates same set of random numbers everytime for a particular
seed number
[118]: np.random.seed(1) #it is same as random_state in Train Test␣
↪split
# We have to run both the above commands in the same cell, otherwise it won't␣
↪give the same output
[120]: arr
[121]: ranar
[131]: #reshape
arr.reshape(5,5) #Changes not permanent
[123]: arr
[124]: ranar
[125]: ranar.max()
4
[125]: 41
[126]: 4
[127]: ranar.min()
[127]: 0
[128]: 9
[133]: arr.shape
[133]: (25,)
[129]: arr1.shape
[129]: (5, 5)
[130]: ranar.shape
[130]: (10,)
5
02_Numpy_Indexing_Selection (1)
March 5, 2024
[4]: arr[4]
[4]: 8
[5]: arr[:7]
0.1 Broadcasting
[6]: arr[0:5] = 100
arr
[6]: array([100, 100, 100, 100, 100, 10, 12, 14, 16, 18, 20])
[8]: slice_of_arr[:] = 99
slice_of_arr
[9]: array([99, 99, 99, 99, 99, 10, 12, 14, 16, 18, 20])
1
0.1.1 In Broadcasting, data is not copied. Its a view of original array. This avoids
memory problems
[10]: array([99, 99, 99, 99, 99, 10, 12, 14, 16, 18, 20])
arr_2d
[12]: arr_2d[1]
[13]: arr_2d[1][1]
[13]: 25
[14]: arr_2d[2][2]
[14]: 45
[15]: arr_2d[2][-1]
[15]: 45
[16]: arr_2d[-1][-1]
[16]: 45
[17]: arr_2d[-1,-1]
[17]: 45
[19]: arr_2d[1:,:2]
2
[19]: array([[20, 25],
[35, 40]])
[23]: arr[bool_arr]
[25]: x = 10/5
arr[arr>x]
3
03_Numpy_Operations
March 5, 2024
1 Arithmetic
[1]: import numpy as np
[3]: arr+arr
[4]: arr*arr
[5]: arr-arr
[6]: arr/arr
C:\Users\shrey\AppData\Local\Temp\ipykernel_20396\1862401812.py:1:
RuntimeWarning: invalid value encountered in divide
arr/arr
[6]: array([nan, 1., 1., 1., 1., 1., 1., 1., 1., 1.])
[7]: 1/arr
C:\Users\shrey\AppData\Local\Temp\ipykernel_20396\255282349.py:1:
RuntimeWarning: divide by zero encountered in divide
1/arr
[8]: arr**0.5
1
[8]: array([0. , 1. , 1.41421356, 1.73205081, 2. ,
2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
[10]: np.exp(arr)
[11]: np.cos(arr)
[12]: np.log(arr)
C:\Users\shrey\AppData\Local\Temp\ipykernel_20396\3120950136.py:1:
RuntimeWarning: divide by zero encountered in log
np.log(arr)
[16]: arr
[17]: arr.sum()
[17]: 45
[18]: arr.mean()
[18]: 4.5
[19]: arr.max()
[19]: 9
[20]: arr.min()
2
[20]: 0
[21]: arr.var()
[21]: 8.25
[22]: arr.std()
[22]: 2.8722813232690143
[24]: arr_2d.sum(axis = 0 )
[25]: arr_2d.sum(axis = 1 )
3
4_Exercises
March 5, 2024
[4]: arr.sum(axis=1)