From the course: Python Data Analysis

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Doing math with NumPy arrays

Doing math with NumPy arrays - Python Tutorial

From the course: Python Data Analysis

Doing math with NumPy arrays

- [Instructor] NumPy is very good at math. While interpreted languages such as Python have a reputation of being slow, we can do math very fast in Python as long as we write code that operates on entire arrays. That's because the loops that are needed to perform math over the array are then implemented in compiled C within the NumPy library. So how do we do math with arrays? Let's start by creating a one-dimensional vector of equally spaced real values between say, zero and five times pi. We wish to compute the sine of all those values. The regular function in the math library, one, two. Instead, we use the NumPy function of the same name. The result is another NumPy array with the same shape as X. Here's a plot. NumPy has a function for pretty much everything you can find in the standard math library. The NumPy functions are known as universal functions, and they operate element-wise across entire arrays. They include arithmetics, logical operations, trigonometry, hyperbolic…

Contents