0
0
NumPydata~10 mins

Controlling copy behavior 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 create a copy of the NumPy array 'a'.

NumPy
import numpy as np

a = np.array([1, 2, 3])
b = a.[1]()
Drag options to blanks, or click blank then click option'
Acopy
Bview
Creshape
Dflatten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view()' instead of 'copy()' which does not create a full copy.
Using 'reshape()' or 'flatten()' which change shape but not copy behavior.
2fill in blank
medium

Complete the code to create a view of the NumPy array 'a'.

NumPy
import numpy as np

a = np.array([4, 5, 6])
b = a.[1]()
Drag options to blanks, or click blank then click option'
Aflatten
Bcopy
Cview
Dtranspose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'copy()' which creates a full independent copy.
Using 'flatten()' or 'transpose()' which change shape or axes.
3fill in blank
hard

Fix the error in the code to create a copy of the array slice.

NumPy
import numpy as np

a = np.array([7, 8, 9, 10])
slice_a = a[1:3]
slice_copy = slice_a.[1]()
Drag options to blanks, or click blank then click option'
Acopy
Breshape
Cview
Dflatten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view()' which still references original data.
Using 'reshape()' or 'flatten()' which do not copy data.
4fill in blank
hard

Fill both blanks to create a view and then modify it without affecting the original array.

NumPy
import numpy as np

a = np.array([10, 20, 30, 40])
view_a = a.[1]()
copy_a = view_a.[2]()
copy_a[0] = 99
Drag options to blanks, or click blank then click option'
Aview
Bcopy
Cflatten
Dreshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'copy()' for the first blank which creates a copy, not a view.
Using 'view()' for the second blank which does not create an independent copy.
5fill in blank
hard

Fill all three blanks to create a copy of a reshaped array slice.

NumPy
import numpy as np

a = np.arange(12)
slice_a = a[2:10]
reshaped = slice_a.[1]((2, 4))
copy_reshaped = reshaped.[2]()
copy_reshaped[0, 0] = [3]
Drag options to blanks, or click blank then click option'
Areshape
Bcopy
C100
Dview
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' instead of 'copy' which modifies original data.
Not reshaping before copying.
Assigning a value that is not an integer.