import numpy as np
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]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]
for i in range(0,n,2):
for j in range(0,m,2):
d = np.append(d,A[i:i+2,j:j+2].max())
print(d.reshape((2,3)))
# c1 = A[0:2,0:2].max()
# c2 = A[0:2,2:4].max()
# c3 = A[0:2,4:6].max()
#
# c4 = A[2:4,0:2].max()
# c5 = A[2:4,2:4].max()
# c6 = A[2:4,4:6].max()
# d = np.array([[c1,c2,c3],
# [c4,c5,c6]])
import numpy as np
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]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]
for i in range(0,n,2):
for j in range(0,m,2):
d = np.append(d,A[i:i+2,j:j+2].min())
print(d.reshape((2,3)))
import numpy as np
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]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]
for i in range(0,n,2):
for j in range(0,m,2):
d = np.append(d,A[i:i+2,j:j+2].mean())
print(d.reshape((2,3)))
import numpy as np
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]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]
y = int(input("kernel size1 = "))
k = int(input("kernel size2 = "))
if n%y == 0 and m%k == 0:
for i in range(0,n,y):
for j in range(0,m,k):
d = np.append(d,A[i:i+y,j:j+k].max())
t1 = int(n/y)
t2 = int(m/k)
d = d.reshape((t1,t2))
print(d)
else:
print(y,k,"is not the common divisor of",n,m)
Pad 4 zeros rows and 4 zeros columns in the matrix borders:
A = [[ 3 3 2 1 5]
[ 5 0 1 3 1]
[ 3 1 2 2 3]
[ 2 0 0 2 2]
[ 2 1 2 1 1]]
A = [[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[0 0 3 3 2 1 5 0 0]
[0 0 5 0 1 3 1 0 0]
[0 0 3 1 2 2 3 0 0]
[0 0 2 0 0 2 2 0 0]
[0 0 2 1 2 1 1 0 0]
[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]]
import numpy as np
a = [[ 3, 3, 2, 1, 5],
[ 5, 0, 1, 3, 1],
[ 3, 1, 2, 2, 3],
[ 2, 0, 0, 2, 2],
[ 2, 1, 2, 1, 1]]
A = np.array(a)
n = A.shape[0]
m = n + 4
b = np.zeros((m,m))
B = np.array(b)
B[2:7,2:7] = a
print(B)
import numpy as np
a = [[0, 0, 0, 0, 0, 0, 0],
[0, 3, 3, 2, 1, 0, 0],
[0, 0, 0, 1, 3, 1, 0],
[0, 3, 1, 2, 2, 3, 0],
[0, 2, 0, 0, 2, 2, 0],
[0, 2, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0]]
A = np.array(a)
n = A.shape[0]
# A = np.delete(A,(0,n-1),axis=0) # delete the 1st & the last rows
# m = A.shape[1]
# A = np.delete(A,(0,m-1),axis=1) # # delete the 1st & the last columns
# print(A)
# 2nd way
A = A[1:n-1,1:n-1]
print(A)
import numpy as np
a = [[1, 0, 1],
[0, 1, 0],
[0, 0, 1]]
b = [[1, 2, 0],
[0, 2, 0],
[1, 0, 0]]
C = np.zeros((3,3))
A = np.array(a)
B = np.array(b)
n = A.shape[0] # a的行
m = B.shape[1] # b的列
t = A.shape[1] # a的列 或 b的行
for i in range(n): # 行数
for j in range(m): # 列数
for k in range(t):
C[i][j] = C[i][j] + (A[i][k] * B[k][j])
print(C)
print("sum = ",C.sum())
# x = A @ B
# print(x)
import numpy as np
a = [[1, 0, 1, 2],
[0, 1, 0, 0],
[0, 0, 1, 1],
[1, 0, 1, 1]]
b = [[1, 1],
[0, 2]]
A = np.array(a)
B = np.array(b)
M = np.array([])
C = A[:,0:2]
D = A[:,2:4]
x = C @ B
y = D @ B
Z = np.hstack((x,y))
n = A.shape[0]
m = A.shape[1]
for i in range(0,n,2):
for j in range(0,m,2):
M = np.append(M,Z[i:i+2,j:j+2].sum())
print(M.reshape((2,2)))
import numpy as np
a = [[ 3, 3, 2, 1, 0],
[ 0, 0, 1, 3, 1],
[ 3, 1, 2, 2, 3],
[ 2, 0, 0, 2, 2],
[ 2, 0, 0, 0, 1]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]
for i in range(0,n-1,1):
for j in range(0,m-1,1):
d = np.append(d,A[i:i+2,j:j+2].sum())
print(d.reshape((n-1,m-1)))
import numpy as np
a = [[ 3, 3, 2, 1, 0],
[ 0, 0, 1, 3, 1],
[ 3, 1, 2, 2, 3],
[ 2, 0, 0, 2, 2],
[ 2, 0, 0, 0, 1]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]
for i in range(0,n-2,1):
for j in range(0,m-2,1):
d = np.append(d,A[i:i+3,j:j+3].sum())
print(d.reshape((n-2,m-2)))
import numpy as np
a = [[ 3, 3, 2, 1, 0],
[ 0, 0, 1, 3, 1],
[ 3, 1, 2, 2, 3],
[ 2, 0, 0, 2, 2],
[ 2, 0, 0, 0, 1]]
A = np.array(a)
d = np.array([])
n = A.shape[0]
m = A.shape[1]
for i in range(0,n-1,1):
for j in range(0,m-2,1):
d = np.append(d,A[i:i+2,j:j+3].sum())
print(d.reshape((n-1,m-2)))
Calculate the 3x3 Convolution as the following example:
M = [[ 3 3 2 1 0]
[ 0 0 1 3 1]
[ 3 1 2 2 3]
[ 2 0 0 2 2]
[ 2 0 0 0 1]]
[[ 6 9 10 8 5]
[10 15 15 15 10]
[ 6 9 11 16 13]
[ 8 10 7 12 10]
[ 4 4 2 5 5]]
Before you do the convolution, try to pad two zero columns and two zero columns , like the following:
(result.sahpe =(5,5), it means the original shpe should be(5+(3-1),5+(3-1))
import numpy as np
a = [[ 3, 3, 2, 1, 0],
[ 0, 0, 1, 3, 1],
[ 3, 1, 2, 2, 3],
[ 2, 0, 0, 2, 2],
[ 2, 0, 0, 0, 1]]
A = np.array(a)
B = np.zeros((7,7))
C = np.array([])
n = B.shape[0]
m = B.shape[1]
B[1:n-1,1:m-1] = A
for i in range(0,n-2,1):
for j in range(0,m-2,1):
C = np.append(C,B[i:i+3,j:j+3].sum())
print(C.reshape((n-2,m-2)))
Calculate the 3x3 Convolution as the following example:
M = [[ 3 3 2 1 0]
[ 0 0 1 3 1]
[ 3 1 2 2 3]
[ 2 0 0 2 2]
[ 2 0 0 0 1]]
[[ 6 9 10 8 5]
[10 15 15 15 10]
[ 6 9 11 16 13]
[ 8 10 7 12 10]
[ 4 4 2 5 5]]
Before you do the convolution, try to pad two zero columns and two zero columns.
like the following:
import numpy as np
a = [[ 3, 3, 2, 1, 0],
[ 0, 0, 1, 3, 1],
[ 3, 1, 2, 2, 3],
[ 2, 0, 0, 2, 2],
[ 2, 0, 0, 0, 1]]
w = [[1,0,1],
[1,1,0],
[1,0,0]]
W = np.array(w)
A = np.array(a)
B = np.zeros((7,7))
C = np.array([])
D = np.array([])
n = B.shape[0]
m = B.shape[1]
B[1:n-1,1:m-1] = A
for i in range(0,n-2,1):
for j in range(0,m-2,1):
C = np.append(C,(B[i:i+3,j:j+3]*W).sum())
C = C.reshape((5,5)) # a.shape=(7,7) reshape(7-(3-1),7-(3-1))
print(C)