引用
import numpy as np;
1維陣列
a = np.array([1, 2, 3]);
2維陣列
a = np.array([ [1, 2, 3], [4, 5, 6] ]);
3維陣列
a = np.array([ [ [1, 2, 3], [4, 5, 6] ], [ [7, 8, 9], [10, 11, 12] ] ]);
內積 (vector dot)
v1 = np.array([1, 2, 3]);
v2 = np.array([4, 5, 6]);
v3 = np.dot(v1,v2); #v3 = 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
print(v3);
矩陣相乘 (matrix multiplication)
m1 = np.array([ [1, 2, 3], [4, 5, 6] ]); #size: 2*3
m2 = np.array([ [1, 2 ], [3, 4], [5, 6] ]); #size: 3*2
m3 = np.matmul(m1,m2); # m3= [ [ 1*1+2*3+3*5 1*2+2*4+3*6] [ 4*1+5*3+6*5 4*2+5*4+6*6 ] ] = [ [ 22 28 ] [ 49 64 ] ]
print(m3);
元素相乘 (multiply)
m1 = np.arange(1,5).reshape(2,2); # m1 = [ [ 1 2 ] [ 3 4 ] ]
m2 = np.arange(0,4).reshape(2,2); # m2 = [ [ 0 1 ] [ 2 3 ] ]
m3 = np.multiply(m1,m2); # m3 = [ [ 1*0 2*1 ] [ 3*2 4*3 ] ] = [ [ 0 2 ] [ 6 12 ] ]
print(m3);
原始碼