0
0
NumPydata~20 mins

Why array creation matters in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy array creation with shape and dtype
What is the output of this code snippet creating a numpy array with zeros, shape (2,3), and dtype int?
NumPy
import numpy as np
arr = np.zeros((2,3), dtype=int)
print(arr)
A
[[0 0 0]
 [0 0 0]]
B
[[0. 0. 0.]
 [0. 0. 0.]]
C
[[0 0]
 [0 0]
 [0 0]]
D[0 0 0 0 0 0]
Attempts:
2 left
💡 Hint
Remember that np.zeros creates an array filled with zeros of the given shape and dtype.
data_output
intermediate
2:00remaining
Shape and size of numpy array created with arange and reshape
What are the shape and size of the numpy array created by this code?
NumPy
import numpy as np
arr = np.arange(12).reshape(3,4)
print(arr.shape, arr.size)
A(3, 4) 7
B(4, 3) 12
C(12,) 3
D(3, 4) 12
Attempts:
2 left
💡 Hint
The reshape changes the shape but not the total number of elements.
🔧 Debug
advanced
2:00remaining
Identify the error in numpy array creation
What error does this code raise when trying to create a numpy array?
NumPy
import numpy as np
arr = np.array([1, 2, 3], dtype='float32', shape=(2,2))
AValueError: cannot reshape array of size 3 into shape (2,2)
BSyntaxError: invalid syntax
CTypeError: array() got an unexpected keyword argument 'shape'
DNo error, array created successfully
Attempts:
2 left
💡 Hint
Check the parameters accepted by np.array function.
🚀 Application
advanced
2:00remaining
Choosing the right numpy array creation method for memory efficiency
You want to create a large array of zeros with shape (10000, 10000) efficiently. Which method is best?
Anp.zeros((10000, 10000))
Bnp.empty((10000, 10000))
Cnp.ones((10000, 10000)) * 0
Dnp.array([[0]*10000]*10000)
Attempts:
2 left
💡 Hint
Consider memory allocation and initialization speed.
🧠 Conceptual
expert
3:00remaining
Why does numpy array creation method affect computation speed?
Which statement best explains why the method of creating a numpy array affects the speed of later computations?
AArrays created with np.empty are faster because they skip initialization, reducing setup time before computations.
BArrays created with np.array are always slower because they copy data from Python lists.
CArrays created with np.ones are faster because they are stored in contiguous memory blocks.
DArrays created with np.zeros are slower because they use integer type by default, which is slower than float.
Attempts:
2 left
💡 Hint
Think about what happens when memory is allocated and initialized.