03 Numpy

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

01-Numpy-Arrays (3)

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

[100]: lst = [1,2,3]


arr = np.array(lst)

[101]: type(lst)

[101]: list

[102]: type(arr)

[102]: numpy.ndarray

[103]: mat = [[1,2,3],[4,5,6],[7,8,9]]


mat

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

[104]: np.array(mat)

[104]: array([[1, 2, 3],


[4, 5, 6],
[7, 8, 9]])

1
1.2 Built-in Methods to create arrays
[105]: #arange: return evenly spaces values within a given interval

np.arange(0,10)

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

[106]: np.arange(0,11,3) # here the last column denotes the step size

[106]: array([0, 3, 6, 9])

1.3 Zeros and Ones : Gives 0 ans 1 matrix


[107]: np.zeros(4)

[107]: array([0., 0., 0., 0.])

[108]: np.zeros((3,4))

[108]: array([[0., 0., 0., 0.],


[0., 0., 0., 0.],
[0., 0., 0., 0.]])

[109]: np.ones((4,2))

[109]: array([[1., 1.],


[1., 1.],
[1., 1.],
[1., 1.]])

1.4 Linspace : Returns equally spaced values


[110]: np.linspace(0,15,5) # here the last column denotes how many numbers you want

[110]: array([ 0. , 3.75, 7.5 , 11.25, 15. ])

[111]: #Eye: gives Identity Matrix

np.eye(5)

[111]: array([[1., 0., 0., 0., 0.],


[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])

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)

[112]: array([0.89460666, 0.08504421, 0.03905478, 0.16983042, 0.8781425 ,


0.09834683, 0.42110763, 0.95788953, 0.53316528])

[113]: np.random.rand(3,4)

[113]: array([[0.69187711, 0.31551563, 0.68650093, 0.83462567],


[0.01828828, 0.75014431, 0.98886109, 0.74816565],
[0.28044399, 0.78927933, 0.10322601, 0.44789353]])

[114]: #randn: return a sample from the standard normal distribution i.e numbers will␣
↪be distributed around 0

np.random.randn(10)

[114]: array([-0.26788808, 0.53035547, -0.69166075, -0.39675353, -0.6871727 ,


-0.84520564, -0.67124613, -0.0126646 , -1.11731035, 0.2344157 ])

[115]: np.random.randn(5,4)

[115]: array([[ 1.65980218, 0.74204416, -0.19183555, -0.88762896],


[-0.74715829, 1.6924546 , 0.05080775, -0.63699565],
[ 0.19091548, 2.10025514, 0.12015895, 0.61720311],
[ 0.30017032, -0.35224985, -1.1425182 , -0.34934272],
[-0.20889423, 0.58662319, 0.83898341, 0.93110208]])

[116]: #randint: return integers from low(inclusive) to high(exclusive)


np.random.randint(1,10)

[116]: 8

[117]: np.random.randint(1,10,15) #excluding 10 here #here the last column denotes␣


↪how many numbers you want

## Also, everytime we run this command, different set of random numbers will be␣
↪generated, so if we want that everytime the same

# set of numbers to be generated then we can use 'Seed'

[117]: array([2, 8, 9, 5, 1, 2, 9, 3, 4, 2, 3, 8, 3, 7, 1])

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

np.random.randint(1,10,19) # here the last column denotes how many␣


↪numbers you want

# We have to run both the above commands in the same cell, otherwise it won't␣
↪give the same output

[118]: array([6, 9, 6, 1, 1, 2, 8, 7, 3, 5, 6, 3, 5, 3, 5, 8, 8, 2, 8])

2.2 Array attributes and Methods


[119]: arr = np.arange(25)
ranar = np.random.randint(0,50,10)

[120]: arr

[120]: array([ 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])

[121]: ranar

[121]: array([30, 32, 22, 13, 41, 9, 7, 22, 1, 0])

[131]: #reshape
arr.reshape(5,5) #Changes not permanent

[131]: array([[ 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]])

[132]: arr1 = arr.reshape(5,5)

[123]: arr

[123]: array([ 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])

[124]: ranar

[124]: array([30, 32, 22, 13, 41, 9, 7, 22, 1, 0])

[125]: ranar.max()

4
[125]: 41

[126]: ranar.argmax() #gives at which index the value is maximum

[126]: 4

[127]: ranar.min()

[127]: 0

[128]: ranar.argmin() #gives at which index the value is minimum

[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

[2]: import numpy as np

[3]: arr = np.arange(0,21,2)


arr

[3]: array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])

[4]: arr[4]

[4]: 8

[5]: arr[:7]

[5]: array([ 0, 2, 4, 6, 8, 10, 12])

0.1 Broadcasting
[6]: arr[0:5] = 100
arr

[6]: array([100, 100, 100, 100, 100, 10, 12, 14, 16, 18, 20])

[7]: slice_of_arr = arr[0:5]


slice_of_arr

[7]: array([100, 100, 100, 100, 100])

[8]: slice_of_arr[:] = 99
slice_of_arr

[8]: array([99, 99, 99, 99, 99])

[9]: arr #changes reflects in original array

[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]: arr_copy = arr.copy()


arr_copy

[10]: array([99, 99, 99, 99, 99, 10, 12, 14, 16, 18, 20])

0.2 Indexing a 2D array (Matrices)


[11]: arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45])) #since there are two␣
↪brackets after np.array, it means this is a 2D array!

arr_2d

[11]: array([[ 5, 10, 15],


[20, 25, 30],
[35, 40, 45]])

[12]: arr_2d[1]

[12]: array([20, 25, 30])

[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

[27]: arr_2d[0:2,1:] #arr[rows,columns]

[27]: array([[10, 15],


[25, 30]])

[19]: arr_2d[1:,:2]

2
[19]: array([[20, 25],
[35, 40]])

0.3 Conditional selection


[20]: arr = np.arange(1,11)
arr

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

[21]: bool_arr = arr > 4

[22]: arr[bool_arr == False]

[22]: array([1, 2, 3, 4])

[23]: arr[bool_arr]

[23]: array([ 5, 6, 7, 8, 9, 10])

[24]: arr[arr > 7]

[24]: array([ 8, 9, 10])

[25]: x = 10/5
arr[arr>x]

[25]: array([ 3, 4, 5, 6, 7, 8, 9, 10])

3
03_Numpy_Operations

March 5, 2024

1 Arithmetic
[1]: import numpy as np

[2]: arr = np.arange(0,10)


arr

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

[3]: arr+arr

[3]: array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

[4]: arr*arr

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

[5]: arr-arr

[5]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

[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

[7]: array([ inf, 1. , 0.5 , 0.33333333, 0.25 ,


0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111])

[8]: arr**0.5

1
[8]: array([0. , 1. , 1.41421356, 1.73205081, 2. ,
2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])

1.1 Universal array functions


[9]: np.sqrt(arr)

[9]: array([0. , 1. , 1.41421356, 1.73205081, 2. ,


2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])

[10]: np.exp(arr)

[10]: array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,


5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
2.98095799e+03, 8.10308393e+03])

[11]: np.cos(arr)

[11]: array([ 1. , 0.54030231, -0.41614684, -0.9899925 , -0.65364362,


0.28366219, 0.96017029, 0.75390225, -0.14550003, -0.91113026])

[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)

[12]: array([ -inf, 0. , 0.69314718, 1.09861229, 1.38629436,


1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458])

1.1.1 Summary statistics

[16]: arr

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

[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

1.2 Axis Logic


• axis 0 denotes the vertical axis
• axis 1 denotes the horizaontal axis

[23]: arr_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])


arr_2d

[23]: array([[ 1, 2, 3, 4],


[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])

[24]: arr_2d.sum(axis = 0 )

[24]: array([15, 18, 21, 24])

[25]: arr_2d.sum(axis = 1 )

[25]: array([10, 26, 42])

3
4_Exercises

March 5, 2024

[1]: import numpy as np

[2]: #Create an array of 20 lineraly spaced points between 0 and 1


np.linspace(0,1,20)

[2]: array([0. , 0.05263158, 0.10526316, 0.15789474, 0.21052632,


0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,
0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,
0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ])

[3]: #reshape above array in 4*5 dimension


arr = np.linspace(0,1,20).reshape(4,5)
arr

[3]: array([[0. , 0.05263158, 0.10526316, 0.15789474, 0.21052632],


[0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421],
[0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211],
[0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ]])

[4]: arr.sum(axis=1)

[4]: array([0.52631579, 1.84210526, 3.15789474, 4.47368421])

[5]: #left bottom 3*3


arr[1:,:3]

[5]: array([[0.26315789, 0.31578947, 0.36842105],


[0.52631579, 0.57894737, 0.63157895],
[0.78947368, 0.84210526, 0.89473684]])

You might also like