- 函数作用
np.multipy(a,b)函数是进行一种乘法运算,a,b对应的位置相乘,得到和a一样的输出size。其中a,b也可以是常数。 - 代码示例
>>> import numpy as np
>>> np.multiply(list(range(3)),10)
array([ 0, 10, 20])
>>> a=np.array([1,2,3,4]) #定义两个数组
>>> b=np.array([1,2,4])
>>> np.multiply(a,10) #数组a和常数相乘
array([10, 20, 30, 40])
>>> np.multiply(a,b) #两个不同size的数组相乘会报错,没有广播机制。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (4,) (3,)
>>> np.multiply(b,b) #这里是对应位置相乘
array([ 1, 4, 16])
>>> np.multiply(a,a)
array([ 1, 4, 9, 16])
>>> np.multiply(2,a) #au也可以是常数
array([2, 4, 6, 8])
>>> np.multiply(2,2) #两个数都可以是常数
4