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

  1. Run the learning example and compare the alarm times with the temperatures.
  2. Change the threshold from 28°C to 26°C and find the additional alarm times.
  3. Mark the threshold line and alarm points on the same plot, and explain the effect of changing the threshold.

Results to retain

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

Checking whether calibration reduces error

Starting conditions

The sensor inputs are x=[20222426]x=\begin{bmatrix}20&22&24&26\end{bmatrix} °C, and the reference temperatures are y=[22.524.726.829.1]y=\begin{bmatrix}22.5&24.7&26.8&29.1\end{bmatrix} °C. The calibration equation is y_hat=1.1*x+0.5.

Procedure

  1. Calculate predictions using the input unchanged and using the calibration equation.
  2. Calculate the difference between each prediction and its reference value, and the mean absolute error.
  3. 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

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

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

  1. Distinguish failed measurements from out-of-range values and record them with their original times.
  2. Use valid values to calculate the mean and alarms above 28°C. Compare with the results when the condition is changed to ≥.
  3. Retain missing times on the plot and indicate that no measurement is available.

Results to retain

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