# Dynamic Nearest Neighbor: An Improved Machine Learning Classifier and Its Application in Finances
## 3 Proposed Method  
In this paper, we propose a novel model for classification, with an embedded feature selection approach. The algorithm is based on the well-known nearest neighbor classifier and is named as Dynamic 1-NN (D1-NN) because it reflects the idea behind the simple but effective modification of the classical algorithm: we have stopped considering the set of attributes as a static set, to make it dynamic. The new idea consists in the dynamic consideration of subsets extracted from the set of attributes \(A=\{A_{1},\ldots,A_{m}\}\) throughout the dataset under study. Therefore, the training or learning phase remains exactly the same as in the classic model. The novelties are reflected in the classification phase and in a better performance, so that the new D1-NN algorithm is competitive in the state of the art of pattern classifier algorithms. The initial assumptions for the proposed algorithm are exactly the same as those specified for the original algorithm. That is, for the creation and operation of the new D1-NN algorithm it is assumed that:  
* In a specific pattern classification problem, there are \(m\), \(n\), and \(p\) which are fixed positive integers greater than 1. * There is a training or learning set of cardinality \(n\), \(T=\{\mathbf{x}_{1},\mathbf{x}_{2},\cdots,\mathbf{x}_{n}\}\), which is made up of patterns. * Each of the \(n\) patterns in the set \(T\) is made up of \(m\) attributes \(A=\{A_{1},\ldots,A_{m}\}\). * The \(j\)-th component of the \(i\)-th training or learning pattern \(\mathbf{x}_{i}\) is denoted by \(x_{ij}\). * The patterns do not contain categorical, mixed, or missing values. There are only numeric values. * Each of the patterns in the training or learning set belongs to a single class in the set of \(p\) classes \(C=\{C_{1},\ldots,C_{p}\}\). The D1-NN classifier training phase is the same as the one of the classical 1-NN algorithm. Therefore, the D1-NN classifier training or learning phase consists of storing in memory the training or learning set \(T\). When the training or learning phase concludes, what proceeds is to test the proposed algorithm with patterns whose class the system does not know. To conduct the D1-NN classification phase, it is assumed that there is a new pattern to be classified, which is formed by the same \(m\) attributes of the training or learning patterns. This is the test pattern, which is denoted by \(\mathsf{o}\), and whose class is unknown. In this phase, the D1-NN will attempt to assign the correct class to the test pattern \(\mathsf{o}\). The introduction of a parameter \(L\) to the proposed algorithm is the main contribution of our research to the original algorithm 1-NN. This parameter \(L\) gives the possibility to choose attributes in subsets of cardinality \(L\). By moving a window of size \(L\) through the set of attributes \(A\), the proposal becomes a dynamic algorithm, the D1-NN. The classification phase of D1-NN has three steps:  
1. A parameter \(L\) is set, \(L\in\mathbb{N}\), \(0\leq L\leq m\) (typically \(0\leq L\leq 3\)). 2. For each index \(i\) such that \(1\leq i\leq m-L\), and for each index \(j\) such that \(i+L\leq j\leq m\) do: 2.1 Create a new learning set \(T_{[ij]}\) with the patterns of \(T\) restricted to attributes \(A_{k}\) such that \(i\leq k\leq j\) 2.2 Apply the 1-NN classifier to the pattern to be classified \(\mathsf{o}\), with the set \(T_{[ij]}\) 2.3 Store the class that delivers the 1-NN in step 2.2, as \(C_{[ij]}\in C\)
3. Assign to the pattern \(\mathsf{o}\) the most frequent class in the set of all values \(C_{[ij]}\)  
In the following, we exemplify the functioning of the proposal with an example dataset (Figure 0(a)), and the classification phase of D1-NN is showed in Figure 0(b)-f. In the example of Figure 1 the number of attributes is \(m=6\) and we set the parameter \(L=2\). Let us consider an object to classify as \(\mathsf{o}=[0.20,2.04,0.38,0.52,3.5,3.6]\). Figure 1: Example of the classification phase of the proposed D1-NN.