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


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

numpy.sqrt(array[, out])函數用於按元素確定數組的正平方根。

用法:  numpy.sqrt()

參數:
array :[數組]必須確定其平方根的輸入值。
out:[ndarray,可選]放置結果的備用數組對象;如果提供,它必須具有與arr相同的形狀。


返回:[ndarray]返回數組中數字的平方根。

代碼1:

# Python program explaining 
# numpy.sqrt() method  
  
# importing numpy 
import numpy as geek  
  
# applying sqrt() method on integer numbers  
arr1 = geek.sqrt([1, 4, 9, 16]) 
arr2 = geek.sqrt([6, 10, 18]) 
  
print("square-root of an array1 :", arr1) 
print("square-root of an array2 :", arr2)
輸出:
square-root of an array1 : [ 1.  2.  3.  4.]
square-root of an array2 : [ 2.44948974  3.16227766  4.24264069]


代碼2:

# Python program explaining 
# numpy.sqrt() method  
  
# importing numpy 
import numpy as geek  
  
# applying sqrt() method on complex numbers 
arr = geek.sqrt([4, -1, -5 + 9J]) 
  
print("square-root of an array :", arr)
輸出:
square-root of an array : [ 2.00000000+0.j  0.00000000+1.j  1.62721083+2.76546833j]


代碼3:

# Python program explaining 
# numpy.sqrt() method  
  
# importing numpy 
import numpy as geek  
  
# applying sqrt() method on negative element of real numbers  
arr = geek.sqrt([-4, 5, -6]) 
  
print("square-root of an array :", arr)
輸出:
RuntimeWarning:invalid value encountered in sqrt
square-root of an array : [ nan  2.23606798  nan]


相關用法


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