Numpy 向量

1.dot函数的两个参数都是向量

import numpy as np
a1 = np.ones(10)
# array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
a2 = 2 * np.ones(10)
# array([2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])
np.dot(a1, a2)
# 20.0

则dot函数实现的是向量的内积运算


2.dot函数的参数是向量和矩阵

a3 = 2 * np.ones(3)
# array([2., 2., 2.])
a4 = np.arange(15).reshape(3, 5)
# array([[ 0,  1,  2,  3,  4],
#        [ 5,  6,  7,  8,  9],
#        [10, 11, 12, 13, 14]])
np.dot(a3, a4)
# array([30., 36., 42., 48., 54.]) 

则向量会自动匹配实现矩阵的乘法


3.dot函数的参数都是矩阵

a4 = np.arange(15).reshape(3, 5)
# array([[ 0,  1,  2,  3,  4],
#        [ 5,  6,  7,  8,  9],
#        [10, 11, 12, 13, 14]])
a5 = np.arange(15).reshape(5, 3)
# array([[ 0,  1,  2],
#        [ 3,  4,  5],
#        [ 6,  7,  8],
#        [ 9, 10, 11],
#        [12, 13, 14]])
np.dot(a4, a5)
# array([[ 90, 100, 110],
#        [240, 275, 310],
#        [390, 450, 510]])

则进行矩阵乘法

猜你喜欢

转载自blog.csdn.net/Ahead_J/article/details/80723286
今日推荐