π» Activity 3.1: Indoor Temperature Control with Lists#
Scenario:#
Imagine you are designing a program to manage the indoor temperature of a building to keep students and employees productive. The program analyzes the temperature in a room over time to know how much warm or cool air to add using the heating, ventilation, and air conditioning system. This data is critical for monitoring the machineβs performance and predicting maintenance needs.
Data Collection: The sensor data is collected at regular intervals and each data point is stored in a Python list. This creates a time-series dataset.
A list named temperature_data
contains temperature values (in degrees Celsius) recorded from various sensors in an office building.
These readings represent temperatures taken every hour over one day.
The values fluctuate, indicating natural variations in room temperature, perhaps due to changes in occupancy, time of day, or HVAC system operation.
temperature_data = [22.5, 22.7, 22.6, 22.8, 23.0, 23.2, 23.1, 22.9, 22.7, 22.6,
22.5, 22.4, 22.3, 22.2, 22.1, 22.0, 21.9, 21.8, 21.7, 21.6,
21.5, 21.4, 21.3, 21.2, 21.1, 21.0]
Data Processing: Lists allow for easy manipulation of these data. For example, a simple moving average filter function is shown below. This filter smooths the data by replacing each point with the average of it and its neighbors.
The function below takes as input arguments a list of data points (
data
) and awindow_size
.The
window_size
determines how many data points are considered for calculating the average. It is set to 3 by default, meaning that each new data point is the average of itself and one neighbor on each side.The function loops through each point in the data, calculates the average for the points within the window, and stores the result in
filtered_data
.Edge cases (start and end of the list) are handled by adjusting the window size to fit within the bounds of the list.
Name the function below moving_average_filter_function
and provide data
as the first input argument.
def ...(..., window_size=3):
"""
Apply a simple moving average filter.
:param data: List of temperature data points.
:param window_size: Number of points to include in the moving average.
Must be an odd number.
:return: List of filtered data points.
"""
filtered_data = []
half_window = window_size // 2
for i in range(len(data)):
# Determine the moving window boundaries
start = max(i - half_window, 0)
end = min(i + half_window + 1, len(data))
# Calculate the average within the window
window_average = sum(data[start:end]) / (end - start)
filtered_data.append(window_average)
return filtered_data
This function can be applied to temperature_data
to smooth out short-term fluctuations and make trends more apparent. The choice of window_size
can be adjusted based on how smooth you want the resulting data to be. A larger window size will produce smoother data but can also obscure short-term variations.
Try applying the filter to temperature_data
with a window size of 3:
moving_average_of_temperatures_window_3 = moving_average_filter_function(...)
print(moving_average_of_temperatures_window_3)
Now try applying the filter to temperature_data
with a window size of 6.
moving_average_of_temperatures_window_6 = moving_average_filter_function(..., ...)
print(moving_average_of_temperatures_window_6)
Feature Extraction: Extracting features like maximum, minimum, and mean value from the list. This might be helpful for evaluating the energy efficiency with which a building has been operated in the past.
max_value = ...
min_value = ...
average = ...
print(f'The maximum temperature over the 24-hour period was {...}.')
print(f'The minimum temperature over the 24-hour period was {...}.')
print(f'The average temperature over the 24-hour period was {...}.')
Data Visualization: lists can be directly used with libraries like Matplotlib to create plots that help in visualizing the trends and patterns in the data.
import matplotlib.pyplot as plt
# plot original hourly temperature in black
plt.plot(...,'k')
# plot 3-hour average temperature in blue
plt.plot(...,'b')
# plot 6-hour average temperature in red
plt.plot(...,'r')
plt.title('Hourly Temperature')
plt.xlabel('Time')
plt.ylabel('Temperature (ΛC)')
plt.show()
Integration with Advanced Analysis: Lists can be converted to more complex data structures like NumPy arrays or Pandas DataFrames for more advanced analysis or machine learning applications.
import numpy as np
temperature_data_array = ...
Benefits:#
Flexibility: Lists are dynamic and can handle data of varying sizes easily.
Ease of Use: Pythonβs syntax and list comprehensions make data processing tasks straightforward.
Integration: Lists can easily be integrated with other Python libraries for more advanced analysis.
This application demonstrates how lists can be a powerful tool in engineering, particularly for handling and processing time-series data in signal processing, for example, in indoor temperature control.