What is the numpy.cos() function in Numpy?

Overview

The numpy.cos() function in NumPy returns an array’s cosine element-wise.

Syntax

numpy.cos(x) 

Parameter value

The numpy.cos() function takes the parameter value x which represents the input array in radians.

Return value

The numpy.cos() function returns the corresponding cosine values of an array’s elements.

Example

import numpy as np
# creating an array
x = np.array([[1.5, 0], [0.5, 2.5]])
# taking the cosine element-wise
myarray = np.cos(x)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array x using the array() method.
  • Line 7: We implement the np.cos() function on the array. The result is assigned to a variable, myarray.
  • Line 9: We print the myarray variable.

Free Resources