0
0
NumPydata~20 mins

Why NumPy performance matters - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy Performance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of NumPy array operation speed test

What is the output of this code that compares the time taken by a Python list and a NumPy array to multiply elements by 2?

NumPy
import time
import numpy as np

size = 1000000

# Python list multiplication
lst = list(range(size))
start_lst = time.time()
lst2 = [x * 2 for x in lst]
end_lst = time.time()

# NumPy array multiplication
arr = np.arange(size)
start_arr = time.time()
arr2 = arr * 2
end_arr = time.time()

print(f"List time: {end_lst - start_lst:.5f} seconds")
print(f"NumPy time: {end_arr - start_arr:.5f} seconds")
AList time is much greater than NumPy time
BList time is about the same as NumPy time
CNumPy time is much greater than list time
DBoth times are zero
Attempts:
2 left
💡 Hint

Think about how NumPy uses optimized C code for operations.

data_output
intermediate
1:30remaining
Shape and size after NumPy operation

What is the shape and size of the resulting array after this operation?

NumPy
import numpy as np

arr = np.arange(12).reshape(3,4)
result = arr * 3
print(result.shape, result.size)
A(4, 3) 12
B(12,) 3
C(3, 4) 12
D(3, 4) 3
Attempts:
2 left
💡 Hint

Multiplying by 3 does not change shape or size.

🔧 Debug
advanced
1:30remaining
Identify the error in NumPy broadcasting

What error does this code raise?

NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([[1, 2], [3, 4]])
result = arr1 + arr2
AValueError: operands could not be broadcast together with shapes (3,) (2,2)
BTypeError: unsupported operand type(s) for +: 'list' and 'ndarray'
CIndexError: index out of bounds
DNo error, result is a 2x3 array
Attempts:
2 left
💡 Hint

Check if shapes can be broadcasted for addition.

🚀 Application
advanced
1:30remaining
Choosing NumPy for large data processing

Why is NumPy preferred over pure Python lists for large numerical data processing?

ANumPy supports strings better than lists
BNumPy automatically parallelizes code on multiple CPUs
CPython lists are immutable, NumPy arrays are mutable
DNumPy uses less memory and faster operations due to vectorization
Attempts:
2 left
💡 Hint

Think about memory and speed advantages.

🧠 Conceptual
expert
2:00remaining
Why does NumPy speed matter in real-world data science?

Which statement best explains why NumPy's performance is critical in data science?

ANumPy's speed allows it to replace databases for storing data
BFaster NumPy operations allow processing large datasets efficiently, enabling quicker insights and model training
CNumPy speed is only important for graphics rendering, not data science
DNumPy speed is irrelevant because Python is slow anyway
Attempts:
2 left
💡 Hint

Consider the impact of speed on handling big data and machine learning.