Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
NumPy arccos()
In this tutorial, you will learn about the numpy.arccos() method with the help of examples.
The arccos() method computes the arccosine (inverse cosine) of each element in an array.
Example
import numpy as np
# create an array of values between -1 and 1
values = np.array([-1, -0.5, 0, 0.5, 1])
# calculate the inverse cosine of each value
inverseCosines = np.arccos(values)
print(inverseCosines)
# Output: [3.14159265 2.0943951 1.57079633 1.04719755 0. ]
arccos() Syntax
The syntax of arccos() is:
numpy.arccos(x, out = None, where = True, dtype = None)
arccos() Arguments
The arccos() method takes following arguments:
x- an input arrayout(optional) - the output array where the result will be storedwhere(optional) - a boolean array or condition indicating where to compute the arccosinedtype(optional) - data type of the output array
arccos() Return Value
The arccos() method returns an array with the corresponding inverse cosine values.
Example 1: Use of out and where in arccos()
import numpy as np
# create an array of values between -0.5 and 0.5
values = np.array([-0.5, -0.2, 0, 0.2, 0.5])
# create an array of zeros with the same shape as values
result = np.zeros_like(values, dtype = float)
# calculate inverse cosine where values >= 0 and store in result.
np.arccos(values, out = result, where = (values >= 0))
print(result)
Output
[0. 0. 1.57079633 1.36943841 1.04719755]
Here,
out = resultspecifies that the output of thenp.arccos()function should be stored in the result arraywhere=(values >= 0)specifies that the inverse cosine operation should only be applied to elements in values that are greater than or equal to 0.
Example 2: Use of dtype Argument in arccos()
import numpy as np
# create an array of values
values = np.array([0, 1, -1])
# calculate the inverse cosine of each value with float data type
arccos Float = np.arccos(values, dtype = float)
print("Inverse Cosine with 'float' dtype:")
print(arccos Float)
# calculate the inverse cosine of each value with complex data type
arccosComplex = np.arccos(values, dtype = complex)
print("\nInverse Cosine with 'complex' dtype:")
print(arccosComplex)
Output
Inverse Cosine with 'float' dtype: [1.57079633 0. 3.14159265] Inverse Cosine with 'complex' dtype: [1.57079633-0.j 0. -0.j 3.14159265-0.j]
Here, by specifying the desired dtype, we can control the data type of the output array according to our specific requirements.
Note: To learn more about the dtype argument, please visit NumPy Data Types.