0
0
NumPydata~20 mins

Vectorization over loops in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vectorization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of vectorized vs loop sum

What is the output of the following code that sums two arrays using a loop and using vectorization?

NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# Using loop
result_loop = []
for i in range(len(arr1)):
    result_loop.append(arr1[i] + arr2[i])

# Using vectorization
result_vec = arr1 + arr2

print(result_loop)
print(result_vec.tolist())
A[5, 7, 9]\n[4, 6, 8]
B[5, 7, 9]\n[1, 2, 3, 4, 5, 6]
C[5, 7, 9]\n[5, 7, 9]
D[4, 6, 8]\n[5, 7, 9]
Attempts:
2 left
💡 Hint

Think about how element-wise addition works in numpy arrays compared to manual loops.

data_output
intermediate
1:30remaining
Result shape after vectorized operation

Given two numpy arrays a with shape (3, 1) and b with shape (1, 4), what is the shape of the result after a + b?

NumPy
import numpy as np
a = np.array([[1], [2], [3]])
b = np.array([[10, 20, 30, 40]])
c = a + b
print(c.shape)
A(3, 4)
B(3, 1)
C(1, 4)
D(4, 3)
Attempts:
2 left
💡 Hint

Remember numpy broadcasting rules for arrays with different shapes.

🔧 Debug
advanced
1:30remaining
Identify error in vectorized code

What error does the following code raise?

NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = arr + '4'
print(result)
ATypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
BValueError: operands could not be broadcast together with shapes (3,) (1,)
CSyntaxError: invalid syntax
DNo error, output: [5 6 7]
Attempts:
2 left
💡 Hint

Think about adding numbers and strings in numpy arrays.

🚀 Application
advanced
2:30remaining
Vectorized calculation of Euclidean distances

You have two sets of points in 2D space stored as numpy arrays A (shape (m, 2)) and B (shape (n, 2)). Which option correctly computes the matrix of Euclidean distances between each point in A and each point in B using vectorization?

NumPy
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8], [9, 10]])
Anp.sqrt(((A - B) ** 2).sum(axis=1))
Bnp.sqrt(((A[:, None, :] - B[None, :, :]) ** 2).sum(axis=2))
Cnp.linalg.norm(A - B, axis=1)
Dnp.sum(np.abs(A - B), axis=2)
Attempts:
2 left
💡 Hint

Use broadcasting to subtract each point in B from each point in A, then sum squared differences.

🧠 Conceptual
expert
1:30remaining
Why vectorization is faster than loops in numpy?

Which of the following best explains why vectorized operations in numpy are faster than explicit Python loops?

AVectorized operations use GPU by default to speed up calculations.
BVectorized operations use multiple CPU cores automatically to parallelize loops.
CVectorized operations convert data to strings for faster processing.
DVectorized operations use optimized C code and avoid Python-level loops, reducing overhead.
Attempts:
2 left
💡 Hint

Think about what happens under the hood when numpy runs vectorized code.