Lesson 1

Part 1: Printing & Accessing to the elements

In [4]:
import sys
import numpy as np

a = [4,3,2,5,1,9,4,1,3]  # this is a Python list
b = [1,1,3,2,7,6,1,1,1]  # this is a Python list
A = np.array(a)          # this is a Numpy array
B = np.array(b)          # this is a Numpy array

print(a)
print()
print(A)
print()
print(A[3:5])
print()
print(A[:5])
print()
print(A[3:])
print()
print(A[-4:-2])
print()
print(A[:-2])
print()
print(A[-4:])
print()
print(A[0])
print()
print(A[-1])
print()
c = A[::-1]
print(c)
[4, 3, 2, 5, 1, 9, 4, 1, 3]

[4 3 2 5 1 9 4 1 3]

[5 1]

[4 3 2 5 1]

[5 1 9 4 1 3]

[9 4]

[4 3 2 5 1 9 4]

[9 4 1 3]

4

3

[3 1 4 9 1 5 2 3 4]

Part 2: Numpy arrays & loops

In [6]:
a = [4,3,2,5,1,9,4,1,3]  # this is a Python list
A = np.array(a)          # this is a Numpy array
print("1st way")
n = len(A)
for i in range(n):
    print(A[i], end = " ")
print()
print()
print("2nd way")
for x in A:
    print(x, end = " ")
print()
1st way
4 3 2 5 1 9 4 1 3

2nd way
4 3 2 5 1 9 4 1 3

Part 3: Numpy arrays & if else

In [7]:
a = [4,3,2,5,1,9,4,1,3]
A = np.array(a)

if 2 in A:
    print("2 exists in A")
else:
    print("2 doesnt exists in A")
2 exists in A

Part 4: Numpy arrays & functions

In [8]:
a = [4,3,2,5,1,9,4,1,3]
A = np.array(a)

c = A.sum()
print(c)
print()
c = A.min()
print(c)
print()
c = A.max()
print(c)
print()
c = A.mean()
print(c)
32

1

9

3.5555555555555554

Part 5: Adding & removing elements

In [9]:
a = [4,3,2,5,1,9,4,1,3]
A = np.array(a)

c = A.copy()
print(c)
print()
c = np.append(c,7)         # append at the end
print(c)
print()
c = np.insert(c,2,100)     # insert 100 at the index 2
print(c)
print()
c = np.delete(c, 2)        # remove the element at index 2
print(c)
[4 3 2 5 1 9 4 1 3]

[4 3 2 5 1 9 4 1 3 7]

[  4   3 100   2   5   1   9   4   1   3   7]

[4 3 2 5 1 9 4 1 3 7]

Part 6: Combining arrays

In [10]:
a = [4,3,2,5,1,9,4,1,3]
b = [1,1,3,2,7,6,1,1,1]
A = np.array(a)
B = np.array(b)

c = a + b                            # concatenation of two lists
print(c)
print()
c = np.concatenate((A, B), axis = 0) # concatenation of two numpy arrays
print(c)
print()
c = np.hstack((A, B))                # horizontal stacking of two numpy arrays
print(c)
print()
c = np.vstack((A, B))                # vertical stacking of two numpy arrays
print(c)
[4, 3, 2, 5, 1, 9, 4, 1, 3, 1, 1, 3, 2, 7, 6, 1, 1, 1]

[4 3 2 5 1 9 4 1 3 1 1 3 2 7 6 1 1 1]

[4 3 2 5 1 9 4 1 3 1 1 3 2 7 6 1 1 1]

[[4 3 2 5 1 9 4 1 3]
 [1 1 3 2 7 6 1 1 1]]

Part 7: Mathematics

In [11]:
a = [4,3,2,5,1,9,4,1,3]
b = [1,1,3,2,7,6,1,1,1]
A = np.array(a)
B = np.array(b)

c = np.add(A,B)
# c = A + B
print(c)
print()
c = np.subtract(A,B)
# c = A - B
print(c)
print()
c = np.multiply(A,B)
# c = A @ B
print(c)
print()
c = np.divide(A,B)
# c = A / B
print(c)
print()
c = np.dot(A,B)
print(c)
print()
c = np.sin(A)
print(c)
print()
c = np.cos(A)
print(c)
print()
c = np.sqrt(A)
print(c)
print()
c = np.log(A)
print(c)
[ 5  4  5  7  8 15  5  2  4]

[ 3  2 -1  3 -6  3  3  0  2]

[ 4  3  6 10  7 54  4  1  3]

[4.         3.         0.66666667 2.5        0.14285714 1.5
 4.         1.         3.        ]

92

[-0.7568025   0.14112001  0.90929743 -0.95892427  0.84147098  0.41211849
 -0.7568025   0.84147098  0.14112001]

[-0.65364362 -0.9899925  -0.41614684  0.28366219  0.54030231 -0.91113026
 -0.65364362  0.54030231 -0.9899925 ]

[2.         1.73205081 1.41421356 2.23606798 1.         3.
 2.         1.         1.73205081]

[1.38629436 1.09861229 0.69314718 1.60943791 0.         2.19722458
 1.38629436 0.         1.09861229]

Lesson 2

Part 1: Printing & Accessing to the elements

In [14]:
import sys
import numpy as np

a = [[4,3,2],[5,1,9],[4,1,3]]  # this is a Python matrix
A = np.array(a)                # this is a Numpy matrix

print(a)
print()
print(A)
print()
print(A[0])
print()
print(A[-1])
print()
print(A[:1])
print()
print(A[1:])
print()
print(A[:-2])
print()
print(A[-1:])
print()
c = A[::-1]
print(c)
[[4, 3, 2], [5, 1, 9], [4, 1, 3]]

[[4 3 2]
 [5 1 9]
 [4 1 3]]

[4 3 2]

[4 1 3]

[[4 3 2]]

[[5 1 9]
 [4 1 3]]

[[4 3 2]]

[[4 1 3]]

[[4 1 3]
 [5 1 9]
 [4 3 2]]

Part 2: Numpy arrays & loops

In [16]:
a = [[4,3,2],[5,1,9],[4,1,3]]
A = np.array(a)

print("1st way")
n = len(A)
for i in range(n):
    print(A[i], end = " ")
print()
print()
print("2nd way")
for x in A:
    print(x, end = " ")
1st way
[4 3 2] [5 1 9] [4 1 3]

2nd way
[4 3 2] [5 1 9] [4 1 3] 

Part 3: Numpy arrays & if else

In [17]:
a = [[4,3,2],[5,1,9],[4,1,3]]
A = np.array(a)

if 2 in A:
    print("2 exists in A")
else:
    print("2 doesnt exists in A")
2 exists in A

Part 4: Numpy arrays & functions

In [18]:
a = [[4,3,2],[5,1,9],[4,1,3]]
A = np.array(a)

c = A.sum()
print(c)
print()
c = A.sum(axis=0)
print(c)
print()
c = A.sum(axis=1)
print(c)
print()
c = A.min()
print(c)
print()
c = A.min(axis=0)
print(c)
print()
c = A.min(axis=1)
print(c)
print()
c = A.max()
print(c)
print()
c = A.max(axis=0)
print(c)
print()
c = A.max(axis=1)
print(c)
print()
c = A.mean()
print(c)
print()
c = A.mean(axis=0)
print(c)
print()
c = A.mean(axis=1)
print(c)
32

[13  5 14]

[ 9 15  8]

1

[4 1 2]

[2 1 1]

9

[5 3 9]

[4 9 4]

3.5555555555555554

[4.33333333 1.66666667 4.66666667]

[3.         5.         2.66666667]

Part 5: Adding & removing elements

In [19]:
a = [[4,3,2],[5,1,9],[4,1,3]]
A = np.array(a)

c = np.insert(A,2,[100,100,100], axis = 0)     # insert 100 at the index 2 (axis 0)
print(c)
print()
c = np.insert(A,2,[100,100,100], axis = 1)     # insert 100 at the index 2 (axis 1)
print(c)
print()
c = np.delete(A, 2, axis = 0)                  # remove the element at index 2, axis 0
print(c)
print()
c = np.delete(A, 2, axis = 1)                  # remove the element at index 2, axis 1
print(c)
[[  4   3   2]
 [  5   1   9]
 [100 100 100]
 [  4   1   3]]

[[  4   3 100   2]
 [  5   1 100   9]
 [  4   1 100   3]]

[[4 3 2]
 [5 1 9]]

[[4 3]
 [5 1]
 [4 1]]

Part 6: Combining arrays

In [20]:
a = [[4,3,2],[5,1,9],[4,1,3]]
b = [[1,1,3],[2,7,6],[1,1,1]]
A = np.array(a)
B = np.array(b)

c = a + b                            # concatenation of two lists
print(c)
print()
c = np.concatenate((A, B), axis = 0) # concatenation of two numpy arrays
print(c)
print()
c = np.hstack((A, B))                # horizontal stacking of two numpy arrays
print(c)
print()
c = np.vstack((A, B))                # vertical stacking of two numpy arrays
print(c)
[[4, 3, 2], [5, 1, 9], [4, 1, 3], [1, 1, 3], [2, 7, 6], [1, 1, 1]]

[[4 3 2]
 [5 1 9]
 [4 1 3]
 [1 1 3]
 [2 7 6]
 [1 1 1]]

[[4 3 2 1 1 3]
 [5 1 9 2 7 6]
 [4 1 3 1 1 1]]

[[4 3 2]
 [5 1 9]
 [4 1 3]
 [1 1 3]
 [2 7 6]
 [1 1 1]]

Part 7: Mathematics (Part 1)

In [23]:
a = [[4,3,2],[5,1,9],[4,1,3]]
b = [[1,1,3],[2,7,6],[1,1,1]]
A = np.array(a)
B = np.array(b)

c = np.add(A,B)
# c = A + B
print(c)
print()
c = np.subtract(A,B)
# c = A - B
print(c)
print()
c = np.multiply(A,B)
# c = A @ B
print(c)
print()
c = np.divide(A,B)
# c = A / B
print(c)
print()
c = np.dot(A,B)
print(c)
print()
c = np.sin(A)
print(c)
print()
c = np.cos(A)
print(c)
print()
c = np.sqrt(A)
print(c)
print()
c = np.log(A)
print(c)
[[ 5  4  5]
 [ 7  8 15]
 [ 5  2  4]]

[[ 3  2 -1]
 [ 3 -6  3]
 [ 3  0  2]]

[[ 4  3  6]
 [10  7 54]
 [ 4  1  3]]

[[4.         3.         0.66666667]
 [2.5        0.14285714 1.5       ]
 [4.         1.         3.        ]]

[[12 27 32]
 [16 21 30]
 [ 9 14 21]]

[[-0.7568025   0.14112001  0.90929743]
 [-0.95892427  0.84147098  0.41211849]
 [-0.7568025   0.84147098  0.14112001]]

[[-0.65364362 -0.9899925  -0.41614684]
 [ 0.28366219  0.54030231 -0.91113026]
 [-0.65364362  0.54030231 -0.9899925 ]]

[[2.         1.73205081 1.41421356]
 [2.23606798 1.         3.        ]
 [2.         1.         1.73205081]]

[[1.38629436 1.09861229 0.69314718]
 [1.60943791 0.         2.19722458]
 [1.38629436 0.         1.09861229]]

Part 8: Mathematics (Part 2)

In [22]:
a = [[4,3,2],[5,1,9],[4,1,3]]
A = np.array(a)

print(A)
print()
c = A.flatten()
print(c)
print()
c = np.transpose(A)
# c = A.T
print(c)
[[4 3 2]
 [5 1 9]
 [4 1 3]]

[4 3 2 5 1 9 4 1 3]

[[4 5 4]
 [3 1 1]
 [2 9 3]]

Matrices 2D

Exercise 1:

Write a numpy program that prints the dimension of a matrix.

A =[[8, 1, 7, 3],
    [0, 0, 1, 1],
    [0, 4, 8, 5]]

dimension = 3,4
In [5]:
import numpy as np

a = [[8,1,7,3],
     [0,0,1,1],
     [0,4,8,5]]
A = np.array(a)

n = len(A)
m = len(A[0])
print(n,m)
#  print(A.shape[1])
3 4

Exercise 2:

Write a numpy program that creates an 3x3 identity matrix of 8.

A = [[8, 0, 0],
     [0, 8, 0],
     [0, 0, 8]]

Solution 1:

In [9]:
import numpy as np

a = [[0,0,0],
     [0,0,0],
     [0,0,0]]
A = np.array(a)
n = A.shape[0]
m = A.shape[1]
for i in range(n):
    for j in range(m):
        if i == j:
            A[i][j] = 8
print(A)
[[8 0 0]
 [0 8 0]
 [0 0 8]]

Solution 2:

In [10]:
import numpy as np

a = [[0,0,0],
     [0,0,0],
     [0,0,0]]
a = np.eye(5)*8
print(a)
[[8. 0. 0. 0. 0.]
 [0. 8. 0. 0. 0.]
 [0. 0. 8. 0. 0.]
 [0. 0. 0. 8. 0.]
 [0. 0. 0. 0. 8.]]

Exercise 3:

Write a NumPy program to create a 5x5 zero matrix with elements on the main diagonal equal to 1, 2, 3, 4, 5.

A = [[1, 0, 0, 0, 0],
     [0, 2, 0, 0, 0],
     [0, 0, 3, 0, 0],
     [0, 0, 0, 4, 0],
     [0, 0, 0, 0, 5]]

Solution 1:

In [52]:
a =[[0,0,0,0,0],
    [0,0,0,0,0],
    [0,0,0,0,0],
    [0,0,0,0,0],
    [0,0,0,0,0]]
A = np.array(a)
n = A.shape[0]
m = A.shape[1]

for i in range(n):
    for j in range(m):
        if i == j:
            A[i][j] = i+1
print(A)
[[1 0 0 0 0]
 [0 2 0 0 0]
 [0 0 3 0 0]
 [0 0 0 4 0]
 [0 0 0 0 5]]

Solution 2:

In [13]:
import numpy as np

c = np.arange(1,6)
A = np.diagflat(c)
print(A)
[[1 0 0 0 0]
 [0 2 0 0 0]
 [0 0 3 0 0]
 [0 0 0 4 0]
 [0 0 0 0 5]]

Exercise 4:

Write a NumPy program to create a 7x7 matrix, in which the elements on the borders will be equal to 1, and inside 0.

A = [[1, 1, 1, 1, 1, 1, 1],
     [1, 0, 0, 0, 0, 0, 1],
     [1, 0, 0, 0, 0, 0, 1],
     [1, 0, 0, 0, 0, 0, 1],
     [1, 0, 0, 0, 0, 0, 1],
     [1, 0, 0, 0, 0, 0, 1],
     [1, 1, 1, 1, 1, 1, 1]]

Solution 1:

In [14]:
import numpy as np

a = [[0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0]]

A = np.array(a)
n = len(A)
m = len(A[0])
for i in range(n):
    for j in range(m):
        A[0][j]= 1
        A[i][0]= 1
        A[n-1][j]= 1
        A[i][n-1]= 1
print(A)
[[1 1 1 1 1 1 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 1 1 1 1 1 1]]

Solution 2:

In [15]:
import numpy as np

a = [[0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0]]

A = np.array(a)
A[0,:]= 1
A[:,0]= 1
A[n-1,:]= 1
A[:,n-1]= 1
print(A)
[[1 1 1 1 1 1 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 0 0 0 0 0 1]
 [1 1 1 1 1 1 1]]

Exercise 5:

Write a NumPy program to create a 3x3 matrix with values ranging from 2 to 10.

A = [[2, 3, 4 ],
     [5, 6, 7 ],
     [8, 9, 10]]

Solution 1:

In [16]:
a =[[0,0,0],
    [0,0,0],
    [0,0,0]]
A = np.array(a)
n = len(A)
m = len(A[0])
#
for i in range(n):
    t = i + 1
    for j in range(m):
        A[i][j]= 2*i + j + t + 1
print(A)
[[ 2  3  4]
 [ 5  6  7]
 [ 8  9 10]]

Solution 2:

In [18]:
a = np.arange(2,11)
A = a.reshape((3,3))
print(A)
[[ 2  3  4]
 [ 5  6  7]
 [ 8  9 10]]

Exercise 6:

Write a NumPy program to create a nxn matrix and fill it with a checkerboard pattern.

n = 4
Output:
       [[0 1 0 1],
        [1 0 1 0],
        [0 1 0 1],
        [1 0 1 0]]
In [19]:
a =[[0,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0]]
A = np.array(a)
n = len(A)
m = len(A[0])

for i in range(n):
    for j in range(m):
        if (i+j)%2==1:
            A[i][j] = 1
print(A)
[[0 1 0 1]
 [1 0 1 0]
 [0 1 0 1]
 [1 0 1 0]]

Exercise 7:

Write a NumPy program to create an array which looks like below array.

n = 4
Output:
       [[ 0.  0.  0.]
        [ 1.  0.  0.]
        [ 1.  1.  0.]
        [ 1.  1.  1.]]

Solution 1:

In [20]:
import numpy as np

a = np.array([])
n = 4
for i in range(0,n):
    for j in range(n-i,n):
        a = np.append(a,1)
    for k in range(i+1,n):
        a = np.append(a,0)

A = a.reshape((n,(n-1)))
print(A)
[[0. 0. 0.]
 [1. 0. 0.]
 [1. 1. 0.]
 [1. 1. 1.]]

Solution 2:

In [24]:
x = np.tri(4, 3, 0)
print(x)
[[1. 0. 0.]
 [1. 1. 0.]
 [1. 1. 1.]
 [1. 1. 1.]]

Exercise 8:

Write a NumPy program to get the number of nonzero elements in an array.

    Original array:
    [[ 0 10 20]
     [20 0 40]]
    Number of non zero elements in the above array:
    4

Solution 1:

In [26]:
import numpy as np

a = [[ 0, 10, 20],
     [20, 0, 40]]
A = np.array(a)
s = 0
n = A.shape[0]
m = A.shape[1]
#
for i in range(n):
    for j in range(m):
        if A[i][j]==0:
            s = s + 1
print("nonzero = ",n*m-s)
nonzero =  4

Solution 2:

In [27]:
import numpy as np

a = [[ 0, 10, 20],
     [20, 0, 40]]
A = np.array(a)
x = np.count_nonzero(A)
print(x)
4

Exercise 9:

Write a NumPy program to convert a NumPy array of float values to a NumPy array of integer values.

    Original array elements:
    [[ 12.0  12.51]
     [  2.34  7.98]
     [ 25.23 36.5 ]]
    Convert float values to intger values:
    [[12 12]
     [ 2  7]
     [25 36]]

Solution 1:

In [28]:
import numpy as np

a = [[ 12.0,  12.51],
     [  2.34,  7.98],
     [ 25.23, 36.5 ]]
A = np.array(a)
n = A.shape[0]
m = A.shape[1]

for i in range(n):
    for j in range(m):
        A[i][j] = int(A[i][j])
print(A)
[[12. 12.]
 [ 2.  7.]
 [25. 36.]]

Solution 2:

In [29]:
A = np.array(a).astype(int)
print(A)
[[12 12]
 [ 2  7]
 [25 36]]

Exercise 10:

Write a NumPy program to remove the negative values in a NumPy array with 0.

    Original array:
    [-1 -4 0 2 3 4 5 -6]
    Replace the negative values of the said array with 0:
    [0 0 0 2 3 4 5 0]

Solution 1:

In [31]:
import numpy as np

A = np.array([-1, -4, 0, 2, 3, 4, 5, -6])
n = A.shape[0]
#
for i in range(n):
    if A[i] < 0:
        A[i] = 0
print(A)
[0 0 0 2 3 4 5 0]

Solution 2:

In [30]:
import numpy as np

A = np.array([-1, -4, 0, 2, 3, 4, 5, -6])
A[A<0]=0
print(A)
[0 0 0 2 3 4 5 0]

Exercise 11:

Write a NumPy program to count the frequency of unique values in NumPy array.

    Original array:
    [10 10 20 10 20 20 20 30 30 50 40 40]

    Frequency of unique values of the said array:
    [[10 20 30 40 50]
     [ 3  4  2  2 1]]

Solution 1:

In [33]:
import numpy as np

a = np.array([10, 10, 20, 10, 20, 20, 20, 30, 30, 50, 40, 40])
b = np.array([])

# put unique elelments in an array
c = np.unique(a)
d = np.array([])
# count number of occurences of each element appeared in original array
for i in range(len(c)):
    s = 0
    for j in range(len(a)):
        if a[j]==c[i]:
            s = s + 1
    d = np.append(d,s)
k = np.vstack((c,d))
print(k)
[[10. 20. 30. 40. 50.]
 [ 3.  4.  2.  2.  1.]]

Solution 2:

In [34]:
import numpy as np

a = np.array([10, 10, 20, 10, 20, 20, 20, 30, 30, 50, 40, 40])
u,c = np.unique(a,return_counts=True)
print(u)
print(c)
[10 20 30 40 50]
[3 4 2 2 1]

Exercise 12:

Write a NumPy program to find indices of elements equal to zero in a NumPy array.

    Sample Output:
    Original array:
    [1 0 2 0 3 0 4 5 6 7 8]
    Indices of elements equal to zero of the said array:
    [1 3 5]

Solution 1:

In [35]:
import numpy as np

a = np.array([1, 0, 2, 0, 3, 0, 4, 5, 6, 7, 8])
b = np.array([])

for i in range(a.shape[0]):
    if a[i]==0:
        b = np.append(b,i)
print(b)
[1. 3. 5.]

Solution 2:

In [36]:
import numpy as np

a = np.array([1, 0, 2, 0, 3, 0, 4, 5, 6, 7, 8])
x = np.where(a <= 4)
print(x[0])
[0 1 2 3 4 5 6]

Exercise 13:

Write a NumPy program to stack arrays in sequence horizontally (column wise).

    Sample Output:
     Original arrays:
     Array-1:
     [[0 1 2]
      [3 4 5]
      [6 7 8]]
     Array-2:
     [[ 0  3  6]
      [ 9 12 15]
      [18 21 24]]
     Stack arrays in sequence horizontally:
     [[ 0 1 2  0  3  6]
      [ 3 4 5  9 12 15]
      [ 6 7 8 18 21 24]]
In [37]:
import numpy as np
a = [[0, 1, 2],
     [3, 4, 5],
     [6, 7, 8]]

b = [[ 0,  3,  6],
     [ 9, 12, 15],
     [18, 21, 24]]
A = np.array(a)
B = np.array(b)
c = np.hstack((a,b))
print(c)
[[ 0  1  2  0  3  6]
 [ 3  4  5  9 12 15]
 [ 6  7  8 18 21 24]]

Exercise 14:

Write a NumPy program to stack arrays in sequence vertically (row wise).

    Sample Output:
    Original arrays:
    Array-1
    [[0 1 2]
     [3 4 5]
     [6 7 8]]
    Array-2
    [[ 0  3  6]
     [ 9 12 15]
     [18 21 24]]
    Stack arrays in sequence horizontally:
    [[ 0  1  2]
     [ 3  4  5]
     [ 6  7  8]
     [ 0  3  6]
     [ 9 12 15]
     [18 21 24]]
In [38]:
import numpy as np

a = [[0, 1, 2],
     [3, 4, 5],
     [6, 7, 8]]

b = [[ 0,  3,  6],
     [ 9, 12, 15],
     [18, 21, 24]]
A = np.array(a)
B = np.array(b)
c = np.vstack((a,b))
print(c)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 0  3  6]
 [ 9 12 15]
 [18 21 24]]

Exercise 15:

Write a NumPy program to extract all the elements of the second row from a given (4x4) array.

    Sample Output:
    Original array:
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]
     [12 13 14 15]]
    Extracted data: Second row
     [4 5 6 7]
In [39]:
import numpy as np

a = [[ 0,  1,  2,  3],
     [ 4,  5,  6,  7],
     [ 8,  9, 10, 11],
     [12, 13, 14, 15]]
A = np.array(a)

print(A[1])
print(A[1][:-1])
print(A[1][1:3])
print(A[1:3,1:3])
[4 5 6 7]
[4 5 6]
[5 6]
[[ 5  6]
 [ 9 10]]

Exercise 16:

Write a NumPy program to extract all the elements of the third column from a given (4x4) array.

    Sample Output:
    Original array:
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]
     [12 13 14 15]]
    Extracted data: Third column
     [ 2 6 10 14]
In [40]:
import numpy as np

a = [[ 0,  1,  2,  3],
     [ 4,  5,  6,  7],
     [ 8,  9, 10, 11],
     [12, 13, 14, 15]]
A = np.array(a)

print(A[0:4,2])
print(A[0:4,1:3])
[ 2  6 10 14]
[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]

Exercise 17:

Ex17: Write a NumPy program to swap columns in a given array.

    Sample Output:
    column1 = 0
    column2 = 1
    Original array:
    [[ 0 1  2  3]
     [ 4 5  6  7]
     [ 8 9 10 11]]
    After swapping arrays:
    [[ 1 0  2  3]
     [ 5 4  6  7]
     [ 9 8 10 11]]

Solution 1:

In [41]:
import numpy as np

a = [[ 0,  1,  2,  3],
     [ 4,  5,  6,  7],
     [ 8,  9, 10, 11]]
A = np.array(a)
b = np.array([])

# extract column2
b = A[0:3,1]
# delete column2
a = np.delete(a,1,axis = 1)
# insert column2 to a
c = np.insert(a,0,b,axis=1)
print(c)
[[ 1  0  2  3]
 [ 5  4  6  7]
 [ 9  8 10 11]]

Solution 2:

In [45]:
import numpy as np

a = [[ 0,  1,  2,  3],
     [ 4,  5,  6,  7],
     [ 8,  9, 10, 11]]
A = np.array(a)
A[0:3,0] , A[0:3,1] = A[0:3,1].copy() , A[0:3,0].copy()
print(A)
print()
x = A[0].copy()
A[0] = A[2].copy()
A[2] = x
print(A)
[[ 1  0  2  3]
 [ 5  4  6  7]
 [ 9  8 10 11]]

[[ 9  8 10 11]
 [ 5  4  6  7]
 [ 1  0  2  3]]

Test:

In [46]:
import numpy as np

a = [[ 0,  1,  2,  3],
     [ 4,  5,  6,  7],
     [ 8,  9, 10, 11]]
A = np.array(a)
B = A
C = B.copy()
D = C.copy()
C[0][1] = 10
print(A)
print(B)
print(C)
print(D)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0 10  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Exercise 18:

Write a NumPy program to calculate the sum of all columns of a 2D NumPy array.

    Sample Output:
    Original 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 25 26]
     [27 28 29 30 31 32 33 34 35]]
    Sum of all columns:
     [54 58 62 66 70 74 78 82 86]
In [47]:
import numpy as np

a = [[ 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, 25, 26],
     [27, 28, 29, 30, 31, 32, 33, 34, 35]]
A = np.array(a)

b = A.sum(axis=0)
c = A.sum(axis=1)
print("sum of column = ",b)
print("sum of row = ",c)
sum of column =  [54 58 62 66 70 74 78 82 86]
sum of row =  [ 36 117 198 279]

Exercise 19:

Write a NumPy program to create a new array which is the average of every consecutive triplet (连续的三个数为一组) of elements of a given array.

    Sample Output:
    Original array:
    [ 1 2 3 2 4 6 1 2 12 0 -12 6]
    Average of every consecutive triplet of elements of the said array:
    [ 2. 4. 5. -2.]
In [48]:
import numpy as np

a = [ 1, 2, 3, 2, 4, 6, 1, 2, 12, 0, -12, 6]
A = np.array(a)
n = A.shape[0]

# A = A.reshape((n,3))
# c = A.mean(axis=1)
# print(c)
b = np.array([])

for i in range(0,n-2):
    for j in range(i,i+3):
        b = np.append(b,A[j])

m = int((b.shape[0]/3))
b = b.reshape((m,3))
print(b)
print()

c = b.max(axis=1)
print(c)
[[  1.   2.   3.]
 [  2.   3.   2.]
 [  3.   2.   4.]
 [  2.   4.   6.]
 [  4.   6.   1.]
 [  6.   1.   2.]
 [  1.   2.  12.]
 [  2.  12.   0.]
 [ 12.   0. -12.]
 [  0. -12.   6.]]

[ 3.  3.  4.  6.  6.  6. 12. 12. 12.  6.]

Exercise 20:

Write a NumPy program to set zero to lower triangles along the last two axes of a three-dimensional of a given array.

    Original array:
    [[[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. 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. 1. 1. 1. 1. 1. 1.]
      [1. 1. 1. 1. 1. 1. 1. 1.]]]

    Result:
    [[[0. 1. 1. 1. 1. 1. 1. 1.]
      [0. 0. 1. 1. 1. 1. 1. 1.]
      [0. 0. 0. 1. 1. 1. 1. 1.]
      [0. 0. 0. 0. 1. 1. 1. 1.]
      [0. 0. 0. 0. 0. 1. 1. 1.]
      [0. 0. 0. 0. 0. 0. 1. 1.]
      [0. 0. 0. 0. 0. 0. 0. 1.]
      [0. 0. 0. 0. 0. 0. 0. 0.]]]

Solution 1:

In [49]:
import numpy as np

a = [[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, 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, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1]]
A = np.array(a)
n = A.shape[0]
m = A.shape[1]

for i in range(0,n):
    for j in range(n-i-1,m):
        A[i][n-j-1] = 0
    for k in range(i+1,m):
        A[i][k] = 1
print(A)
[[0 1 1 1 1 1 1 1]
 [0 0 1 1 1 1 1 1]
 [0 0 0 1 1 1 1 1]
 [0 0 0 0 1 1 1 1]
 [0 0 0 0 0 1 1 1]
 [0 0 0 0 0 0 1 1]
 [0 0 0 0 0 0 0 1]
 [0 0 0 0 0 0 0 0]]

Solution 2:

In [50]:
import numpy as np

a = [[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, 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, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1]]
A = np.array(a)
x = np.triu(A, 1)
print(x)
[[0 1 1 1 1 1 1 1]
 [0 0 1 1 1 1 1 1]
 [0 0 0 1 1 1 1 1]
 [0 0 0 0 1 1 1 1]
 [0 0 0 0 0 1 1 1]
 [0 0 0 0 0 0 1 1]
 [0 0 0 0 0 0 0 1]
 [0 0 0 0 0 0 0 0]]

Matrices 3D

Exercise 1:

Add all the elements in a 2D matrix to 5 and print it.

In [7]:
import numpy as np
a = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
A = np.array(a)
n = A.shape[0]
m = A.shape[1]

# for i in range(n):
#     for j in range(m):
#         A[i][j] = A[i][j] + 5
A = A + 5
print(A)
[[ 6  7  8]
 [ 9 10 11]
 [12 13 14]]

Exercise 2:

Add all the elements in a 3D matrix to 1 and print it.

In [6]:
a =[[[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]],
    [[  2,   0,  0,  0,  7,  6],
     [  8,   1,  2,  0,  5,  3],
     [  4,   0,  7,  4,  0,  2],
     [ 12,  10,  5, 12,  9,  1]],
    [[  2,   0,  0,  0,  7,  6],
     [  8,   1,  2,  0,  5,  3],
     [  4,   0,  7,  4,  0,  2],
     [ 12,  10,  5, 12,  9,  1]]]

A = np.array(a)
n = A.shape[0]
m = A.shape[1]
k = A.shape[2]

# for i in range(n):
#     for j in range(m):
#         for t in range(k):
#             A[i][j][t] = A[i][j][t] + 1

A = A + 1
print(A)
[[[ 13  21  31   1   8   7]
  [  9  13   3   1   6   4]
  [ 35  71  38   5  11  13]
  [113 101  26  13   2   4]]

 [[  3   1   1   1   8   7]
  [  9   2   3   1   6   4]
  [  5   1   8   5   1   3]
  [ 13  11   6  13  10   2]]

 [[  3   1   1   1   8   7]
  [  9   2   3   1   6   4]
  [  5   1   8   5   1   3]
  [ 13  11   6  13  10   2]]]

Exercise 3:

Perform two numpy 3D Matrices multiplication.

In [9]:
a =[[[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]],
    [[  2,   0,  0,  0,  7,  6],
     [  8,   1,  2,  0,  5,  3],
     [  4,   0,  7,  4,  0,  2],
     [ 12,  10,  5, 12,  9,  1]]]

b = [[  1, 1, 1, 1],
     [  1, 1, 1, 1],
     [  1, 1, 1, 1],
     [  1, 1, 1, 1],
     [  1, 1, 1, 1],
     [  1, 1, 2, 0]]
A = np.array(a)
B = np.array(b)
n = A.shape[0]

# for i in range(n):
#     C.append(A[i]@B)
# C = np.array(C)
# C = C.reshape((n,4,4))
C = A @ B
print(C)
[[[ 75  75  81  69]
  [ 30  30  33  27]
  [167 167 179 155]
  [253 253 256 250]]

 [[ 15  15  21   9]
  [ 19  19  22  16]
  [ 17  17  19  15]
  [ 49  49  50  48]]]

Exercise 4:

Perform two numpy 3D Matrices broadcasting multiplication.

In [10]:
a =[[[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]],
    [[  2,   0,  0,  0,  7,  6],
     [  8,   1,  2,  0,  5,  3],
     [  4,   0,  7,  4,  0,  2],
     [ 12,  10,  5, 12,  9,  1]]]

b = [[[2, 2, 2, 2, 2, 2],
     [ 2, 2, 2, 2, 2, 2],
     [ 2, 2, 2, 2, 2, 2],
     [ 2, 2, 2, 2, 2, 2]],
    [[ 2, 2, 2, 2, 2, 2],
     [ 2, 2, 2, 2, 2, 2],
     [ 2, 2, 2, 2, 2, 2],
     [ 2, 2, 2, 2, 2, 2]]]

A = np.array(a)
B = np.array(b)
C = []
n = A.shape[0]
m = A.shape[1]
k = A.shape[2]

# for i in range(n):
#     C.append(A[i]*B[i])
# C = np.array(C)
# C = C.reshape((n,m,k))

C = A * B
print(C)
[[[ 24  40  60   0  14  12]
  [ 16  24   4   0  10   6]
  [ 68 140  74   8  20  24]
  [224 200  50  24   2   6]]

 [[  4   0   0   0  14  12]
  [ 16   2   4   0  10   6]
  [  8   0  14   8   0   4]
  [ 24  20  10  24  18   2]]]

Exercise 5:

Pad zeros around a 2D matrix

In [14]:
a = [[  2,   0,  0,  0,  7,  6],
     [  8,   1,  2,  0,  5,  3],
     [  4,   0,  7,  4,  0,  2],
     [ 12,  10,  5, 12,  9,  1]]

A = np.array(a)
n = A.shape[0]
m = A.shape[1]
B = np.zeros((n+2,m+2))
n1 = B.shape[0]
m1 = B.shape[1]
B[1:n1-1,1:m1-1] = A
print(B)
[[ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  2.  0.  0.  0.  7.  6.  0.]
 [ 0.  8.  1.  2.  0.  5.  3.  0.]
 [ 0.  4.  0.  7.  4.  0.  2.  0.]
 [ 0. 12. 10.  5. 12.  9.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]]

Exercise 6:

Calculate the total mean of a 3D matrix

In [13]:
a =[[[ 12,  20, 30,  0,  7,  6],
     [  8,  12,  2,  0,  5,  3],
     [ 34,  70, 37,  4, 10, 12],
     [112, 100, 25, 12,  1,  3]],
    [[  2,   0,  0,  0,  7,  6],
     [  8,   1,  2,  0,  5,  3],
     [  4,   0,  7,  4,  0,  2],
     [ 12,  10,  5, 12,  9,  1]],
    [[  2,   0,  0,  0,  7,  6],
     [  8,   1,  2,  0,  5,  3],
     [  4,   0,  7,  4,  0,  2],
     [ 12,  10,  5, 12,  9,  1]]]

A = np.array(a)
n = A.shape[0]
# C = []
# for i in range(n):
#     C.append(A[i].mean())
# C = np.array([C])
# C = C.mean()
# print(C)

A = A.mean()
print(A)
10.069444444444445

Exercise 7:

Calculate the mean of values for each axis

In [12]:
a = [[[1,1,1],
 	  [2,2,2],
      [3,3,3]],
     [[1,1,1],
 	  [2,2,2],
      [3,3,3]],
     [[1,1,1],
 	  [2,2,2],
      [3,3,3]]]


A = np.array(a)

n = A.shape[0]
m = A.shape[1]
k = A.shape[2]
C = np.zeros((m,k))
D = []
E = []

for i in range(n):   # axis = 0
    C = C + A[i]
C = C / n

for i in range(n):   # axis = 1
    for j in range(k):
        D.append(A[i,0:m,j].sum()/m)
D = np.array(D)
D = D.reshape((m,k))

for i in range(n):   # axis = 2
    for j in range(m):
        E.append(A[i][j].sum()/k)
E = np.array(E)
E = E.reshape((m,k))

print(C)
print()
print(D)
print()
print(E)
[[1. 1. 1.]
 [2. 2. 2.]
 [3. 3. 3.]]

[[2. 2. 2.]
 [2. 2. 2.]
 [2. 2. 2.]]

[[1. 2. 3.]
 [1. 2. 3.]
 [1. 2. 3.]]

Exercise 8:

Based on Ex7, calculate the followings:

Mean of axes=(0,1):   [2. 2. 2.]
Mean of axes=(0,2):   [1. 2. 3.]
Mean of axes=(1,2):   [2. 2. 2.]
Mean of axes=(0,1,2): 2.0
In [11]:
a = [[[1,1,1],
 	  [2,2,2],
      [3,3,3]],
     [[1,1,1],
 	  [2,2,2],
      [3,3,3]],
     [[1,1,1],
 	  [2,2,2],
      [3,3,3]]]

A = np.array(a)

n = A.shape[0]
m = A.shape[1]
k = A.shape[2]
C = np.zeros((m,k))
D = []
E = []
F = []
G = []

for i in range(n):   # axis = (0,1)
    C = C + A[i]
C = C / n
for i in range(3):
    D.append(C[0:3,i].sum()/n)
D = np.array(D)

for i in range(3):   # axis = (0,2)
    E.append(C[i].sum()/k)
E = np.array(E)

for i in range(n):   # axis = (1,2)
    for j in range(k):
        F.append(A[i,0:m,j].sum()/m)
F = np.array(F)
F = F.reshape((m,k))

for i in range(n):
        G.append(F[i].sum()/k)
G = np.array(G)

print(D)
print()
print(E)
print()
print(G)
[2. 2. 2.]

[1. 2. 3.]

[2. 2. 2.]

Exercise 9:

Flip vertically the matrix:

In [15]:
A = [[1,2,3],
  	 [4,5,6],
      [7,8,9]]
A = np.array(A)
C = []
for i in range(A.shape[0]):
    C.append(A[i][::-1])
C = np.array(C)
print(C)
[[3 2 1]
 [6 5 4]
 [9 8 7]]

Exercise 10:

Flip horizontally the matrix:

In [16]:
A = [[1,2,3],
 	 [4,5,6],
     [7,8,9]]
A = np.array(A)
A = A[::-1,:]
print(A)
[[7 8 9]
 [4 5 6]
 [1 2 3]]