z = np.zeros((3,4))print(z)print(z.dtype)print()
one = np.ones((3,4), dtype=int)print(one)print(one.dtype)print()
emt = np.empty((3,4), dtype=int)print(emt)print(emt.dtype)print()
ran = np.arange(12).reshape((3,4))print(ran)print(ran.dtype)print()
li = np.linspace(1,10,6).reshape(2,3)print(li)print(li.dtype)
a = np.array([10,20,30,40])
b = np.arange(4)print(a)print(b)print()print(a+b)print(a-b)print(a*b)print()print(a**b)print()print(10*np.sin(a))print()print(b<3)print()
A = np.arange(2,14).reshape(3,4)print(A)print(np.argmin(A))#最小索引print(np.argmax(A))#最大索引print()print(A.mean())print(np.median(A))#中位数print(A.cumsum())#累加值print(np.diff(A))#相邻差值print()
A = np.array([[1,0],[0,3]])print(A)print(A.nonzero())#分别输出非零元素的行和列值print(np.sort(A))#逐行排序后的矩阵print(np.sort(A, axis=0))#逐列排序的矩阵print(np.sort(A).nonzero())print()
B = np.arange(14,2,-1).reshape(3,4)print(B)print(B.transpose())#转置print((B.T).dot(B))#转置print()print(np.clip(B,5,9))#B中将范围限定,大于9的数都为9,小于5的都为5,之间的数不变print()
A = np.arange(3,7)print(A)print(A[2])print()
B = np.arange(3,15).reshape(3,4)print(B)print(B[2])print(B[2][1])print(B[2,1])print()print(B[2,2:])print(B[1:,2:])print()for row in B:print(row)print()for col in B.T:print(col)print()print(B.flatten())for elm in B.flat:print(elm)
#矩阵合并
A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A, B, A, B))print(C)print(A.shape,(A.T).shape)print(C.shape)print()
D = np.hstack((A, B))print(D)print()print(A[np.newaxis,:])print(A[:, np.newaxis])print(np.hstack((A[:, np.newaxis], B[:, np.newaxis])))print()print(np.stack((A,B), axis=0))print(np.stack((A,B), axis=1))#print(np.concatenate((A,B,B,A), axis=0))#print(np.concatenate((A,B,B,A), axis=1))