📜  Python中的numpy.square

📅  最后修改于: 2020-06-17 04:38:06             🧑  作者: Mango

numpy.square(arr, out = None, ufunc ‘square’) : 此数学函数可帮助用户计算数组中每个元素的平方值。

参数:

arr:[array_like]输入数组或对象他们的元素,我们需要平方。

返回:

一个具有每个数组平方值的数组。

代码1:

# 解释Square()函数的Python程序 
  
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:

# 解释Square()函数的Python程序 
  
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()的图形表示

# 解释Square()函数的Python程序 
  
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.]