The numpy.cos()
method in Python calculates the ratio of the length of the side nearest to the angle to the length of the hypotenuse. This function returns the cosine of a number. To be more precise, it returns the cosine of a radian.
A visual representation of the cosine () is as follows.
numpy.cos(x, out=None)
x
: This method takes an array of radian numbers as an input.out=None
: This is an optional parameter in the numpy.cos()
function. This could be ndarray
, None
, or tuple
of the ndarray
. This parameter identifies a location where the solution or output is stored.This method returns an ndarray
containing the cosine values of radian numbers from the input. The output may be scalar if the input is scalar.
import numpy as np# Creating an arrayarray = np.array([0, np.pi/2, np.pi])#Calculating the cosine of the valuesresult = np.cos(array)#Using the out parameternew_result = np.array([0, np.pi/2, np.pi])np.cos(array, out = new_result)print("Cosine of values : ", result)print("New Result array : ", new_result)
np.array()
method.array
.new_result
array using the out
parameter.Note: Providing the
out
parameter in thenumpy.cos()
function will store the result in theout
parameter, and return a reference to it.
Free Resources