0
0
NumPydata~10 mins

Profiling NumPy operations - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module used for profiling NumPy operations.

NumPy
import [1]
Drag options to blanks, or click blank then click option'
Amatplotlib
Bpandas
CcProfile
Dtimeit
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like pandas or matplotlib.
Using timeit instead of cProfile for detailed profiling.
2fill in blank
medium

Complete the code to start profiling a function named compute_array.

NumPy
profiler = cProfile.Profile()
profiler.[1]('compute_array()')
profiler.print_stats()
Drag options to blanks, or click blank then click option'
Arun
Brunctx
Cenable
Ddisable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'enable' or 'disable' instead of 'run'.
Using 'runctx' which requires extra context parameters.
3fill in blank
hard

Fix the error in the code to measure execution time of a NumPy operation using timeit.

NumPy
import numpy as np
import timeit

arr = np.arange(1000)
time = timeit.timeit(lambda: np.[1](arr), number=1000)
print(time)
Drag options to blanks, or click blank then click option'
Aappend
Bsort
Cadd
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' which requires two arrays.
Using 'append' which is not a NumPy function.
Using 'sort' which is a method, not a function.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that profiles functions with timeit and filters results greater than 0.001 seconds.

NumPy
functions = [np.sum, np.mean, np.std]
results = {func.__name__: timeit.timeit(lambda: func(np.arange(1000)), number=1000) for func in functions if [1] [2] 0.001}
Drag options to blanks, or click blank then click option'
Atimeit.timeit(lambda: func(np.arange(1000)), number=1000)
B>
C<
Dfunc.__name__
Attempts:
3 left
💡 Hint
Common Mistakes
Using function name instead of timing expression in the first blank.
Using '<' instead of '>' for filtering.
5fill in blank
hard

Fill all three blanks to create a profiling dictionary that stores function names, their execution times, and filters times less than 0.005 seconds.

NumPy
functions = [np.min, np.max, np.median]
profile = [1]: [2] for func in functions if [3] < 0.005
Drag options to blanks, or click blank then click option'
Afunc.__name__
Btimeit.timeit(lambda: func(np.arange(1000)), number=1000)
Dfunc
Attempts:
3 left
💡 Hint
Common Mistakes
Using function object instead of function name as key.
Using function object instead of timing expression for value or condition.
Mixing up the order of keys and values.