0
0
NumPydata~10 mins

In-place operations for memory efficiency in NumPy - 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 5 to each element of the numpy array in-place.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
arr [1]= 5
print(arr)
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of += which creates a new array.
Using * or / which multiply or divide instead of adding.
2fill in blank
medium

Complete the code to multiply each element of the numpy array by 3 in-place.

NumPy
import numpy as np
arr = np.array([2, 4, 6])
arr [1]= 3
print(arr)
Drag options to blanks, or click blank then click option'
A+
B/
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of *= which creates a new array.
Using + or - which add or subtract instead of multiplying.
3fill in blank
hard

Fix the error in the code to subtract 2 from each element of the numpy array in-place.

NumPy
import numpy as np
arr = np.array([10, 20, 30])
arr [1]= 2
print(arr)
Drag options to blanks, or click blank then click option'
A-=
B+=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of -= which creates a new array.
Using += which adds instead of subtracting.
4fill in blank
hard

Fill both blanks to divide each element of the numpy array by 4 in-place and then print the array.

NumPy
import numpy as np
arr = np.array([8, 16, 24])
arr [1]= [2]
print(arr)
Drag options to blanks, or click blank then click option'
A/
B*
C4
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of / which multiplies instead of dividing.
Using 2 instead of 4 which changes the division factor.
5fill in blank
hard

Fill all three blanks to create a numpy array, add 10 in-place, and then multiply by 2 in-place.

NumPy
import numpy as np
arr = np.array([1])
arr [2]= 10
arr [3]= 2
print(arr)
Drag options to blanks, or click blank then click option'
A[5, 10, 15]
B+=
C*=
D[1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong list for the array creation.
Using = instead of += or *= which creates new arrays.
Mixing up the order of operations.