Python numpy.absolute()
numpy.absolute(arr, out = None, ufunc ‘absolute’) :这个数学函数帮助用户计算每个元素的绝对值。对于复杂的输入,a + ib,绝对值是
参数 :
arr : [array_like] 输入数组或对象的元素,我们需要测试。
返回 :
一个带有每个数组绝对值的数组。
**代码 #1 : **
# Python program explaining
# absolute () function
import numpy as np
arr1 = [1, -3, 15, -466]
print ("Absolute Value of arr1 : \n",
np.absolute(arr1))
arr2 = [23 , -56]
print ("\nAbsolute Value of arr2 : \n",
np.absolute(arr2))
输出 :
Absolute Value of arr1 :
[ 1 3 15 466]
Absolute Value of arr2 :
[23 56]
代码#2:与复数打交道
# Python program explaining
# absolute () function
import numpy as np
a = 4 + 3j
print("Absolute(4 + 3j) : ",
np.absolute(a))
b = 16 + 13j
print("\nAbsolute value(16 + 13j) : ",
np.absolute(b))
输出 :
Absolute(4 + 3j) : 5.0
Absolute value(16 + 13j) : 20.6155281281
代码#3:numpy.absolute()的图形表示法
# Python program explaining
# absolute () function
import numpy as np
import matplotlib.pyplot as plt
a = np.linspace(start = -5, stop = 5,
num = 6, endpoint = True)
print("Graphical Representation : \n",
np.absolute(a))
plt.title("blue : with absolute\nred : without absolute")
plt.plot(a, np.absolute(a))
plt.plot(a, a, color = 'red')
plt.show()
输出 :
Graphical Representation :
[ 5. 3. 1. 1. 3. 5.]