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)
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()
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")
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)
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)
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)
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)
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)
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 = " ")
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")
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)
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)
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)
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)
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)
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
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])
Write a numpy program that creates an 3x3 identity matrix of 8.
A = [[8, 0, 0],
[0, 8, 0],
[0, 0, 8]]
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)
import numpy as np
a = [[0,0,0],
[0,0,0],
[0,0,0]]
a = np.eye(5)*8
print(a)
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]]
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)
import numpy as np
c = np.arange(1,6)
A = np.diagflat(c)
print(A)
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]]
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)
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)
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]]
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)
a = np.arange(2,11)
A = a.reshape((3,3))
print(A)
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]]
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)
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.]]
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)
x = np.tri(4, 3, 0)
print(x)
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
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)
import numpy as np
a = [[ 0, 10, 20],
[20, 0, 40]]
A = np.array(a)
x = np.count_nonzero(A)
print(x)
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]]
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)
A = np.array(a).astype(int)
print(A)
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]
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)
import numpy as np
A = np.array([-1, -4, 0, 2, 3, 4, 5, -6])
A[A<0]=0
print(A)
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]]
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)
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)
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]
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)
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])
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]]
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)
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]]
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)
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]
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])
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]
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])
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]]
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)
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)
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)
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]
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)
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.]
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)
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.]]]
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)
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)
Add all the elements in a 2D matrix to 5 and print it.
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)
Add all the elements in a 3D matrix to 1 and print it.
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)
Perform two numpy 3D Matrices multiplication.
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)
Perform two numpy 3D Matrices broadcasting multiplication.
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)
Pad zeros around a 2D matrix
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)
Calculate the total mean of a 3D matrix
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)
Calculate the mean of values for each axis
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)
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
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)
Flip vertically the matrix:
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)
Flip horizontally the matrix:
A = [[1,2,3],
[4,5,6],
[7,8,9]]
A = np.array(A)
A = A[::-1,:]
print(A)