0
0
NumPydata~20 mins

np.power() and np.square() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Power and Square Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.power() with array and scalar
What is the output of this code snippet?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.power(arr, 3)
print(result)
A[1 6 9]
B[1 8 27]
C[3 6 9]
D[1 4 9]
Attempts:
2 left
💡 Hint
np.power raises each element to the given power.
Predict Output
intermediate
2:00remaining
Difference between np.square() and np.power()
What is the output of this code?
NumPy
import numpy as np
arr = np.array([2, 4, 6])
result = np.square(arr)
print(result)
A[1 16 36]
B[8 64 216]
C[2 4 6]
D[4 16 36]
Attempts:
2 left
💡 Hint
np.square is like np.power with exponent 2.
data_output
advanced
2:00remaining
Result shape after np.power with broadcasting
What is the shape of the result array after this operation?
NumPy
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([2, 3])
result = np.power(arr1, arr2)
print(result.shape)
A(2, 2)
B(2,)
C(2, 3)
D(3, 2)
Attempts:
2 left
💡 Hint
Broadcasting matches shapes to perform element-wise operations.
🔧 Debug
advanced
2:00remaining
Identify the error in np.power usage
What error does this code raise?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.power(arr, '2')
print(result)
ASyntaxError
BValueError
CTypeError
DNo error, outputs [1 4 9]
Attempts:
2 left
💡 Hint
The exponent must be a number, not a string.
🚀 Application
expert
3:00remaining
Using np.square to compute Euclidean distances
Given two 2D points, which code correctly computes the squared Euclidean distance using np.square?
Anp.sum(np.square(point1 - point2))
Bnp.square(np.sum(point1 - point2))
Cnp.square(point1) - np.square(point2)
Dnp.sum(point1 - point2)**2
Attempts:
2 left
💡 Hint
Squared distance is sum of squared differences of coordinates.