Numpy - Basics - Jupyter Notebook
Numpy - Basics - Jupyter Notebook
Numpy - Basics - Jupyter Notebook
Computer Vision
Fall 2022
¶
This notebook introduces basic of NumPy.
NumPy
NumPy is an open-source numerical Python library.
NumPy contains a multi-dimensional array and matrix data structures.
It can be utilised to perform a number of mathematical operations on arrays such as trigonometric, statistical, and algebraic routines.
Numpy also contains random number generators.
NumPy is a wrapper around a library implemented in C.
localhost:8888/notebooks/Numpy_basics.ipynb 1/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
arr:
[[0. 0.]
[0. 0.]]
No. of dimensions: 2
Shape of array: (2, 2)
Size of array: 4
[[7. 7. 7.]
[7. 7. 7.]
[7. 7. 7.]]
float64
[[5. 5.]
[5. 5.]]
[[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.]]
In [ ]: random_arr = np.random.random((3,3)) # Create an array filled with random values ??? difference with randn and rand??
print(random_arr)
localhost:8888/notebooks/Numpy_basics.ipynb 2/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
In [ ]: x = np.array([[1,2,3],
[3,4,8]])
y = np.array([[5,6,1],
[7,8,1]])
print(x + y) # Elementwise sum; both produce the array
print(np.add(x, y))
print(x - y) # Elementwise difference; both produce the array
print(np.subtract(x, y))
print(x * y) # Elementwise product; both produce the array
print(np.multiply(x, y))
print(x / y) # Elementwise division; both produce the array
print(np.divide(x, y))
print(np.sqrt(x)) # Elementwise square root; produces the array
[[ 6 8 4]
[10 12 9]]
[[ 6 8 4]
[10 12 9]]
[[-4 -4 2]
[-4 -4 7]]
[[-4 -4 2]
[-4 -4 7]]
[[ 5 12 3]
[21 32 8]]
[[ 5 12 3]
[21 32 8]]
[[0.2 0.33333333 3. ]
[0.42857143 0.5 8. ]]
[[0.2 0.33333333 3. ]
[0.42857143 0.5 8. ]]
[[1. 1.41421356 1.73205081]
[1.73205081 2. 2.82842712]]
Stack numpy arrays along the horizontal and vertical axis respectively
hstack: [1 2 3 4 5 6] (6,)
vstack: [[1 2 3]
[4 5 6]] (2, 3)
In [9]: vec_vstack
In [ ]: matrix_a = np.array([[1,2,3],
[4,5,6]])
matrix_b = np.array([[11,12,13],
[14,15,16]])
matrix_vstack = np.vstack((matrix_a,matrix_b)) # vertically stack 1-d arrays
print("vstack: ",matrix_vstack, matrix_vstack.shape)
matrix_a = np.array([[1,2,3],[4,5,6]])
matrix_b = np.array([[11,12,13],[14,15,16]])
matrix_hstack = np.hstack((mat_a,mat_b)) # horizontally stack 1-d arrays
print("hstack: ",matrix_hstack, matrix_hstack.shape)
vstack: [[ 1 2 3]
[ 4 5 6]
[11 12 13]
[14 15 16]] (4, 3)
hstack: [[ 1 2 3 11 12 13]
[ 4 5 6 14 15 16]] (2, 6)
localhost:8888/notebooks/Numpy_basics.ipynb 3/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
In [ ]:
[1 2 3 4 5 6]
In [ ]: matrix_a = np.array([[1,2,3],
[4,5,6]])
matrix_b = np.array([[11,12,13],
[14,15,16]])
concat_ab = np.concatenate((matrix_a,matrix_b),axis=0)
print(concat_ab)
concat_ab = np.concatenate((matrix_a,matrix_b),axis=1)
print(concat_ab)
[[ 1 2 3]
[ 4 5 6]
[11 12 13]
[14 15 16]]
[[ 1 2 3 11 12 13]
[ 4 5 6 14 15 16]]
In [ ]: rand_normal = np.random.normal() # random number with normal distribution has zero mean and std div = 1
print(rand_normal) # how to sepecify the diferent range of number ????
0.2254003292825066
In [23]: mu = 0.5
sigma = 0.5
rand_normal_arr1 = np.random.normal(mu, sigma, size=5) # generate random numbers with mean 0.5 and sigma 1
print(rand_normal_arr1)
[0.20530556 0.16074971 0.08628646 0.22412307 0.45037746]
In [ ]: int_rand_arr = np.random.randint(low=2, high=300, size=4) # generate integer random numbers between 1 and 100
print(int_rand_arr)
[33 79 29 80]
In [24]: random_arr = np.random.randint(5, size = (3, 2)) # numbers are smaller than 5
print(random_arr)
[[4 4]
[3 3]
[2 1]]
localhost:8888/notebooks/Numpy_basics.ipynb 4/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
transposed_arr:
[[4 3 2]
[4 3 1]]
transposed arr shape: (2, 3)
flattended_arr:
[4 3 2 4 3 1]
(1, 3, 2)
(3, 2)
[[ 3 11]
[ 6 0]
[10 10]]
10
In [40]: print(random_arr[2]) #extracting all elements from first axis at 2nd location
[10 10]
[[ 3 11]]
In [ ]: print(random_arr[:,1])
[2 0 8]
𝒆1 = ( 1 0 0 )
𝒆2 = ( 0 1 0 )
𝒆3 = ( 0 0 1 )
localhost:8888/notebooks/Numpy_basics.ipynb 5/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
In [ ]: e1 = np.array([1, 0, 0])
e2 = np.array([0, 1, 0])
e3 = np.array([0, 0, 1])
print("e1 =", e1)
print("e2 =", e2)
print("e3 =", e3)
e1 = [1 0 0]
e2 = [0 1 0]
e3 = [0 0 1]
e1.e2 = 0
e2.e2 = 1
Scalar-vector multiplication
𝑘𝒗 = 3 ( 1 3 2 ) = ( 3 9 6 )
In [ ]: k = 3
v = np.array([1, 3, 2])
print("k*v =", k*v)
k*v = [3 9 6]
Consider 𝑤1 = 3, 𝑤2 = 1, and 𝑤3 = 4
𝒘 = 𝑤1 𝒆1 + 𝑤2 𝒆2 + 𝑤3 𝒆3
= 3(0 0 1) + 1(1 0 0) + 4(0 1 0)
= (3 0 0) + (0 1 0) + (0 0 4)
= (3 1 4)
In [ ]: w1 = 3
w2 = 1
w3 = 4
w = w1*e1 + w2*e2 + w3*e3
print("w = w1*e1 + w2*e2 + w3*e3 =", w)
In [ ]: u = np.array([1, 2, 0])
v = np.array([2, 4, 1])
result = np.dot(u,v)
print(result)
10
3
𝒖.𝒗 = ∑ 𝑢𝑖 𝑣𝑖
𝑖=1
In [ ]: result = 0
for i in range(len(u)):
result += u[i]*v[i]
print(result)
10
(𝑨𝑩)𝑇 = 𝑩𝑇 𝑨𝑇
localhost:8888/notebooks/Numpy_basics.ipynb 6/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
In [ ]: A = np.random.randint(4, size=(3,3))
B = np.random.randint(4, size=(3,3))
result = np.dot(A, B).T
print("A =\n", A)
print("B =\n", B)
print("(AB)^T =\n", result)
A =
[[0 3 3]
[2 2 1]
[1 2 2]]
B =
[[3 1 3]
[0 2 0]
[1 3 2]]
(AB)^T =
[[ 3 7 5]
[15 9 11]
[ 6 8 7]]
M =
[[3 0 4]
[1 1 1]
[3 4 4]]
u =
[[1]
[2]
[3]]
Mu =
[[15]
[ 6]
[23]]
In [ ]: matrix = np.ones((5,5))
print(matrix)
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
localhost:8888/notebooks/Numpy_basics.ipynb 7/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
[[0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0. 0.]]
a1.shape: (3, 4)
a2.shape: (2, 4)
A.shape: (3,)
A =
[array([[3, 1, 4, 1],
[5, 9, 2, 6],
[5, 3, 5, 8]])
array([[9, 7, 9, 3],
[2, 3, 8, 4]])
array([[3, 3, 8, 3],
[2, 7, 9, 5],
[0, 2, 8, 8],
[6, 2, 6, 4]])]
A1 = np.array(A1)
print("A =\n", A)
print("A1 =\n", A1)
[array([[3, 1, 4, 1],
[5, 9, 2, 6],
[5, 3, 5, 8]])
array([[9, 7, 9, 3],
[2, 3, 8, 4]])
array([[3, 3, 8, 3],
[2, 7, 9, 5],
[0, 2, 8, 8],
[6, 2, 6, 4]])] (3,)
A =
[array([[3, 1, 4, 1],
[5, 9, 2, 6],
[5, 3, 5, 8]])
array([[9, 7, 9, 3],
[2, 3, 8, 4]])
array([[3, 3, 8, 3],
[2, 7, 9, 5],
[0, 2, 8, 8],
[6, 2, 6, 4]])]
A1 =
[[[3 1 4 1]
[5 9 2 6]]
[[9 7 9 3]
[2 3 8 4]]
[[3 3 8 3]
[2 7 9 5]]]
localhost:8888/notebooks/Numpy_basics.ipynb 8/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook
[((0, 1), (0, 0)), ((0, 2), (0, 0)), ((0, 0), (0, 0))]
A =
[array([[3, 1, 4, 1],
[5, 9, 2, 6],
[5, 3, 5, 8]])
array([[9, 7, 9, 3],
[2, 3, 8, 4]])
array([[3, 3, 8, 3],
[2, 7, 9, 5],
[0, 2, 8, 8],
[6, 2, 6, 4]])]
A3 =
[[[3 1 4 1]
[5 9 2 6]
[5 3 5 8]
[0 0 0 0]]
[[9 7 9 3]
[2 3 8 4]
[0 0 0 0]
[0 0 0 0]]
[[3 3 8 3]
[2 7 9 5]
[0 2 8 8]
[6 2 6 4]]]
localhost:8888/notebooks/Numpy_basics.ipynb 9/9