Challenge - 5 Problems
Array Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that np.zeros creates an array filled with zeros of the given shape and dtype.
✗ Incorrect
np.zeros creates an array of zeros with the specified shape and data type. Here, shape (2,3) means 2 rows and 3 columns, and dtype=int means integer zeros.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
The reshape changes the shape but not the total number of elements.
✗ Incorrect
np.arange(12) creates an array with 12 elements from 0 to 11. Reshape changes it to 3 rows and 4 columns. Size is total elements, 12.
🔧 Debug
advanced2: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))
Attempts:
2 left
💡 Hint
Check the parameters accepted by np.array function.
✗ Incorrect
np.array does not accept a 'shape' argument. Shape is set by the input data or by reshaping after creation.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
Consider memory allocation and initialization speed.
✗ Incorrect
np.empty allocates memory without initializing values, so it's faster and uses less CPU. You can fill zeros later if needed.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about what happens when memory is allocated and initialized.
✗ Incorrect
np.empty allocates memory without setting values, so it is faster to create. This reduces setup time before computations start.