Machine Learning with Measurement Data
English translation
Learning objectives
Create alarms from temperature measurement records and compare prediction errors of a sensor calibration equation.
Comparing measurements and alarm labels
Starting conditions
The times are [0,5,10,15,20,25] min and temperatures are [24,25,29,23,30,26] °C. Trigger an alarm when the temperature exceeds the threshold.
Procedure
- Run the learning example and compare the alarm times with the temperatures.
- Change the threshold from 28°C to 26°C and find the additional alarm times.
- Mark the threshold line and alarm points on the same plot, and explain the effect of changing the threshold.
Results to retain
- Alarm times and counts for each threshold, plus a plot with axes and units.
import numpy as np
import matplotlib.pyplot as plt
minutes=np.array([0,5,10,15,20,25])
temperature=np.array([24.,25.,29.,23.,30.,26.])
threshold=28.
alarm=temperature>threshold
print('alarm times [min]:',minutes[alarm])
print('alarm count:',alarm.sum())
plt.plot(minutes,temperature,'o-',label='measurement')
plt.axhline(threshold,color='gray',linestyle='--',label='threshold')
plt.scatter(minutes[alarm],temperature[alarm],color='red',label='alarm')
plt.xlabel('Time [min]');plt.ylabel('Temperature [deg C]');plt.legend();plt.grid()
# TODO: Compare results using the 26°C criterion.
Record results and self-check
Reference temperature:
Alarm times:
Alarm count:
Points to check
- Are there two alarms at the 28°C threshold, at 10 and 20 minutes?
- Did you exclude values exactly at 26°C when the condition is above 26°C?
Checking whether calibration reduces error
Starting conditions
The sensor inputs are °C, and the reference temperatures are °C. The calibration equation is y_hat=1.1*x+0.5.
Procedure
- Calculate predictions using the input unchanged and using the calibration equation.
- Calculate the difference between each prediction and its reference value, and the mean absolute error.
- Also compare errors from the two methods for a new input of 28°C and a reference temperature of 31.4°C.
Results to retain
- A table with inputs, reference values, two predictions, and errors side by side, plus a comparison using a new measurement.
x=np.array([20.,22.,24.,26.]);reference=np.array([22.5,24.7,26.8,29.1])
corrected=1.1*x+.5
print(np.column_stack([x,reference,corrected]))
print('raw MAE:',np.mean(np.abs(x-reference)))
print('corrected MAE:',np.mean(np.abs(corrected-reference)))
# TODO: Compute predictions for the new inputs and both errors.
Record results and self-check
Input [°C]:
Reference [°C]:
Uncalibrated error:
Calibrated error:
Points to check
- Is the calibration equation's mean absolute error approximately .025°C?
- Did you describe this experiment as evaluating a provided equation, without presenting it as a result of learning the coefficients?
Identifying and recording invalid measurements
Starting conditions
Use the records [24,None,29,100,26,28] °C at the same times. The sensor's valid temperature range is 15–40°C, and None indicates a failed measurement.
Procedure
- Distinguish failed measurements from out-of-range values and record them with their original times.
- Use valid values to calculate the mean and alarms above 28°C. Compare with the results when the condition is changed to ≥.
- Retain missing times on the plot and indicate that no measurement is available.
Results to retain
- A table of input status, the mean and alarm count for valid measurements, and a plot that distinguishes missing measurements.
records=[24,None,29,100,26,28]
values=np.array([np.nan if v is None else float(v) for v in records])
valid=np.isfinite(values)&(values>=15)&(values<=40)
print('valid times:',minutes[valid])
print('valid mean:',values[valid].mean())
# TODO: Compare the input conditions and two alarm conditions, and plot with invalid points marked as np.nan.
Record results and self-check
Time:
Original measurement:
Input status:
Alarm status:
Points to check
Are there 4 valid values with a mean of 26.75°C?
Is there 1 alarm under the condition and 2 under the ≥ condition?
Did you avoid replacing None and 100°C with actual 0°C values?