0
0
NumPydata~15 mins

Profiling NumPy operations - Mini Project: Build & Apply

Choose your learning style9 modes available
Profiling NumPy operations
📖 Scenario: You are working with numerical data using NumPy. You want to understand how long certain NumPy operations take to run. This helps you find which parts of your code are slow and need improvement.
🎯 Goal: Build a simple program that creates a NumPy array, sets a threshold value, measures the time taken to perform an operation on the array, and prints the elapsed time.
📋 What You'll Learn
Create a NumPy array with specific values
Set a threshold variable
Use time.perf_counter() to measure elapsed time of a NumPy operation
Print the elapsed time in seconds
💡 Why This Matters
🌍 Real World
Profiling helps data scientists find slow parts of their code so they can make it faster and more efficient.
💼 Career
Knowing how to measure and improve code performance is important for data scientists working with large datasets and complex calculations.
Progress0 / 4 steps
1
Create a NumPy array
Import NumPy as np and create a NumPy array called data with these exact values: [10, 20, 30, 40, 50].
NumPy
Need a hint?

Use np.array() to create the array with the given list of numbers.

2
Set a threshold value
Create a variable called threshold and set it to the integer 25.
NumPy
Need a hint?

Just assign the number 25 to the variable named threshold.

3
Measure time for a NumPy operation
Import time. Use time.perf_counter() to record the start time in a variable called start_time. Then create a new NumPy array called filtered that contains only the values from data greater than threshold. After that, record the end time in a variable called end_time using time.perf_counter().
NumPy
Need a hint?

Use boolean indexing data[data > threshold] to filter the array.

4
Print the elapsed time
Calculate the elapsed time by subtracting start_time from end_time and store it in a variable called elapsed. Then print the text Elapsed time: followed by the elapsed time rounded to 6 decimal places and the string seconds.
NumPy
Need a hint?

Use an f-string to format the elapsed time with 6 decimal places.