0
0
NumPydata~20 mins

np.full() for custom-filled arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.full() Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.full() with integer fill value
What is the output of this code?
import numpy as np
arr = np.full((2, 3), 7)
print(arr)
NumPy
import numpy as np
arr = np.full((2, 3), 7)
print(arr)
A
[[7 7 7]
 [7 7 7]]
B
[[7 7 7 7]
 [7 7 7 7]]
C
[[7 7]
 [7 7]
 [7 7]]
D
[[0 0 0]
 [0 0 0]]
Attempts:
2 left
💡 Hint
Look at the shape argument (2, 3) and the fill value 7.
data_output
intermediate
2:00remaining
Shape and dtype of np.full() array
What is the shape and data type of the array created by this code?
import numpy as np
arr = np.full((4,), 3.14, dtype=np.float32)
print(arr.shape, arr.dtype)
NumPy
import numpy as np
arr = np.full((4,), 3.14, dtype=np.float32)
print(arr.shape, arr.dtype)
A(4,) int64
B(4, 1) float64
C(1, 4) float32
D(4,) float32
Attempts:
2 left
💡 Hint
Check the shape tuple and the dtype argument.
🔧 Debug
advanced
2:00remaining
Identify the error in np.full() usage
What error does this code raise?
import numpy as np
arr = np.full(3, [1, 2])
print(arr)
NumPy
import numpy as np
arr = np.full(3, [1, 2])
print(arr)
AValueError: could not broadcast input array from shape (2,) into shape ()
BNo error, prints array with shape (3, 2)
CSyntaxError: invalid syntax
DTypeError: fill_value must be a scalar
Attempts:
2 left
💡 Hint
np.full expects a scalar fill value, but here a list is given.
🚀 Application
advanced
2:00remaining
Creating a masked array with np.full()
You want to create a 3x3 numpy array filled with -1 to mark missing data. Which code correctly creates this array with integer type?
Anp.full((3, 3), '-1')
Bnp.full((3, 3), -1, dtype=int)
Cnp.full([3, 3], -1.0, dtype=int)
Dnp.full((3, 3), -1, dtype='float')
Attempts:
2 left
💡 Hint
Check the shape format and dtype for integer type.
🧠 Conceptual
expert
3:00remaining
Memory behavior of np.full() with mutable fill value
What happens if you run this code?
import numpy as np
arr = np.full((2,), [0])
arr[0].append(1)
print(arr)
NumPy
import numpy as np
arr = np.full((2,), [0])
arr[0].append(1)
print(arr)
ARaises TypeError because fill_value is a list
BFirst element is [0, 1], second is [0] because lists are copied
CBoth elements show [0, 1] because the same list object is used
DPrints [[0] [0]] unchanged
Attempts:
2 left
💡 Hint
Think about how np.full handles mutable objects as fill_value.