Challenge - 5 Problems
np.array() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of np.array() with nested lists
What is the output of this code?
import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr.shape)
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr.shape)
Attempts:
2 left
💡 Hint
Think about how many rows and columns the nested list has.
✗ Incorrect
The nested list has 2 lists each with 2 elements, so the shape is (2, 2).
❓ data_output
intermediate1:30remaining
Data type of np.array from mixed Python lists
What is the data type of the numpy array created by this code?
import numpy as np arr = np.array([1, 2.5, 3]) print(arr.dtype)
NumPy
import numpy as np arr = np.array([1, 2.5, 3]) print(arr.dtype)
Attempts:
2 left
💡 Hint
Numpy converts all elements to the same type, choosing the one that can hold all values.
✗ Incorrect
Since 2.5 is a float, numpy converts all elements to float64 to hold decimal values.
🔧 Debug
advanced2:00remaining
Error when creating np.array from irregular nested lists
What error does this code raise?
import numpy as np arr = np.array([[1, 2], [3, 4, 5]]) print(arr)
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4, 5]]) print(arr)
Attempts:
2 left
💡 Hint
Check how numpy handles nested lists with different lengths.
✗ Incorrect
Numpy creates a 1D array of lists (dtype=object) when nested lists have different lengths.
🚀 Application
advanced1:30remaining
Creating a 3D numpy array from Python lists
Which option creates a numpy array with shape (2, 2, 2)?
Attempts:
2 left
💡 Hint
Count the number of nested brackets and elements per level.
✗ Incorrect
Option A has two blocks, each with two rows and two columns, making shape (2, 2, 2).
🧠 Conceptual
expert2:00remaining
Why does np.array() sometimes create dtype=object?
Why does numpy create an array with dtype=object when given certain Python lists?
Attempts:
2 left
💡 Hint
Think about numpy's need for uniform data types and shapes.
✗ Incorrect
Numpy requires arrays to have uniform shape and type; irregular nested lists force dtype=object.