1.numpy.dot(a, b, out=None)
函数说明:
Dot product of two arrays.
For 2-D arrays it is equivalent to matrix multiplication(对于2维数组等于矩阵乘法), and for 1-D arrays to inner product of vectors (without complex conjugation)(对于1维数组等于向量内积). For N dimensions it is a sum product over the last axis of a and the second-to-last of b:
向量内积概念:
范例:
'''
Parameters:
a : array_like
First argument.
b : array_like
Second argument.
out : ndarray, optional
Output argument. This must have the exact kind that would be returned if it was not used. In particular,
it must have the right type, must be C-contiguous, and its dtype must be the dtype that would be returned for dot(a,b).
This is a performance feature. Therefore, if these conditions are not met,
an exception is raised, instead of attempting to be flexible.
Returns:
output : ndarray
Returns the dot product of a and b. If a and b are both scalars or both 1-D arrays then a scalar is returned;
otherwise an array is returned. If out is given, then it is returned.
'''
import numpy as np
print(np.dot(3, 4.0)) # 12.0
a = np.array([[1, 2, 3],
[2, 3, 4]])
b = np.array([2, 2, 2])
c = np.array([2, 2, 2])
print(np.dot(a, b)) # [12 18] 实现矩阵乘法
print(np.dot(b, c)) # 12 实现向量内积
2. numpy.multiply(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj])
函数说明:
Multiply arguments element-wise(参数元素对应向乘)
范例:
'''
Parameters:
x1, x2 : array_like
Input arrays to be multiplied.
out : ndarray, None, or tuple of ndarray and None, optional
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to.
If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.
**kwargs
For other keyword-only arguments, see the ufunc docs.
Returns:
y : ndarray
The product of x1 and x2, element-wise. Returns a scalar if both x1 and x2 are scalars.
'''
import numpy as np
print(np.multiply(2.0, 4.0)) # 8.0
a = np.array([[1, 2, 3],
[2, 3, 4]])
b = np.array([2, 2, 2])
c = np.array([2, 2, 2])
print(np.multiply(b, c)) # [4 4 4]
print(np.multiply(a, 2)) # 运用python的广播机制
# [[2 4 6]
# [4 6 8]]
print(np.multiply(a, b))
# [[2 4 6]
# [4 6 8]]
print(b * c) # [4 4 4]
print(a * 2) # 与multiply结果相同
print(a * b) # 与multiply结果相同