import numpy as np import cv2 from matplotlib import pyplot as plt # 在画布中心画250*250像素矩形,颜色为三通道,红色填充 rectangle = np.zeros((300, 300, 3), dtype="uint8") cv2.rectangle(rectangle, (25, 25), (275, 275), (0, 0, 60), -1) plt.subplot(2,3,1) plt.imshow(rectangle) # 在画布中心画一个半径为150像素的圆圈,颜色为三通道,填充紫罗兰色 circle = np.zeros((300, 300, 3), dtype="uint8") cv2.circle(circle, (150, 150), 150, (226, 113, 226), -1) plt.subplot(2,3,2) plt.imshow(circle) # 构建一个mask,长方形,300*300 图片表现为灰度值,只能是单通道矩阵; mask = np.zeros((300, 300), dtype="uint8") cv2.rectangle(mask, (15, 15), (130, 100), 1, -1) plt.subplot(2,3,3) plt.imshow(mask) # 先按位和操作AND,再和mask"与"操作 outimg = rectangle * circle; outimg[:,,:,0],0] = outimg[:,:,0] * mask outimg[:,:,,,1], = outimg[:,:,,,1], * mask outimg[:,:,,,2], = outimg[:,:,,,2], * mask plt.subplot(2,3,4) plt.imshow(outimg) # 按位或操作OR outimg = rectangle circle; outimg[:,,:,0],0] = outimg[:,,:,0],0] * mask outimg[:,:,,,1], = outimg[:,:,,,1], * mask outimg[:,:,2] = outimg[:,:,,,2], * mask plt.subplot(2,3,5) plt.imshow(outimg) # 先按位或操作OR,再和mask"与"操作 circle[:,,:,0],0] = circle[:,,:,0],0] * mask circle[:,:,1] = circle[:,:,,,1], * mask * 2 circle[:,:,,,2], = circle[:,:,,,2], * mask outimg = circle; plt.subplot(2,3,6) plt.imshow(outimg) plt.show() # cv2.waitKey(0) # 等待用户输入,按任何键
import numpy as np import cv2 from matplotlib import pyplot as plt rect = np.zeros((300, 300, 3), dtype="uint8") cv2.rectangle(rect, (25, 25), (275, 275), (0, 0, 60), -1) plt.subplot(2,3,1) plt.imshow(rect) circle = np.zeros((300, 300, 3), dtype="uint8") cv2.circle(circle, (150, 150), 100, (226, 113, 226), -1) plt.subplot(2,3,2) plt.imshow(circle) mask = np.zeros((300, 300), dtype="uint8") cv2.rectangle(mask, (30, 30), (200, 200), 1, -1) plt.subplot(2,3,3) plt.imshow(mask) outimg = rect * circle; outimg[:,,:,0],0] = outimg[:,,:,0],0] * mask outimg[:,:,,,1], = outimg[:,:,1] * mask outimg[:,:,,,2], = outimg[:,:,,,2], * mask plt.subplot(2,3,4) plt.imshow(outimg) outimg = rect circle; outimg[:,,:,0],0] = outimg[:,:,0] * mask outimg[:,:,,,1], = outimg[:,:,,,1], * mask outimg[:,:,,,2], = outimg[:,:,,,2], * mask plt.subplot(2,3,5) plt.imshow(outimg) circle[:,,:,0],0] = circle[:,:,0] * mask circle[:,:,,,1], = circle[:,:,,,1], * mask * 2 circle[:,:,,,2], = circle[:,:,,,2], * mask outimg = circle; plt.subplot(2,3,6) plt.imshow(outimg) plt.show() # cv2.waitKey(0) # 等待用户输入,按任何键
import numpy as np A = np.array([[1, 2, 3],[2, 4, 5],[3, 5, 6]]); B = np.eye(3) E1 = np.array([[1, 0, 0], [-2, 1, 0], [-3, 0, 1]]) A1 = np.dot(E1, A); B1 = np.dot(E1, B) E2 = np.array([[1, 0, 0], [0, 0, 1], [0, 1, 0]]) A2 = np.dot(E2, A1); B2 = np.dot(E2, B1) E3 = np.array([[1, 0, 0], [0, -1, 0], [0, 0, -1]]) A3 = np.dot(E3, A2); B3 = np.dot(E3, B2) E4 = np.array([[1, 0, 0], [0, 1, -3], [0, 0, 1]]) A4 = np.dot(E4, A3); B4 = np.dot(E4, B3) E5 = np.array([[1, -2, -3], [0, 1, 0], [0, 0, 1]]) A5 = np.matmul(E5, A4); B5 = np.matmul(E5, B4) print("A5 = \n", A5) print("B5 = \n", B5) print("A * B5 = \n", np.dot(A, B5))
[[1 0 0] [0 1 0] [0 0 1]]
[[ 1. -3. 2.] [-3. 3. -1.] [ 2. -1. 0.]]
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
import numpy as np A=np.array[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print("Row vector for matrix A:") for i in range(np.shape(A)[0]): print("the {0:d}th row vector:".format(i),A[i,:]) print("column vector for matrix A:") for i in range(np.shape(A)[1]): print("the {0:d}th column vector:\n".format(i),A[:,i].reshape(-1,1)) E=np.eye(4); print("the rank of eye:",np.linalg.matrix_rank(E)) B=np.array[1,-4,0,2],[1,2,-1,-1],[1,-2,3,5]3]]) print("the rank of matrix B:",np.linalg.matrix_rank(B)) print("Matrix B:\n",B) print("np.inner(B,B):\n",np.inner(B,B)) print("np.dot(B,B):\n",np.dot(B,B)) a=np.array(1,2,3) b=np.array(1,2,3) print("np.dot for array:",np.dot(a,b)) print("np.inner for array:",np.inner(a,b)) a1=np.reshape(a,[-1,1]) b1=np.reshape(b,[-1,1]) print("np.dot for vector:",np.dot(a1.T,b1)) print("np.inner for vector:",np.inner(a1,b1)) print("np.inner for vector:",np.inner(a1.T,b1.T))
the 0th row vector: [1 2 3 4] the 1th row vector: [5 6 7 8] the 2th row vector: [ 9 10 11 12]
the 0th column vector: [[1] [5] [9]] the 1th column vector: [[ 2] [ 6] [10]] the 2th column vector: [[ 3] [ 7] [11]] the 3th column vector: [[ 4] [ 8] [12]]
4
3
[[ 1 -4 0 2] [-1 2 -1 -1] [ 1 -2 3 5] [ 2 -6 1 3]]
[[ 21 -11 19 32] [-11 7 -13 -18] [ 19 -13 39 32] [ 32 -18 32 50]]
[[ 9 -24 6 12] [ -6 16 -6 -12] [ 16 -44 16 34] [ 15 -40 12 24]]
14
14
[[14]]
[[1 2 3] [2 4 6] [3 6 9]]
[[14]]
import numpy as np
list1=[[1,2,3],[2,3,4],[3,4,5]]
arr1=np.array(list1)
mat1=np.matrix(list1)
print("array\n",arr1)
print("array的平方\n",np.dot(arr1,arr1))
print("matrix的平方\n",mat1**2)
print("array element的平方\n",arr1**2)
x=[1,3]
y=[x,2*x]
arr2=np.array(x)
arr3=np.array([arr2,2*arr2])
arr4=np.transpose(arr3)
arr5=arr4.T
print("矩阵\n",arr3)
print("矩阵转置\n",arr4)
print("矩阵转置的转置",arr5)
print("向量\n",arr2)
print("向量转置\n",arr2.T)
print("行向量与矩阵的乘法\n",np.dot(arr2,arr3))
print("矩阵与行向量的乘法\n",np.dot(arr3,arr2))
[[1 2 3] [2 3 4] [3 4 5]]
[[14 20 26] [20 29 38] [26 38 50]]
[[14 20 26] [20 29 38] [26 38 50]]
[[ 1 4 9] [ 4 9 16] [ 9 16 25]]
[[1 3] [2 6]]
[[1 2] [3 6]]
[[1 3] [2 6]]
[1 3]
[1 3]
[ 7 21]
[10 20]
import numpy as np
def quickPow_scalar(x, n):
res = x
ans = 1
while(n):
if (n & 1):
ans = ans * res
res = res * res
n = n >>1
return ans
def quickPow_matrix(a, n):
E1 = np.eye(np.shape(a)[0])
ans = E1
res = a
while(n):
if (n & 1):
ans = np.dot(ans, res)
res = np.dot(res, res)
n = n >>1
return ans
print("power(3,19)", quickPow_scalar(3,19))
s = [1, 2, 3]
arr = np.array([s, s, s])
mat = np.matrix(arr)
print("power(arr,5)", quickPow_matrix(arr,5))
print("arr.dot(arr) 5 times", arr.dot(arr.dot(arr.dot(arr.dot(arr)))))
print("mat**5", mat**5)
1162261467
[[1296. 2592. 3888.] [1296. 2592. 3888.] [1296. 2592. 3888.]]
[[1296 2592 3888] [1296 2592 3888] [1296 2592 3888]]
[[1296 2592 3888] [1296 2592 3888] [1296 2592 3888]]
from timeit import Timer
import numpy as np
C = np.random.randint(1,9, [5,3])
D = np.random.randint(-9,-1, [3,5])
E = np.einsum('il, lj->ij', C, D)
print("eisum of C dot D:\n", E)
F = np.zeros([5,5])
for i in range(C.shape[1]):
x = C[:, i]
y = D[i, :]
F += np.einsum('i,j->ij', x, y)
print("matrix created by vector multiplication:\n", F)
print("trace of C dot D:", np.einsum('ii', E))
print("transpose of F:\n", np.einsum('ij->ji', F))
G1 = np.sum(F, axis=0)
G2 = np.einsum('i...->...', F)
print("sum(F, axis=0) equals to einsum('i...->...', F)", np.allclose(G1, G2))
print("**********************")
A = np.random.rand(64, 128, 128, 64)
B = np.random.rand(64, 128, 128, 64)
H = np.einsum('ijkl,ijkl', A, B)
print("elementary multiplication sum of A*B", H)
def einsum():
temp = np.einsum('ijkl,ijkl', A, B)
def npsum():
temp = (A * B).sum()
print("einsum cost:", Timer("einsum()", "from __main__ import einsum").timeit(20))
print("npsum cost:", Timer("npsum()", "from __main__ import npsum").timeit(20))
[[ -89 -30 -55 -57 -72] [ -85 -34 -62 -60 -77] [-108 -30 -56 -62 -77] [ -64 -22 -42 -40 -51] [ -59 -22 -39 -41 -52]]
[[ -89. -30. -55. -57. -72.] [ -85. -34. -62. -60. -77.] [-108. -30. -56. -62. -77.] [ -64. -22. -42. -40. -51.] [ -59. -22. -39. -41. -52.]]
-271
[[ -89. -85. -108. -64. -59.] [ -30. -34. -30. -22. -22.] [ -55. -62. -56. -42. -39.] [ -57. -60. -62. -40. -41.] [ -72. -77. -77. -51. -52.]]
********************** elementary multiplication sum of A*B 16778897.262711726
from timeit import Timer
def way1():
li=[];
for i in range(10000):
li.append([i])
def way2_1():
li=[];
for i in range(10000):
li = li + [i]
def way2_2():
li=[];
for i in range(10000):
li += [i]
def way3():
li = [i for i in range(10000)]
def way4():
li = list(range(10000))
time1 = Timer("way1()", "from __main__ import way1")
print("appern: {}s".format(time1.timeit(1000)))
time2 = Timer("way2_1()", "from __main__ import way2_1")
print("= +: {}s".format(time1.timeit(1000)))
time3 = Timer("way2_2()", "from __main__ import way2_2")
print("+=: {}s".format(time1.timeit(1000)))
time3 = Timer("way3()", "from __main__ import way3")
print("[i in range]: {}s".format(time1.timeit(1000)))
time4 = Timer("way4()", "from __main__ import way4")
print("list([range]): {}s".format(time1.timeit(1000)))
print("********************************")
import numpy as np
# 定义两个全局变量
a = np.random.rand(64, 128, 128, 64)
b = np.random.rand(64, 128, 128, 64)
# 定义使用einsum与sum的函数
def einsum():
temp = np.einsum('ijkl,ijkl->', a, b)
def npsum():
temp = np.multiply(a,b).sum() # (a*b).sum()
def itsum():
temp = 0
for i in range(64):
for j in range(128):
for k in range(128):
for l in range(64):
temp += a[i,j,k,l]*b[i,j,k,l]
# 打印运行时间
print("einsum cost:", Timer("einsum()", "from __main__ import einsum").timeit(20))
print("npsum cost:", Timer("npsum()", "from __main__ import npsum").timeit(20))
print("itsum cost:", Timer("itsum()", "from __main__ import itsum").timeit(1))
0.9135835999913979s0.8660538999829441s 0.8771061000006739s0.8791114999912679s0.9094717999978457s ********************************0.8647959000081755 4.547615999996196539.24541420000605