0
0
Data Analysis Pythondata~20 mins

Array shapes and dimensions in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Shapes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the shape of the resulting array?
Consider the following code that creates a NumPy array. What is the shape of arr after running this code?
Data Analysis Python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
A(3,)
B(3, 2)
C(2, 3)
D(6,)
Attempts:
2 left
💡 Hint
Think about how many rows and columns the array has.
data_output
intermediate
2:00remaining
What is the number of dimensions of this array?
Given the code below, what is the value of arr.ndim?
Data Analysis Python
import numpy as np
arr = np.array([[[1], [2]], [[3], [4]]])
print(arr.ndim)
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Count how many levels of nested lists are inside the array.
🔧 Debug
advanced
2:00remaining
Why does this reshape operation fail?
The code below tries to reshape an array but raises an error. What is the reason?
Data Analysis Python
import numpy as np
arr = np.arange(10)
arr_reshaped = arr.reshape(3, 4)
AThe array must be 2-dimensional before reshaping.
BThe reshape method requires a tuple, not separate arguments.
CThe reshape method only works on square arrays.
DThe total number of elements does not match the new shape.
Attempts:
2 left
💡 Hint
Check how many elements are in the original array and the new shape.
🚀 Application
advanced
2:00remaining
How to flatten a 3D array into 1D?
Given a 3D NumPy array, which code correctly converts it into a 1D array?
Data Analysis Python
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
Aarr.flatten()
Barr.reshape(2, 4)
Carr.reshape(1, 8)
Darr.reshape(-1, 1)
Attempts:
2 left
💡 Hint
Flatten means to make all elements in one dimension.
🧠 Conceptual
expert
3:00remaining
What is the shape of the array after this operation?
Consider this code snippet. What is the shape of arr_new after execution?
Data Analysis Python
import numpy as np
arr = np.arange(24).reshape(2, 3, 4)
arr_new = arr.swapaxes(1, 2)
A(3, 2, 4)
B(2, 4, 3)
C(4, 3, 2)
D(2, 3, 4)
Attempts:
2 left
💡 Hint
swapaxes swaps two axes positions in the shape tuple.