numpy的rank1 array與矩陣內積外積計算
阿新 • • 發佈:2019-02-19
a = np.random.randn(5) print(a) ## [-1.17124494 -1.0144042 -1.81090015 0.06239375 0.80871696] print(a.T) ## [ 1.27831203 -0.02878799 -0.18697777 0.22739936 -0.53940577] ## a為rank1 vector, 無法進行outer product計算 b = np.random.randn(5, 1) print(b) # b為column vector # [[-0.76206967] # [ 0.38957009] # [-0.60387659] # [ 1.32058981]# [ 0.28547156]] c = np.random.randn(1, 5) print(c) # c為row vector # [[-0.12683617 -0.07037879 0.68081286 -0.93557581 1.1530988 ]] dot = np.dot(b,c) outer = np.outer(b,c) print(dot.shape) # (5, 5) print(outer.shape) # (5, 5) # 可見進行outer product運算的結果相同