Python numpy degrees

The Python numpy degrees is a trigonometric function to convert the angles from radian to degrees. For instance, π radian = 180 and 2π = 360 degrees.

Syntax

The syntax of this mathematical method is

numpy.degrees(a, axis = None, dtype = None, out = None)

Python numpy degrees Example

In this example, we declared two one-dimensional ndarray of positive and negative numbers from -π to π. Here, np.pi returns the π value. Next, this method returns the degrees for the given radians.

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.]

Two-Dimensional example

In this program, we will declare a two-dimensional array of random values using the randn and arange method and find its degrees.

In the last line, we used out and dtype arguments. The dtype is to change the data type, and the out argument is to save the result in a separate array.

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   ]