0
0
NumPydata~15 mins

np.einsum() for efficient computation in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Using np.einsum() for Efficient Computation
📖 Scenario: Imagine you work in a small company that analyzes sales data. You have two sets of numbers: the quantity of items sold and the price per item. You want to find the total sales amount for each item quickly and efficiently.
🎯 Goal: You will learn how to use np.einsum() to multiply arrays element-wise and sum over specific axes, which is a fast way to do calculations like total sales.
📋 What You'll Learn
Create two numpy arrays with exact values
Define a configuration variable for the operation
Use np.einsum() with the correct subscripts to compute total sales
Print the resulting array showing total sales per item
💡 Why This Matters
🌍 Real World
In real business scenarios, fast and memory-efficient calculations like total sales help companies make quick decisions.
💼 Career
Data scientists and analysts often use <code>np.einsum()</code> to speed up numerical computations in data processing and machine learning.
Progress0 / 4 steps
1
Create the sales quantity and price arrays
Create a numpy array called quantities with values [2, 3, 5] representing items sold. Also create a numpy array called prices with values [10, 20, 30] representing price per item.
NumPy
Need a hint?

Use np.array() to create arrays with the exact values given.

2
Define the einsum operation string
Create a string variable called operation and set it to 'i,i->' which tells np.einsum() to multiply elements of two 1D arrays and sum the result.
NumPy
Need a hint?

The string 'i,i->' means multiply elements with index i from both arrays and sum all results.

3
Calculate total sales using np.einsum()
Use np.einsum() with the operation string and arrays quantities and prices to calculate the total sales amount. Store the result in a variable called total_sales.
NumPy
Need a hint?

Call np.einsum() with the operation string and the two arrays in order.

4
Print the total sales amount
Print the variable total_sales to display the total sales amount.
NumPy
Need a hint?

Use print(total_sales) to show the result.