如何计算给定NumPy数组中所有元素的数值负值
在这篇文章中,我们将看到如何计算给定NumPy数组中所有元素的负值。所以,负值实际上是指与任何数字相加后成为0的数字。
示例:
如果我们把一个数字定为4,那么-4就是它的负数,因为当我们把-4加到4的时候,得到的总和是0。 现在让我们再举一个例子,假设我们把一个数字-6加上+6,那么总和就是0,因此+6是-6的负值。
A = [1,2,3,-1,-2,-3,0] So, the negative value of A is A'=[-1,-2,-3,1,2,3,0].
因此,为了找到一个元素的数字负值,我们必须使用NumPy库的numpy.negative()函数。
语法: numpy.negative(arr, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘negative’)
返回: [ndarray or scalar] 返回的数组或标量 = -(input arr or scalar )
现在,让我们看看这些例子。
示例 1:
# importing library import numpy as np # creating a array x = np.array([-1, -2, -3, 1, 2, 3, 0]) print("Printing the Original array:", x) # converting array elements to # its corresponding negative value r1 = np.negative(x) print("Printing the negative value of the given array:", r1)
输出:
Printing the Original array: [-1 -2 -3 1 2 3 0] Printing the negative value of the given array: [ 1 2 3 -1 -2 -3 0]
示例 2:
# importing library import numpy as np # creating a numpy 2D array x = np.array([[1, 2], [2, 3]]) print("Printing the Original array Content:\n", x) # converting array elements to # its corresponding negative value r1 = np.negative(x) print("Printing the negative value of the given array:\n", r1)
输出:
Printing the Original array Content: [[1 2] [2 3]] Printing the negative value of the given array: [[-1 -2] [-2 -3]]
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com