Python numpy 的 degrees 函数是一个三角函数,用于将角度从弧度转换为度。例如,π 弧度 = 180 度,2π 弧度 = 360 度。
语法
此数学方法的语法是
numpy.degrees(a, axis = None, dtype = None, out = None)
Python numpy degrees 示例
在此示例中,我们声明了两个一维 ndarray,包含从 -π 到 π 的正数和负数。其中,np.pi 返回 π 的值。接下来,此方法为给定的弧度返回对应的度数。
import numpy as np p = np.pi a = np.array([0, -1, 0.5, -0.5, 1]) print(a) b = np.degrees(a) print(b) c = np.array([2* p, p, p / 2, p / 3, p / 4, p / 6]) print(c) d = np.degrees(c) print(d)
[ 0. -1. 0.5 -0.5 1. ]
[ 0. -57.29577951 28.64788976 -28.64788976 57.29577951]
[6.28318531 3.14159265 1.57079633 1.04719755 0.78539816 0.52359878]
[360. 180. 90. 60. 45. 30.]
二维示例
在此 程序 中,我们将使用 randn 和 arange 方法声明一个二维 数组,其中包含随机值,并计算其度数。
在最后一行,我们使用了 out 和 dtype 参数。dtype 用于更改数据类型,out 参数用于将结果保存在单独的数组中。
import numpy as np a = np.random.randn(2, 3) print(a) b = np.degrees(a) print(b) c = np.arange(6) * 0.7 print(c) d = np.degrees(c) print(d) x = np.arange(6.) np.degrees(c, out = x, dtype = np.float16) print(x)
[[ 0.32940705 -0.29039143 0.13304635]
[-0.68501219 0.91421034 1.02088745]]
[[ 18.87363379 -16.63820344 7.62299427]
[-39.24830713 52.38039389 58.4925421 ]]
[0. 0.7 1.4 2.1 2.8 3.5]
[ 0. 40.10704566 80.21409132 120.32113698 160.42818264 200.5352283 ]
[ 0. 40.125 80.25 120.3125 160.5 200.5 ]