當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.square()用法及代碼示例

numpy.square(arr,out = None,ufunc'square'):此數學函數可幫助用戶計算數組中每個元素的平方值。

參數:

arr  : [array_like] Input array or object
          whose elements, we need to square.

返回:


An array with square value of each array.  


代碼1:工作

# Python program explaining 
# square () function 
  
import numpy as np 
  
arr1 = [1, -3, 15, -466] 
print ("Square Value of arr1 : \n", np.square(arr1)) 
  
arr2 = [23 ,-56] 
print ("\nSquare Value of arr2 : ", np.square(arr2))

輸出:

Square Value of arr1 : 
 [     1      9    225 217156]

Square Value of arr2 :  [ 529 3136]


代碼2:使用複數

# Python program explaining 
# square () function 
  
import numpy as np 
  
a = 4 + 3j
print("Square(4 + 3j) : ", np.square(a)) 
  
b = 16 + 13j
print("\nSquare value(16 + 13j) : ", np.square(b))

輸出:

Square(4 + 3j) :  (7+24j)

Square value(16 + 13j) :  (87+416j)


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

# Python program explaining 
# square () 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.square(a)) 
  
plt.title("blue : with square\nred : without square") 
plt.plot(a, np.square(a)) 
  
plt.plot(a, a, color = 'red') 
plt.show()

輸出:

Graphical Representation : 
 [ 25.   9.   1.   1.   9.  25.]


參考文獻:
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.absolute.html



相關用法


注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 numpy.square() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。