0
0
NumPydata~15 mins

Masked arrays concept in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Masked arrays concept
📖 Scenario: Imagine you have a list of temperatures recorded over a week. Some sensors failed and gave wrong readings, which you want to ignore in your analysis.
🎯 Goal: You will create a masked array to hide the wrong temperature readings and then calculate the average temperature ignoring those bad values.
📋 What You'll Learn
Create a numpy array with exact temperature values
Create a mask array to mark wrong readings
Create a masked array using numpy.ma module
Calculate the mean of the masked array ignoring masked values
Print the masked array and the calculated mean
💡 Why This Matters
🌍 Real World
Sensors and data collection often produce invalid or missing data. Masked arrays help ignore these bad values during analysis.
💼 Career
Data scientists and analysts use masked arrays to clean and analyze real-world datasets with missing or corrupted data.
Progress0 / 4 steps
1
Create the temperature data array
Create a numpy array called temperatures with these exact values: [22.5, 21.0, -999.0, 23.0, 22.0, -999.0, 24.5]. The value -999.0 represents wrong sensor readings.
NumPy
Need a hint?

Use np.array([...]) to create the array with the exact values.

2
Create the mask for wrong readings
Create a boolean numpy array called mask that is True where temperatures equals -999.0 and False elsewhere.
NumPy
Need a hint?

Use a comparison like temperatures == -999.0 to create the mask.

3
Create the masked array
Use np.ma.masked_array to create a masked array called masked_temps from temperatures using the mask array.
NumPy
Need a hint?

Use np.ma.masked_array(data, mask=mask) to create the masked array.

4
Print the masked array and calculate mean
Print the masked_temps array. Then calculate the mean of masked_temps using its .mean() method and print the result.
NumPy
Need a hint?

Use print(masked_temps) and print(masked_temps.mean()).