In [ ]:
import numpy as np
In [ ]:
A = np.array([1,2,3])
A
In [ ]:
A.shape # output: (3,)
In [ ]:
# 一维数组的转置还是一维数组
A.T
In [ ]:
B = A[np.newaxis]
In [ ]:
B.shape
In [ ]:
B.T
In [ ]:
C = A[None,:]
C.shape
In [ ]:
C.T
In [ ]:
# 1D
a = np.array([1,2,3])
b = np.array([9,8,7])
# 2D
aa = np.array([a,3*a])
bb = np.array([b,2*b])
In [ ]:
print(a, b)
print("\n")
print(aa, bb)
In [ ]:
np.inner(a, b) # = np.dot(a, b)
# 1*9 + 2*8 + 3*7
In [ ]:
np.inner(aa, bb) # = np.dot(aa, bb.T)
# [(1*9 + 2*8 + 3*7), (1*18 + 2*16 + 3*14)]
# [(3*9 + 6*8 + 9*7), (3*18 + 6*16 + 9*14)]
In [ ]:
np.outer(a, b)
# [ 1*9, 1*8, 1*7 ]
# [ 2*9, 2*8, 2*7 ]
# [ 3*9, 3*8, 3*7 ]
In [ ]:
a*b
# [1*9, 2*8, 3*7], 一一对应相乘
In [ ]:
aa*bb
# [1*9, 2*8, 3*7]
# [3*18, 6*16, 9*14]