numpy 算术运算(Arithmetic operations)

本文详细介绍了NumPy库中各种基本数学运算函数的功能与用法,包括加减乘除、求幂、取余等操作,并提供了丰富的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/4
array([0, 0, 0, 0, 1])
>>> x//4
array([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)
请你使用python代码来实现下列功能: 首先,请你使用pyqt5模块来生成一个可视化界面,要求这个界面的大小适中,并且可以 显示出所有可以实现功能的按钮,并且要求这个界面精美大气 然后你还要实现数字图像处理有关的功能 这些功能分为三类:基本操作类,图像预处理类,形态学图像操作类、以及图像分割类 基本操作类的功能包括有:图像读取,还要有一个图像查看器,以便于查看对读取图像执行操作后的结果,还要有一个撤销操作的功能,使用该功能可以返回上一步操作,还有图像的算术运算,包括加法、减法、乘法、除法运算、图像的逻辑运算,包括与运算、或运算、非运算、异或运算,注意图像的算术运算和图像的逻辑运算需要读取两个图片来操作,这些功能请你采用基本算法来实现 图像预处理类的功能包括有:图像的灰度化转换,图像的去噪操作,图像的亮度调节,图像的对比度调节,(注意亮度和对比度调节时需要用滑块来操作,为了防止图像调节过度失真,你可采用多线程来处理)图像的滤波变换(可变换的滤波包括空域平滑滤波、空域锐化滤波)这些功能采用基本算法来实现 形态学图像操作类的功能包括有:腐蚀与膨胀,开运算与闭运算、边界提取、区域填充调用opencv库来实现 图像分割类的功能包括有:阈值分割、基于边界的图像分割、基于区域的图像分割、提取图像的全局特征(包括形状、颜色、灰度、周长)、提取图像的局部特征(包括斑点、角点、边缘、Harris描述子、sift描述字),图像分割类的功能也调用opencv库来实现
最新发布
04-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值