0
0
NumPydata~10 mins

Masked arrays concept 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 masked array from a numpy array.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
masked_arr = np.ma.[1](arr)
Drag options to blanks, or click blank then click option'
Amask
Barray
Cma_array
Dmasked_array
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.array instead of np.ma.masked_array
Trying to use np.mask which is not a constructor
Using an incorrect function name like ma_array
2fill in blank
medium

Complete the code to mask all elements equal to 3 in the array.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 3])
masked_arr = np.ma.masked_array(arr, mask=arr [1] 3)
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ==
Using > or < which mask wrong elements
3fill in blank
hard

Fix the error in the code to correctly mask elements less than 0.

NumPy
import numpy as np
arr = np.array([-1, 0, 1, 2])
masked_arr = np.ma.masked_array(arr, mask=arr [1] 0)
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or == which mask wrong elements
Using <= which masks zero as well
4fill in blank
hard

Fill both blanks to create a masked array masking values greater than 10 and then fill masked values with -1.

NumPy
import numpy as np
arr = np.array([5, 12, 7, 15, 3])
masked_arr = np.ma.masked_array(arr, mask=arr [1] 10)
filled_arr = masked_arr.[2](-1)
Drag options to blanks, or click blank then click option'
A>
Bfill
Cfilled
Dmask
Attempts:
3 left
💡 Hint
Common Mistakes
Using fill() instead of filled()
Using mask instead of filled to replace masked values
5fill in blank
hard

Fill all three blanks to create a masked array from a list, mask negative values, and get the compressed (unmasked) data.

NumPy
import numpy as np
lst = [4, -2, 7, -5, 9]
masked_arr = np.ma.[1](lst, mask=[x [2] 0 for x in lst])
compressed_data = masked_arr.[3]()
Drag options to blanks, or click blank then click option'
Amasked_array
B<
Ccompressed
Dmask
Attempts:
3 left
💡 Hint
Common Mistakes
Using mask instead of masked_array to create the array
Using > instead of < for masking negative values
Using mask() instead of compressed() to get unmasked data