Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Mathematical Operations | Math with NumPy
course content

Зміст курсу

Ultimate NumPy

Basic Mathematical OperationsBasic Mathematical Operations

Now that you're familiar with the concept of broadcasting, let’s discuss some basic mathematical operations in NumPy.

Scalar Operations

Remember, broadcasting allows you to perform mathematical operations between two arrays of compatible shapes or between an array and a scalar.

Let’s first look at an example with scalars:

As you can see, each operation is performed element-wise on the array. Essentially, a scalar is broadcast to an array of the same shape as our original array, where all the elements are the same number. Therefore, the operation is performed on every pair of corresponding elements of the two arrays.

Operations Between Two Arrays

If the shapes of two arrays are compatible, broadcasting is performed if needed, and once again, an operation is performed element-wise:

Division, subtraction, and other arithmetic operations work in a similar fashion. Here is an example where the second array is broadcast:

arr_2 is broadcast to a 2D array with two identical rows, each containing the array [5, 6, 7].

Завдання

Calculate the quarterly revenue growth for each product in percent (each row of a 2D array contains quarterly sales for a certain product).

Все було зрозуміло?

Секція 4. Розділ 2
toggle bottom row
course content

Зміст курсу

Ultimate NumPy

Basic Mathematical OperationsBasic Mathematical Operations

Now that you're familiar with the concept of broadcasting, let’s discuss some basic mathematical operations in NumPy.

Scalar Operations

Remember, broadcasting allows you to perform mathematical operations between two arrays of compatible shapes or between an array and a scalar.

Let’s first look at an example with scalars:

As you can see, each operation is performed element-wise on the array. Essentially, a scalar is broadcast to an array of the same shape as our original array, where all the elements are the same number. Therefore, the operation is performed on every pair of corresponding elements of the two arrays.

Operations Between Two Arrays

If the shapes of two arrays are compatible, broadcasting is performed if needed, and once again, an operation is performed element-wise:

Division, subtraction, and other arithmetic operations work in a similar fashion. Here is an example where the second array is broadcast:

arr_2 is broadcast to a 2D array with two identical rows, each containing the array [5, 6, 7].

Завдання

Calculate the quarterly revenue growth for each product in percent (each row of a 2D array contains quarterly sales for a certain product).

Все було зрозуміло?

Секція 4. Розділ 2
toggle bottom row
some-alt