# Hybrid Machine Learning-Statistical Method for Anomaly Detection in Flight Data
## 3 Local Outlier Factor
### Implementation
#### 3.2.4 \(k\) Nearest Neighbor
However, the disadvantage of this measure is that it ignores the magnitude of vectors and only considers their direction. Therefore, the difference in values may not be considered, which is important for detecting anomalous flights. \begin{table}
\begin{tabular}{c c c c} \hline \hline
**Flights** & **LOF Score (\(k\) = 13)** & **LOF Score (\(k\) = 49)** & **LOF Score (\(k\) = 80)** \\ \hline Flight 1 & 1.89719894601340 & 1.794999571605941 & 1.61952094645704 \\ \hline Flight 2 & 1.24792810898067 & 1.09357548151929 & 1.00903988539139 \\ \hline Flight 3 & 1.20925468652125 & 1.12810045075146 & 1.07174161543744 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Outlying nature of flights for different \(k\) values. Figure 5: Selection of \(k\) values at different instances of LOF score. The Manhattan distance was chosen to find the optimal value of \(k\) as it calculates the distance between real-valued vectors. This helps to capture the \(k\) value where a data point is most outlying. More importantly, flight data have both discrete and binary attributes. For instance, flight parameters such as landing gear and weight on wheels have only values as 0 or 1. Using Manhattan as the distance measure helps in this scenario as it considers the paths that realistically could be taken within values of such attributes. Manhattan distance measure is fast compared to Euclidean distance for each pair of distance calculations; there is no need for squaring or taking square roots to obtain absolute values. Moreover, squaring of the vector components can skew results by giving more weight to outliers that are of our interest. Minneapolis airport was taken as an example. There are, in total, 200 flights in a dataset arriving at this airport. We calculate the LOF score for all possible \(k\) values between 10 and 180. The \(k\) value, which gives the highest LOF score, is the optimal one. In this case, the \(k\) value of 53 is the most optimal as it has the highest LOF score of 1.87. The left side of Figure 6 gives the plot for the LOF score and possible \(k\) values between 10 and 180. From the plot, it can be seen that the highest LOF value is between the k value of 40 and 60. Moreover, in between this range, only the k value stabilizes before decreasing. The highest LOF score is between \(k\) values 50 and 55. The highlighted box of Figure 6 shows that the highest LOF score is at a \(k\) value of 53. Therefore, 53 is the optimal k value for this case. For the purpose of comparison, Figure 7 shows the line plot comparing the optimal \(k\) values for three airports Detroit, Minneapolis and Memphis. The optimal \(k\) value is for the different total number of flights arriving at each airport. From the work of [PERSON] and [PERSON], it was concluded that the suitable setting of the \(k\) value should satisfy \(k=\sqrt{N}\) for the datasets with a sample size \(N\) which is larger than 100 [9]. However, such a setting has been proven to be not suitable for flight data in this study. In the case of the detection of anomalous flights, this general rule is not valid due to the nature of aviation data, and the method discussed above gives a dynamic value of \(k\) depending on the total number of fights and the arrival airport.