🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.cos()

AI & DATA SCIENCE // np-cos

np.cos() computes the trigonometric cosine of each element in an array, with input angles interpreted in radians.

Syntax

np.cos(arr)

Deep Dive Course

np.cos() behaves exactly like np.sin() in terms of expecting radians and being bounded between -1 and 1, but computes the cosine instead of the sine — the two are phase-shifted versions of the same underlying wave, with cos(x) equal to sin(x + pi/2). Together, sin and cos are the building blocks for representing rotations, oscillations, and periodic signals, and they satisfy the identity sin(x) squared plus cos(x) squared equals 1 for any real x.

1Understanding np.cos()

np.cos() behaves exactly like np.sin() in terms of expecting radians and being bounded between -1 and 1, but computes the cosine instead of the sine — the two are phase-shifted versions of the same underlying wave, with cos(x) equal to sin(x + pi/2). Together, sin and cos are the building blocks for representing rotations, oscillations, and periodic signals, and they satisfy the identity sin(x) squared plus cos(x) squared equals 1 for any real x.

💡

Just like np.sin(), np.cos() expects radians, not degrees — use np.radians() to convert degree values first if that's the form your input data is in.

editor.html
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.cos(angles))
localhost:3000

2Practical Example

Here is a real-world application of np.cos() showing how it is used in production NumPy code.

editor.html
import numpy as np

theta = np.linspace(0, 2 * np.pi, 4, endpoint=False)
x = np.cos(theta)
y = np.sin(theta)
print(np.round(x, 2))
print(np.round(y, 2))
localhost:3000

3Best Practices

Follow these guidelines when working with np.cos():

1. Convert degree values to radians with np.radians() before calling np.cos(), the same as for np.sin()

2. Use np.cos() and np.sin() together for rotation calculations or generating circular/periodic coordinate data

3. Use np.isclose() rather than == when validating trigonometric identities or expected results numerically, due to floating-point precision

⚠️

Tip: Just like np.sin(), np.cos() expects radians, not degrees — use np.radians() to convert degree values first if that's the form your input data is in.

editor.html
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.cos(angles))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.cos(angles))
Example 02Advanced Example
import numpy as np

theta = np.linspace(0, 2 * np.pi, 4, endpoint=False)
x = np.cos(theta)
y = np.sin(theta)
print(np.round(x, 2))
print(np.round(y, 2))

Best Practices

  • Convert degree values to radians with np.radians() before calling np.cos(), the same as for np.sin()
  • Use np.cos() and np.sin() together for rotation calculations or generating circular/periodic coordinate data
  • Use np.isclose() rather than == when validating trigonometric identities or expected results numerically, due to floating-point precision

Interview Question

How are np.sin() and np.cos() related mathematically, and how does that show up in the code2 example generating points around a circle?

Hint: Think about the standard parametric equations for a circle.

sin and cos are the same wave shifted by a quarter cycle, or pi/2 radians, relative to each other, and together they satisfy the identity that sin squared plus cos squared always equals 1 for any input. This identity is exactly what makes the pair (cos(theta), sin(theta)) trace out points on a unit circle as theta varies — which is why generating x-coordinates from cos() and y-coordinates from sin() over an evenly spaced set of angles, as in the example, produces points evenly distributed around a circle.

Exercises

MediumPractice using np.cos() in a real scenario.
View Solution
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.cos(angles))

Frequently Asked Questions

How are np.sin() and np.cos() related mathematically, and how does that show up in the code2 example generating points around a circle?

sin and cos are the same wave shifted by a quarter cycle, or pi/2 radians, relative to each other, and together they satisfy the identity that sin squared plus cos squared always equals 1 for any input. This identity is exactly what makes the pair (cos(theta), sin(theta)) trace out points on a unit circle as theta varies — which is why generating x-coordinates from cos() and y-coordinates from sin() over an evenly spaced set of angles, as in the example, produces points evenly distributed around a circle.

Related Functions

np-sinnp-tannp-linspace