Python 中的 numpy.absolute()

| | | | | | | | | | | | | | | |

參數:

arr: [array_like] 輸入數組或對象的元素,我們需要測試。 

返回:

一個數組,每個數組的絕對值。 

代碼#1:工作


# Python 程序解釋
# absolute() 函數


import numpy as np

arr1 = [ 1 , - 3 , 15 , - 466 ]

print ( "arr1 的絕對值:" ,

np.absolute (arr1))

arr2 = [ 23 , - 56 ]

print ( " 絕對值arr2: " ,

< a href="https://python.engineering/numpy-absolute-python/">np.absolute (arr2))

輸出:

arr1 的絕對值:[1 3 15 466] arr2 的絕對值:[23 56] 

代碼#2:工作帶複數的 g


# Python程序解釋
#絕對()函數


import numpy as np

a = 4 + 3j

print ( "絕對 (4 + 3j):" ,

np.absolute (a))

b = 16 + 13j

打印 ( "絕對值 (16 + 13j):" ,

np.absolute (b))

輸出:

絕對值(4 + 3j):5.0 絕對值(16 + 13j):20.6155281281 

代碼# 3:numpy.absolute()的圖形表示


#Python程序解釋
# absolute() 函數

import numpy as np

import matplotlib.pyplot as plt


一個 = np.linspace (start = - 5 , 停止 = 5 ,

num = 6 ,端點 = True )


print ( "圖形表示:" ,

np.absolute< /a> (a))


plt.title ( "blue: 帶絕對紅色:沒有絕對的 " )

plt.plot (a, < a href="https://python.engineering/numpy-absolute-python/">np.absolute (a) )


plt.plot (a, a, color = `red` )

plt.show ()

輸出:

圖形表示:[5. 3. 1. 1. 3. 5.] 

鏈接:
https:// /docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.absolute.html
,