## 3 System Implementation and Verification
### Data Restoration Using the KNN Algorithm  
The system collects environmental data once per minute using multiple sensors that operate simultaneously. Consequently, the system collects a lot of data. Data correction is not a priority. This is the reason for selecting the QoS 0 message transmission in the MQTT protocol. Data loss can occur due to sensor malfunction and power loss. To address these problems, the KNN algorithm was selected for data restoration. First, a data value is entered, making a judgment, dealing with the missing part of the data, and filling in the \"not a number\" value. Subsequently, the training data are brought into the training set, and the indoor temperature, humidity, and carbon dioxide levels are set as references. The missing data are imputed, and the features of the missing data are compared with the corresponding features of data in the training set. The KNN algorithm is used to extract the most similar data from the training set (nearest neighbors), thereby restoring the missing data [17]. Figure 11 depicts the restoration operation using the KNN algorithm. The experimental results employ min-max normalization (also known as feature scaling) to reduce the impact of unrelated samples on the measured results. Min-max normalization applies a linear transformation to the original data, scaling all values to fall within the range (0, 1). By defining the minimum value \(X_{min}\) and maximum value \(X_{max}\), the scaled data \(X_{scaled}\) is calculated using the formula \((X-X_{min})/(X_{max}-X_{min})\), where \(X\) represents the original measured data. While this approach ensures a bounded range, it also results in smaller standard deviations, which may suppress the effects of outliers.  
Figures 12 and 13 present two comparison line charts indicating the frequencies with which different values of humidity and carbon dioxide content were recorded/predicted. The green lines represent the original data; the red lines represent the imputed data predicted using the KNN algorithm. Among the evaluation criteria, MAPE and RMSE are commonly used evaluation formulas. MAPE is calculated by standardizing against the original value \(y_{i}\), meaning that the error is independent of the variation in \(y_{i}\). In contrast, MAE is directly influenced by the variation in \(y_{i}\). Additionally, if the correlation coefficient R needs to be calculated, the sample mean value of a distribution (\(\bar{y}\)) must first be determined, which requires additional computation time. The accuracy of the imputation  
Figure 10: Experimental environment of a proposed home monitoring system.  
was determined in terms of the mean absolute percentage error (MAPE), which can be expressed as follows [14].  
\[MAPE=\frac{1}{m}\sum_{i=1}^{m}\frac{|y_{i}-f(x_{i})|}{\max(y_{i},\varepsilon)} \times 100\% \tag{7}\]  
where \(m\) represents the total number of times, \(y_{i}\) represents the real data, _f(x\({}_{i}\))_ represents the predicted data, and \(\varepsilon\) is a small positive constant used to avoid division by zero. The \(x_{i}\) and \(y_{i}\) variables represent the features of the sample, such as temperature, humidity, carbon dioxide concentration, or other sensor data. The average relative errors of humidity and carbon dioxide content were calculated to be approximately 0.0415% and 0.134%, respectively.  
Figure 11: Restoration function of the KNN algorithm.  
Figure 12: Comparison line chart for humidity, indicating the number of times.