numpy如何实现算术运算

这篇文章给大家分享的是有关 numpy如何实现算术运算的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

numpy算数运算函数

namedescripe
add(x1, x2[, out])Add arguments element-wise.
reciprocal(x[, out])Return the reciprocal of the argument, element-wise.
negative(x[, out])Numerical negative, element-wise.
multiply(x1, x2[, out])Multiply arguments element-wise.
divide(x1, x2[, out])Divide arguments element-wise.
power(x1, x2[, out])First array elements raised to powers from second array, element-wise.
subtract(x1, x2[, out])Subtract arguments, element-wise.
true_divide(x1, x2[, out])Returns a true division of the inputs, element-wise.
floor_divide(x1, x2[, out])Return the largest integer smaller or equal to the division of the inputs.
fmod(x1, x2[, out])Return the element-wise remainder of division.
mod(x1, x2[, out])Return element-wise remainder of division.
modf(x[, out1, out2])Return the fractional and integral parts of an array, element-wise.
remainder(x1, x2[, out])Return element-wise remainder of division.

1.numpy.add(x1, x2[, out ]) = ufunc‘add’
求和

>>> np.add(1.0, 4.0)5.0>>> x1 = np.arange(9.0).reshape((3, 3))[[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.]]>>> x2 = np.arange(3.0)
[ 0.  1.  2.]
>>> np.add(x1, x2)
array([[ 0., 2., 4.], [ 3., 5., 7.], [ 6., 8., 10.]])

2.numpy.reciprocal(x[, out ]) = ufunc ‘reciprocal’
求倒数

>>> np.reciprocal(2.)
0.5>>> np.reciprocal([1, 2., 3.33])array([ 1. , 0.5 , 0.3003003])

3.numpy.negative(x[, out ]) = ufunc ‘negative’
求相反数

>>> np.negative([1.,-1.])array([-1., 1.])

4.numpy.multiply(x1, x2[, out ]) = ufunc ‘multiply’
求积

>>> np.multiply(2.0, 4.0)8.0>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.multiply(x1, x2)
array([[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.]])

5.numpy.divide(x1, x2[, out ]) = ufunc ‘divide’
求商

>>> np.divide(2.0, 4.0)0.5>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.divide(x1, x2)
array([[ NaN, 1. , 1. ], [ Inf, 4. , 2.5], [ Inf, 7. , 4. ]])

numpy.true_divide(x1, x2[, out ]) = ufunc ‘true_divide’

>>> x = np.arange(5)
>>> np.true_divide(x, 4)array([ 0. , 0.25, 0.5 , 0.75, 1. ])
>>> x/4array([0, 0, 0, 0, 1])
>>> x//4array([0, 0, 0, 0, 1])

numpy.floor_divide(x1, x2[, out ]) = ufunc ‘floor_divide’

>>> np.floor_divide(7,3)
2
>>> np.floor_divide([1., 2., 3., 4.], 2.5)array([ 0., 0., 1., 1.])

6.numpy.power(x1, x2[, out ]) = ufunc ‘power’
求幂

>>> x1 = range(6)>>> x1
[0, 1, 2, 3, 4, 5]>>> np.power(x1, 3)
array([ 0, 1, 8, 27, 64, 125])>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]>>> np.power(x1, x2)
array([ 0., 1., 8., 27., 16., 5.])

7.numpy.subtract(x1, x2[, out ]) = ufunc ‘subtract’
求差

>>> np.subtract(1.0, 4.0)
-3.0>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.subtract(x1, x2)
array([[ 0., 0., 0.], [ 3., 3., 3.], [ 6., 6., 6.]])

8.numpy.fmod(x1, x2[, out ]) = ufunc ‘fmod’
求余

>>> np.fmod([-3, -2, -1, 1, 2, 3], 2)
array([-1, 0, -1, 1, 0, 1])
>>> np.remainder([-3, -2, -1, 1, 2, 3], 2)
array([1, 0, 1, 1, 0, 1])
>>> np.fmod([5, 3], [2, 2.])
array([ 1., 1.])
>>> a = np.arange(-3, 3).reshape(3, 2)
>>> a
array([[-3, -2], [-1, 0], [ 1, 2]])
>>> np.fmod(a, [2,2])
array([[-1, 0], [-1, 0], [ 1, 0]])

numpy.mod(x1, x2[, out ]) = ufunc ‘remainder’

>>> np.remainder([4, 7], [2, 3])array([0, 1])
>>> np.remainder(np.arange(7), 5)array([0, 1, 2, 3, 4, 0, 1])

numpy.remainder(x1, x2[, out ]) =

>>> np.remainder([4, 7], [2, 3])array([0, 1])
>>> np.remainder(np.arange(7), 5)array([0, 1, 2, 3, 4, 0, 1])

9.numpy.modf(x[, out1, out2 ]) = ufunc ‘modf’
求整,求小数

>>> np.modf([0, 3.5])
(array([ 0. , 0.5]), array([ 0., 3.]))
>>> np.modf(-0.5)
(-0.5, -0)

感谢各位的阅读!关于“ numpy如何实现算术运算”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接