0
0
Data Analysis Pythondata~10 mins

Array shapes and dimensions in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a 2D NumPy array with shape (3, 2).

Data Analysis Python
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr[1])
Drag options to blanks, or click blank then click option'
A.size
B.dtype
C.ndim
D.shape
Attempts:
3 left
💡 Hint
Common Mistakes
Using .size instead of .shape returns total elements, not dimensions.
Using .ndim returns number of dimensions, not the shape tuple.
2fill in blank
medium

Complete the code to find the number of dimensions of the array.

Data Analysis Python
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[1])
Drag options to blanks, or click blank then click option'
A.shape
B.ndim
C.size
D.dtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using .shape returns the shape tuple, not the number of dimensions.
Using .size returns total elements, not dimensions.
3fill in blank
hard

Fix the error in the code to reshape the array to 2 rows and 3 columns.

Data Analysis Python
import numpy as np
arr = np.arange(6)
reshaped = arr[1]((2, 3))
print(reshaped)
Drag options to blanks, or click blank then click option'
A.reshape
B.resize
C.reshape()
D.shape
Attempts:
3 left
💡 Hint
Common Mistakes
Using .reshape() with extra parentheses causes syntax errors.
Using .resize modifies the array in place and can cause unexpected results.
4fill in blank
hard

Fill both blanks to create a 3D array with shape (2, 2, 2) and print its number of dimensions.

Data Analysis Python
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr[1])
print(arr[2])
Drag options to blanks, or click blank then click option'
A.shape
B.size
C.ndim
D.dtype
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up .size and .ndim attributes.
Using .dtype instead of shape or ndim.
5fill in blank
hard

Complete the code to create a dictionary with word lengths for words longer than 3 characters.

Data Analysis Python
words = ['data', 'science', 'is', 'fun']
lengths = {word: len(word) for word in words if len(word) [1] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
A:
B>
C)
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Using comma instead of colon in dictionary comprehension.
Using < or == instead of > in the condition.
Missing closing parenthesis in print.