Nmupy中np.dot、np.multiply、* 的区别

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结果相同
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值