0
0
NumPydata~20 mins

Profiling NumPy operations - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of timing a NumPy operation with %timeit
What is the output type when you run the following code in a Jupyter notebook cell?

import numpy as np
arr = np.random.rand(1000, 1000)
%timeit np.dot(arr, arr)
NumPy
import numpy as np
arr = np.random.rand(1000, 1000)
%timeit np.dot(arr, arr)
AIt returns a NumPy array with the dot product result.
BIt prints the average execution time of the dot product operation multiple times.
CIt raises a SyntaxError because %timeit is invalid syntax in Python scripts.
DIt returns the shape of the resulting array from the dot product.
Attempts:
2 left
💡 Hint
Remember that %timeit is a magic command in Jupyter to measure execution time.
data_output
intermediate
1:30remaining
Result of profiling a NumPy sum operation with time.perf_counter
What is the value of the variable duration after running this code?

import numpy as np
import time
arr = np.arange(1000000)
start = time.perf_counter()
s = np.sum(arr)
end = time.perf_counter()
duration = end - start
print(round(duration, 5))
NumPy
import numpy as np
import time
arr = np.arange(1000000)
start = time.perf_counter()
s = np.sum(arr)
end = time.perf_counter()
duration = end - start
print(round(duration, 5))
AA small float number representing seconds, typically less than 0.01.
BThe sum of numbers from 0 to 999999, which is 499999500000.
CAn integer representing the number of elements in the array, 1000000.
DA string showing the start and end times concatenated.
Attempts:
2 left
💡 Hint
The variable duration measures elapsed time, not the sum value.
🔧 Debug
advanced
1:30remaining
Identify the error in profiling a NumPy operation
What error will this code raise when run in a standard Python script (not Jupyter)?

import numpy as np
arr = np.random.rand(1000, 1000)
%timeit np.linalg.inv(arr)
NumPy
import numpy as np
arr = np.random.rand(1000, 1000)
%timeit np.linalg.inv(arr)
ATypeError because np.linalg.inv expects a 1D array.
BNo error; it runs and prints timing information.
CSyntaxError: invalid syntax because %timeit is a Jupyter magic command.
DValueError because the matrix is singular and cannot be inverted.
Attempts:
2 left
💡 Hint
Consider where %timeit commands are valid.
🚀 Application
advanced
2:00remaining
Choosing the best method to profile a NumPy function in a script
You want to measure how long a NumPy matrix multiplication takes inside a Python script (not Jupyter). Which method is best to get accurate timing?

Options:
NumPy
import numpy as np
import time
arr = np.random.rand(1000, 1000)

# Which timing method to use here?
AUse time.perf_counter() before and after the operation to measure elapsed time.
BUse %timeit magic command around the operation.
CUse print(time.time()) before and after, then subtract.
DUse np.profile() function to get timing.
Attempts:
2 left
💡 Hint
Magic commands like %timeit only work in Jupyter, and np.profile() does not exist.
🧠 Conceptual
expert
2:30remaining
Understanding the impact of array size on NumPy operation profiling
You profile np.dot on arrays of increasing size and notice the time grows roughly with the cube of the dimension (n). Why does the time complexity behave like this for matrix multiplication?
ABecause the arrays are stored inefficiently causing slow access.
BBecause NumPy uses a recursive algorithm that doubles time each step.
CBecause the time depends only on the number of rows, not columns.
DBecause matrix multiplication involves n^3 scalar multiplications and additions.
Attempts:
2 left
💡 Hint
Think about how many operations are needed to multiply two n x n matrices.