0
0
Data Analysis Pythondata~10 mins

Broadcasting rules 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 add a 1D array to each row of a 2D array using broadcasting.

Data Analysis Python
import numpy as np
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
arr1d = np.array([10, 20, 30])
result = arr2d + [1]
print(result)
Drag options to blanks, or click blank then click option'
Anp.array([1, 2])
Barr2d
Carr1d
Dnp.array([[10], [20], [30]])
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr2d instead of arr1d causes no change.
Using arrays with incompatible shapes causes errors.
2fill in blank
medium

Complete the code to multiply a 2D array by a column vector using broadcasting.

Data Analysis Python
import numpy as np
arr2d = np.array([[1, 2], [3, 4], [5, 6]])
col_vec = np.array([[10], [20], [30]])
result = arr2d * [1]
print(result)
Drag options to blanks, or click blank then click option'
Acol_vec
Barr2d
Cnp.array([10, 20, 30])
Dnp.array([[1, 2, 3]])
Attempts:
3 left
💡 Hint
Common Mistakes
Using a 1D array with shape (3,) instead of (3,1) causes shape mismatch.
Multiplying by arr2d itself does not change the array.
3fill in blank
hard

Fix the error in the code to add a 1D array to a 2D array when shapes are incompatible.

Data Analysis Python
import numpy as np
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
arr1d = np.array([10, 20])
result = arr2d + [1]
print(result)
Drag options to blanks, or click blank then click option'
Aarr1d
Barr1d.reshape(2, 1)
Carr1d.reshape(3, 1)
Darr1d.reshape(1, 2)
Attempts:
3 left
💡 Hint
Common Mistakes
Adding arr1d directly causes a shape mismatch error.
Reshaping to (1,2) does not match arr2d's shape.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2] > 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for the value.
Checking word > 3 instead of len(word) > 3.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 3.

Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {{ [1]: [2] for word in words if [3] > 3 }}
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting words to uppercase for the key.
Using word instead of len(word) for the value or condition.