Numpy is linear algebra library for python . this is importent library because all pydata ecosystem rely on numpy.
import numpy as np
arr = [1,2,3]
arr
[1, 2, 3]
np.array(arr)
array([1, 2, 3])
my_mat = [[1,2,3],[4,5,6],[7,8,9]]
my_mat
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
np.array(my_mat)
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
my_mat[0][1]
2
# np.arange(start , last , offset)
np.arange(0,10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,5,1)
array([0, 1, 2, 3, 4])
np.arange(2,12,2)
array([ 2, 4, 6, 8, 10])
# Zeros
np.zeros(3) # one dimensional
array([0., 0., 0.])
np.zeros((3,3)) # two dimensional
array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
# one's
np.ones(5)
array([1., 1., 1., 1., 1.])
np.ones((2,2))
array([[1., 1.], [1., 1.]])
# linspace -> (start , end , how many point u want in between start to end)
np.linspace(0,5,10)
array([0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222, 2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])
# Diagonal element
np.eye(3)
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
# random -> (number of points in between )
np.random.rand(5)
array([[0.93087129, 0.30534035, 0.2508104 ], [0.05900399, 0.39901502, 0.00206355], [0.10066378, 0.67083509, 0.95297132]])
np.random.rand(3,3)
array([[0.29917792, 0.88770559, 0.0923786 ], [0.33748283, 0.56245829, 0.88557374], [0.88111555, 0.40227034, 0.28943298]])
# random based on standard normal distrubution
np.random.randn(3)
array([ 0.65926802, -0.18545583, -0.5672595 ])
# random number between start to end anything in interger format
np.random.randint(1,20)
8
# start , end , numbers of points in between
np.random.randint(1,20,5)
array([18, 12, 16, 3, 4])
# reshape
# we can reshape the arr into 2 dimesional
# we can't resize 10 into 2 row and 3 column or anything we have to reshape into row 2 and column 5
array = np.arange(10)
array.reshape(2,5)
array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
number = np.random.randint(1,20,5);
print(number.max())
print(number.min())
print(number.argmax()) # return location where max is stored
print(number.argmin()) # return location where min is stored
17 2 0 2
array.shape #because its one dimesional
(10,)
# type
array.dtype
dtype('int32')
# instead of writing like these -> np.random.randint(1,20)
from numpy.random import randint
# then we can directly call randint which will return random number in between 3 to 10
randint(3,10)
3
arr_1 = np.arange(1,20)
arr_1
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
arr_1 + arr_1
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38])
arr_1 * arr_1
array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361])
arr_1 - arr_1
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
arr_1 / arr_1
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
arr_1*10
array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190])
arr_1**2
array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361], dtype=int32)
np.square(arr_1)
array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361], dtype=int32)
np.max(arr_1)
19
np.sqrt(arr_1)
array([1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. , 3.16227766, 3.31662479, 3.46410162, 3.60555128, 3.74165739, 3.87298335, 4. , 4.12310563, 4.24264069, 4.35889894])
np.exp(arr_1)
array([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, 2.20264658e+04, 5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06, 3.26901737e+06, 8.88611052e+06, 2.41549528e+07, 6.56599691e+07, 1.78482301e+08])
# mathematical operator
np.sin(arr_1)
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849, -0.54402111, -0.99999021, -0.53657292, 0.42016704, 0.99060736, 0.65028784, -0.28790332, -0.96139749, -0.75098725, 0.14987721])
np.log(arr_1)
array([0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509, 2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 , 2.77258872, 2.83321334, 2.89037176, 2.94443898])
arr_2d = np.array([+.
[5,10,15],[20,23,45],[78,54,12]])
arr_2d
array([[ 5, 10, 15], [20, 23, 45], [78, 54, 12]])
arr_2d[0][1]
10
#Grab Everything from row 2 which is starting from column 1 to onwords
arr_2d[:2,1:]
array([[10, 15], [23, 45]])
arr_2d[1,1:]
array([23, 45])
boolean = arr_2d > 30
boolean
array([[False, False, False], [False, False, True], [ True, True, False]])
arr_2d[boolean]
array([45, 78, 54])
arr_2d[arr_2d>20]
array([23, 45, 78, 54])