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


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


numpy.cbrt(arr,out = None,ufunc'cbrt'):此數學函數可幫助用戶計算所有x作為數組元素的x的立方根。

參數:

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

返回:


An array with cube root of x for all x i.e. array elements 


代碼1:工作

# Python program explaining 
# cbrt () function 
   
import numpy as np 
   
arr1 = [1, 27000, 64, -1000] 
print ("cbrt Value of arr1 : \n", np.cbrt(arr1)) 
   
arr2 = [1024 ,-128] 
print ("\ncbrt Value of arr2 : ", np.cbrt(arr2))

輸出:

cbrt Value of arr1 : 
 [  1.  30.   4. -10.]

cbrt Value of arr2 :  [ 10.0793684  -5.0396842]


代碼2:使用複數

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

輸出:

TypeError: ufunc 'cbrt' not supported for the input types,
and the inputs could not be safely coerced to any supported
types according to the casting rule ''safe''


代碼3:圖形表示

# Python program explaining 
# cbrt () function 
   
import numpy as np 
import matplotlib.pyplot as plt 
   
a = np.linspace(start = -5, stop = 150, 
                num = 10, endpoint = True) 
                   
print("Graphical Representation : \n", np.cbrt(a)) 
   
plt.title("blue : with cbrt\nred : without cbrt") 
plt.plot(a, np.cbrt(a)) 
   
plt.scatter(a, a, color = 'red') 
plt.show()

輸出:

Graphical Representation : 
 [-1.70997595  2.30347441  3.08793243  3.60027433  3.99768384  4.3287262
  4.61565763  4.87076238  5.10162421  5.31329285]

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



相關用法


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