0% found this document useful (0 votes)
177 views

Numpy - Basics - Jupyter Notebook

This document introduces basics of the NumPy library in Python. It discusses NumPy arrays, including generating random numbers, basic mathematical operations, stacking and concatenating arrays, and changing array dimensions. NumPy is an open-source numerical library that contains multi-dimensional array and matrix data structures for efficient mathematical and statistical operations in Python.

Uploaded by

huzaifa khawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Numpy - Basics - Jupyter Notebook

This document introduces basics of the NumPy library in Python. It discusses NumPy arrays, including generating random numbers, basic mathematical operations, stacking and concatenating arrays, and changing array dimensions. NumPy is an open-source numerical library that contains multi-dimensional array and matrix data structures for efficient mathematical and statistical operations in Python.

Uploaded by

huzaifa khawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook

Computer Vision
Fall 2022

This notebook introduces basic of NumPy.

1. Bascis of NumPy arrays


2. Generate random numbers with NumPy
3. Change dimensions of a NumPy array
4. Indexing and slicing of NumPy arrays
5. Dot product of NumPy arrays
6. Padding in NumPy arrays

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.

Check the version of NumPy

In [50]: import numpy as np



np.version.version
Out[50]: '1.15.1'

Basics of Numpy arrays

localhost:8888/notebooks/Numpy_basics.ipynb 1/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook

In [3]: arr = np.array([7,2,9,10])



# Printing type of arr object
print("Array is of type: ", type(arr))

# Printing array dimensions (axes)


print("No. of dimensions: ", arr.ndim)

# Printing shape of array


print("Shape of array: ", arr.shape)

# Printing size (total number of elements) of array


print("Size of array: ", arr.size)

# Printing type of elements in array


print("Array stores elements of type: ", arr.dtype)

Array is of type: <class 'numpy.ndarray'>


No. of dimensions: 1
Shape of array: (4,)
Size of array: 4
Array stores elements of type: int32

In [4]: arr = np.array([[1,2,3],


[2,9,10],
[4,6,8]])

print("Array is of type: ", type(arr))
print("No. of dimensions: ", arr.ndim)
print("Shape of array: ", arr.shape)
print("Size of array: ", arr.size)
print("Array stores elements of type: ", arr.dtype)

Array is of type: <class 'numpy.ndarray'>


No. of dimensions: 2
Shape of array: (3, 3)
Size of array: 9
Array stores elements of type: int32

In [5]: arr_of_zeros = np.zeros((2,2)) # Create an array of all zeros


print("arr:\n", arr_of_zeros)
print("No. of dimensions: ", arr_of_zeros.ndim)
print("Shape of array: ", arr_of_zeros.shape)
print("Size of array: ", arr_of_zeros.size)

arr:
[[0. 0.]
[0. 0.]]
No. of dimensions: 2
Shape of array: (2, 2)
Size of array: 4

In [7]: arr_of_ones = np.ones((3,3))*7 # Create an array of all ones


print(arr_of_ones)
print(arr_of_ones.dtype)

[[7. 7. 7.]
[7. 7. 7.]
[7. 7. 7.]]
float64

In [21]: arr_constant = np.full((2,2), 5) # Create a constant array


#arr_constant_f = arr_constant.astype(float)
#print(arr_constant_f)

[[5. 5.]
[5. 5.]]

In [7]: identity_matrix = np.eye(5) # Create a 3x3 of identity matrix


print(identity_matrix)

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

[[0.83993864 0.90486946 0.07592118]


[0.74731677 0.02290747 0.05058199]
[0.24667761 0.30200453 0.17171275]]

localhost:8888/notebooks/Numpy_basics.ipynb 2/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook

Basic mathematical functions in numpy

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

In [8]: vec_a = np.array([1,2,3])


vec_b = np.array([4,5,6])


vec_hstack = np.hstack((vec_a,vec_b)) # horizontally stack 1-d arrays
vec_vstack = np.vstack((vec_a,vec_b)) # vertically stack 1-d arrays

print("hstack: ", vec_hstack, vec_hstack.shape)
print("vstack: ", vec_vstack, vec_vstack.shape)

hstack: [1 2 3 4 5 6] (6,)
vstack: [[1 2 3]
[4 5 6]] (2, 3)

In [9]: vec_vstack

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

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 [ ]: ​

In [10]: arr_a = np.array([1,2,3])


arr_b = np.array([4,5,6])

In [16]: concat_ab = np.concatenate((arr_a,arr_b),axis=0) # horizontally concatenate


print(concat_ab)

[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 [13]: import random

2 Generate random numbers with NumPy

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 [ ]: rand_normal_arr = np.random.normal(size=(4,4)) # random numbers from normal distribution


print(rand_normal_arr)
[[-0.41422068 -1.08040137 0.10653205 0.75061585]
[ 1.60802705 1.99937067 0.71877369 -1.10872117]
[ 3.06026462 -0.02816034 1.69340412 0.96417534]
[ 0.28913056 1.18347862 1.50966666 -0.25539689]]

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 [ ]: rand_uniform = np.random.uniform(size=4) # random numbers from uniform distribution


print(rand_uniform)
[0.04371106 0.34042549 0.43609134 0.91523555]

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]]

3 Change dimensions of a NumPy array

localhost:8888/notebooks/Numpy_basics.ipynb 4/9
12/14/22, 6:42 AM Numpy_basics - Jupyter Notebook

In [26]: transposed_arr = np.transpose(random_arr) # transpose the array


print("transposed_arr:\n", transposed_arr)
print("transposed arr shape:", transposed_arr.shape)

transposed_arr:
[[4 3 2]
[4 3 1]]
transposed arr shape: (2, 3)

In [27]: reshaped_arr = np.reshape(transposed_arr, (3,2)) # reshape the array


print("reshaped_arr:\n", reshaped_arr)
reshaped_arr:
[[4 3]
[2 4]
[3 1]]

In [28]: flattened_arr = reshaped_arr.flatten() # flatten the array


print("flattended_arr:\n", flattened_arr)

flattended_arr:
[4 3 2 4 3 1]

In [29]: new_arr = reshaped_arr[np.newaxis,:,:] # add new dimension in the array ???


print(new_arr.shape)

(1, 3, 2)

In [30]: squeezed_arr = np.squeeze(new_arr) # squeezed the array , it reduce 1 dimension of an array


print(squeezed_arr.shape)

(3, 2)

4 Indexing and slicing of NumPy arrays

In [37]: random_arr = np.random.randint(0, 15, size=(3,2))


print(random_arr)

[[ 3 11]
[ 6 0]
[10 10]]

In [38]: print(random_arr[0,0]) # access specific element

In [39]: print(random_arr[2,1]) # access specific element

10

In [40]: print(random_arr[2]) #extracting all elements from first axis at 2nd location

[10 10]

In [41]: print(random_arr[:1])# display zeroth row only (display 0 to 1-1 row)

[[ 3 11]]

In [ ]: print(random_arr[:,1])

[2 0 8]

5 Dot prodcut of NumPy arrays

Consider three vectors arrays

𝒆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]

The dot product of two vector arrays can be defined as

In [ ]: print("e1.e2 =", np.dot(e1, e2))


print("e2.e2 =", np.dot(e2, e2))

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)

w = w1*e1 + w2*e2 + w3*e3 = [3 1 4]

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]]

In [ ]: result = np.dot(B.T, A.T)


print("A^T.B^T =\n", result)
A^T.B^T =
[[ 3 7 5]
[15 9 11]
[ 6 8 7]]

3 × 3 matrix array and a 3 × 1 vector arrays:


 𝑀11 𝑀12 𝑀13   𝑢1   𝑀11 𝑢1 + 𝑀12 𝑢2 + 𝑀13 𝑢3 
Here is an example of matrix array multiplication between a

𝑴𝒖 =  𝑀21 𝑀22 𝑀23   𝑢2  =  𝑀21 𝑢1 + 𝑀22 𝑢2 + 𝑀23 𝑢3 


 𝑀31 𝑀32 𝑀33   𝑢3   𝑀31 𝑢1 + 𝑀32 𝑢2 + 𝑀33 𝑢3 
In [ ]: M = np.random.randint(5, size=(3,3))
u = np.array([[1],
[2],
[3]])

result = np.matmul(M, u)

print("M =\n", M)

print("u =\n", u)

print("Mu =\n", result)

M =
[[3 0 4]
[1 1 1]
[3 4 4]]
u =
[[1]
[2]
[3]]
Mu =
[[15]
[ 6]
[23]]

7 Padding in Numpy arrays

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

In [ ]: pad_matrix = np.pad(matrix, pad_width=1, mode='constant', constant_values=0)


print(pad_matrix)

[[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.]]

In [46]: a1 = np.array([np.array([3, 1, 4, 1]),


np.array([5, 9, 2, 6]),
np.array([5, 3, 5, 8])])

a2 = np.array([np.array([9, 7, 9, 3]),
np.array([2, 3, 8, 4])])

a3 = np.array([np.array([3, 3, 8, 3]),
np.array([2, 7, 9, 5]),
np.array([0, 2, 8, 8]),
np.array([6, 2, 6, 4])])

print("a1.shape:",a1.shape)
print("a2.shape:",a2.shape)

A = np.array([a1, a2, a3])
print("A.shape:",A.shape)

print("A =\n", A.T)

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

Subset the first two elements to make a tensor array

In [ ]: A1 = [record[:] for record in A]


print(A,A.shape)

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

Subset the last two elements to make a tensor array

In [ ]: A2 = [record[-2:] for record in A]


A2 = np.array(A2)

print("A =\n", A)

print("A2 =\n", A2)

In [ ]: pad_below1 = 4 - len(a1) # 4-3=1 len(a1) = 3x4


pad_below2 = 4 - len(a2) # 4-2=2 len(a2) = 2x4
pad_below3 = 4 - len(a3) # 4-4=0 len(a3) = 4x4

pad_above = 0
pad_left = 0
par_right = 0

n_add = [((pad_above, pad_below1), (pad_left, par_right)), # one tuple for each dimension
((pad_above, pad_below2), (pad_left, par_right)),
((pad_above, pad_below3), (pad_left, par_right))]
print(n_add)

A3 = [np.pad(A[i], pad_width = n_add[i], mode = 'constant', constant_values = 0) for i in range(3)] # list comprehension returns
A3 = np.array(A3) # convert A3 into array again

print("A =\n", A)
print("A3 =\n", A3)

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

You might also like