def classifySeq(self, input_seq): # print('input seq:', input_seq) self.class_labels = np.copy(self.c_labels) self.distances = np.zeros((len(self.X)), dtype=float) dtw = DTW() dtw.setY(input_seq) for i, seq in enumerate(self.X): # print('\tcomparing with', i) dtw.setX(seq) self.distances[i] = dtw.perform_dtw() bubble_sort(self.K, self.distances, self.class_labels) self.bins = np.bincount(self.class_labels[:self.K]) self.assigned_classlabel = self.bins.argmax() # print('assigned class label: ', self.assigned_classlabel) return self.assigned_classlabel
def main(): X = np.array( [[ -1.870935e-01, -1.941152e+01, -5.882407e+00, -6.821157e+00, -1.960176e+01, -9.122212e+00, -5.398831e+00, 1.076720e+01, 6.568266e+00, -4.519125e+00, 3.895056e+00, 5.133921e+00, 8.817558e-01, -3.344995e-01, -5.670179e-01, 1.977534e-01, -2.825252e+00, 5.670564e-02, -4.054463e-01, -1.570377e+00, 2.622198e-01, -1.543853e+00, -2.045755e-01, -3.527825e-01, -9.646030e-01, 2.183463e-02, -1.032675e-01, -2.037469e-01, 6.428432e-02, 3.759445e-01, 1.421797e-01, 6.430891e-01, 4.654990e-01, 3.725418e-01, 1.287469e-01, -1.014837e-01, -2.791396e-01, -1.542485e-01, 2.186694e-03 ], [ -1.019952e+00, -1.993099e+01, -4.057678e+00, -1.526845e+01, -2.177533e+01, -1.245389e+01, -9.609365e+00, 1.133379e+01, 1.232076e+00, -6.754516e+00, 2.665905e+00, 2.272804e+00, 9.301512e-01, -4.683660e-01, -9.745451e-01, 2.767194e-01, -2.455450e+00, 5.393829e-01, 9.000364e-01, -8.480091e-01, 1.262016e+00, -1.286797e+00, -2.833939e-01, -7.601787e-01, -1.068123e+00, 3.039335e-02, -1.688788e-01, -1.540181e-01, 1.790668e-01, 9.829438e-01, 1.804260e-01, 8.561022e-01, 8.271446e-01, 4.591559e-01, 3.011147e-01, -1.383719e-01, -4.674556e-01, -2.046140e-01, -3.690498e-04 ], [ -1.443142e+00, -2.198701e+01, -5.805995e+00, -1.672378e+01, -1.823138e+01, -9.483617e+00, -1.114543e+01, 1.179495e+01, 1.517057e+00, -4.424240e+00, 2.745863e+00, 1.741368e+00, 9.667338e-01, -7.838881e-01, -1.382024e+00, 4.797013e-01, -1.130435e+00, 5.262859e-01, 2.157257e+00, 3.958666e-01, 1.625053e+00, -1.028640e+00, -6.725525e-01, -1.544723e+00, -1.684104e+00, 2.848946e-02, -1.810844e-01, 7.951448e-02, 2.598753e-01, 1.174168e+00, 6.566963e-02, 4.949601e-01, 8.592530e-01, 3.002176e-01, 3.606367e-01, -2.307723e-01, -4.826202e-01, -2.030101e-01, -4.236597e-03 ], [ -1.900802e+00, -2.299645e+01, -4.536950e+00, -1.414699e+01, -1.758996e+01, -4.441329e+00, -6.765486e+00, 1.656342e+01, 2.659910e+00, -5.983455e+00, 6.687205e-01, 1.489522e+00, 9.912346e-01, -9.541904e-01, -9.296216e-01, 9.521492e-01, 1.242009e+00, 7.240354e-01, 2.593670e+00, 1.582148e+00, 1.876561e+00, -2.959121e-01, -6.624181e-01, -2.094018e+00, -1.627936e+00, 1.666238e-02, -6.888758e-02, 3.390231e-01, 3.715679e-02, 6.889561e-01, -7.894406e-02, -1.904353e-01, 3.151930e-01, -2.784935e-01, 3.556462e-01, -9.185118e-02, -8.689702e-02, 1.021361e-01, -7.807227e-03 ], [ -3.666164e+00, -2.478892e+01, -3.244268e+00, -1.303400e+01, -1.906302e+01, -2.341908e+00, -4.841555e+00, 1.627757e+01, 7.112659e-01, -8.267406e+00, -2.829990e+00, -2.894825e+00, 9.936614e-01, -9.970276e-01, -1.918873e-01, 1.159399e+00, 1.196834e+00, 2.927031e-01, 1.222421e+00, 1.510809e+00, 1.455974e+00, -2.361124e-01, -1.168944e+00, -2.098960e+00, -1.699771e+00, 7.517332e-03, 1.816315e-01, 3.902620e-01, -3.455281e-01, -2.452208e-01, -1.617719e-01, -7.106320e-01, -5.168278e-01, -8.029260e-01, 1.800197e-01, 7.171424e-02, 3.953680e-01, 5.738304e-01, -7.401578e-03 ], [ -4.679300e+00, -2.317812e+01, -5.777994e-01, -1.090323e+01, -1.773932e+01, -3.056363e+00, -4.850668e+00, 1.847520e+01, 1.554637e-01, -8.145046e+00, -5.016462e+00, -3.548824e+00, 1.000000e+00, -7.062657e-01, 1.255049e-01, 1.227021e-01, -1.742893e-01, 2.614480e-01, 4.152198e-01, 1.704610e-01, -4.592960e-02, 9.517462e-02, -4.944558e-01, -9.176090e-01, -5.496670e-01, 1.843350e-03, 4.214767e-01, 2.275055e-01, -5.783656e-01, -8.267415e-01, -2.690008e-01, -6.347027e-01, -9.392050e-01, -1.088750e+00, -6.555431e-02, 2.584541e-01, 8.261142e-01, 8.615100e-01, -5.349388e-03 ], [ -5.039043e+00, -2.285567e+01, -1.988579e+00, -1.236157e+01, -1.669317e+01, -4.063885e+00, -4.548844e+00, 1.811908e+01, 1.588668e+00, -9.188303e+00, -4.906452e+00, -4.238289e+00, 9.999387e-01, 3.074194e-04, 4.170531e-02, -8.331995e-01, -1.648457e+00, -5.129647e-02, -3.066546e-01, -1.482461e+00, -1.428380e+00, -3.240738e-01, -3.979310e-01, -1.560897e-01, 6.459251e-01, -1.109232e-03, 5.914558e-01, 1.447010e-01, -3.698424e-01, -2.614930e-01, -2.379599e-01, -3.071502e-01, -8.654104e-01, -9.751623e-01, -2.588414e-01, 3.338255e-01, 9.100384e-01, 8.658913e-01, -4.769736e-03 ], [ -4.745595e+00, -2.333554e+01, -4.551358e+00, -1.535441e+01, -1.746766e+01, -1.504144e+00, -6.059576e+00, 1.541317e+01, 2.697018e+00, -7.995371e+00, -2.880983e+00, -5.871685e-01, 9.973097e-01, 6.545821e-01, 9.114037e-02, -9.433860e-01, -1.469063e+00, -4.489590e-01, 1.847529e-01, -1.617276e+00, -2.124964e+00, -5.797045e-01, 2.443566e-01, 1.065121e+00, 1.506727e+00, -5.771380e-03, 5.974856e-01, 3.222021e-01, 1.184963e-01, 7.826490e-01, -7.407951e-02, -4.043330e-01, -3.859515e-01, -5.287699e-01, -3.860550e-01, -1.046112e-01, 4.986993e-01, 3.908327e-01, -6.098467e-03 ], [ -3.631593e+00, -2.450166e+01, -5.423533e+00, -1.905079e+01, -1.945535e+01, -4.651355e+00, -1.164923e+01, 1.066649e+01, -2.179882e+00, -1.033172e+01, -4.678272e+00, -1.146063e+00, 9.894610e-01, 1.279829e+00, 5.487746e-01, -1.567750e-01, 5.368466e-01, -5.418938e-01, -1.980120e-01, -1.922416e+00, -2.380239e+00, -1.192883e+00, 1.307755e-01, 1.459865e+00, 1.601482e+00, -1.252421e-02, 3.835591e-01, 4.835010e-01, 3.622566e-01, 1.399534e+00, 1.932966e-01, -4.817923e-01, 1.580537e-01, -4.482182e-02, -1.587683e-01, -4.263003e-01, 7.625756e-02, -4.941015e-02, -6.395079e-03 ], [ -2.110161e+00, -2.189944e+01, -3.577273e+00, -1.490368e+01, -1.860307e+01, -1.838648e+00, -9.386616e+00, 1.157644e+01, -8.588143e-01, -6.351494e+00, 1.952272e-01, 2.438842e+00, 9.763833e-01, 1.641409e+00, 1.482953e+00, 3.769910e-01, 2.646360e+00, 1.363502e-01, -1.660754e+00, -1.539307e+00, -2.213823e+00, -1.400694e+00, -1.281913e+00, 7.679644e-01, 9.266631e-01, -2.294159e-02, 6.459616e-02, 3.304029e-01, 2.195285e-01, 9.616777e-01, 3.012913e-01, -6.065879e-01, 2.680732e-01, 2.129893e-01, 4.940115e-03, -6.909447e-01, -4.549990e-01, -3.945748e-01, -3.847875e-03 ], [ 4.239744e-02, -2.082980e+01, -3.259567e+00, -9.902978e+00, -1.883495e+01, -4.886611e+00, -1.249734e+01, 8.136093e+00, -2.597832e+00, -9.356349e+00, 8.548814e-01, 2.256215e+00, 9.477813e-01, 1.424646e+00, 1.763293e+00, 3.178869e-01, 3.291507e+00, 6.225432e-01, -1.792925e+00, -7.311083e-01, -1.608112e+00, -7.074323e-01, -1.766296e+00, 3.738186e-01, 6.888947e-01, -2.449871e-02, -1.485645e-01, 8.271299e-02, -3.127924e-02, 2.166191e-01, 2.373298e-01, -4.095209e-01, 2.766629e-01, 2.636068e-01, 1.393205e-01, -5.765115e-01, -5.688236e-01, -4.159141e-01, -9.079840e-04 ], [ 1.624431e+00, -1.775662e+01, -3.748471e+00, -6.696738e+00, -1.709612e+01, -9.690301e+00, -1.333205e+01, 5.609426e+00, -4.097603e+00, -1.489259e+01, -1.807804e+00, 2.345169e+00, 9.034401e-01, 9.051237e-01, 1.135900e+00, -8.310343e-02, 1.962012e+00, 4.752693e-01, -2.050676e+00, -8.725550e-01, -1.446113e+00, -7.977447e-01, -2.261846e+00, -6.668284e-01, -9.852156e-03, -1.902324e-02, -1.992105e-01, -1.321529e-01, -1.321248e-01, -2.698160e-01, 5.306010e-02, -1.037717e-01, 1.192216e-01, 1.697413e-01, 1.115534e-01, -2.455495e-01, -3.910235e-01, -2.571701e-01, 1.331323e-03 ]]) Y = np.array( [[ -1.870935e-01, -1.941152e+01, -5.882407e+00, -6.821157e+00, -1.960176e+01, -9.122212e+00, -5.398831e+00, 1.076720e+01, 6.568266e+00, -4.519125e+00, 3.895056e+00, 5.133921e+00, 8.817558e-01, -3.344995e-01, -5.670179e-01, 1.977534e-01, -2.825252e+00, 5.670564e-02, -4.054463e-01, -1.570377e+00, 2.622198e-01, -1.543853e+00, -2.045755e-01, -3.527825e-01, -9.646030e-01, 2.183463e-02, -1.032675e-01, -2.037469e-01, 6.428432e-02, 3.759445e-01, 1.421797e-01, 6.430891e-01, 4.654990e-01, 3.725418e-01, 1.287469e-01, -1.014837e-01, -2.791396e-01, -1.542485e-01, 2.186694e-03 ], [ -1.019952e+00, -1.993099e+01, -4.057678e+00, -1.526845e+01, -2.177533e+01, -1.245389e+01, -9.609365e+00, 1.133379e+01, 1.232076e+00, -6.754516e+00, 2.665905e+00, 2.272804e+00, 9.301512e-01, -4.683660e-01, -9.745451e-01, 2.767194e-01, -2.455450e+00, 5.393829e-01, 9.000364e-01, -8.480091e-01, 1.262016e+00, -1.286797e+00, -2.833939e-01, -7.601787e-01, -1.068123e+00, 3.039335e-02, -1.688788e-01, -1.540181e-01, 1.790668e-01, 9.829438e-01, 1.804260e-01, 8.561022e-01, 8.271446e-01, 4.591559e-01, 3.011147e-01, -1.383719e-01, -4.674556e-01, -2.046140e-01, -3.690498e-04 ], [ -1.443142e+00, -2.198701e+01, -5.805995e+00, -1.672378e+01, -1.823138e+01, -9.483617e+00, -1.114543e+01, 1.179495e+01, 1.517057e+00, -4.424240e+00, 2.745863e+00, 1.741368e+00, 9.667338e-01, -7.838881e-01, -1.382024e+00, 4.797013e-01, -1.130435e+00, 5.262859e-01, 2.157257e+00, 3.958666e-01, 1.625053e+00, -1.028640e+00, -6.725525e-01, -1.544723e+00, -1.684104e+00, 2.848946e-02, -1.810844e-01, 7.951448e-02, 2.598753e-01, 1.174168e+00, 6.566963e-02, 4.949601e-01, 8.592530e-01, 3.002176e-01, 3.606367e-01, -2.307723e-01, -4.826202e-01, -2.030101e-01, -4.236597e-03 ], [ -1.900802e+00, -2.299645e+01, -4.536950e+00, -1.414699e+01, -1.758996e+01, -4.441329e+00, -6.765486e+00, 1.656342e+01, 2.659910e+00, -5.983455e+00, 6.687205e-01, 1.489522e+00, 9.912346e-01, -9.541904e-01, -9.296216e-01, 9.521492e-01, 1.242009e+00, 7.240354e-01, 2.593670e+00, 1.582148e+00, 1.876561e+00, -2.959121e-01, -6.624181e-01, -2.094018e+00, -1.627936e+00, 1.666238e-02, -6.888758e-02, 3.390231e-01, 3.715679e-02, 6.889561e-01, -7.894406e-02, -1.904353e-01, 3.151930e-01, -2.784935e-01, 3.556462e-01, -9.185118e-02, -8.689702e-02, 1.021361e-01, -7.807227e-03 ], [ -3.666164e+00, -2.478892e+01, -3.244268e+00, -1.303400e+01, -1.906302e+01, -2.341908e+00, -4.841555e+00, 1.627757e+01, 7.112659e-01, -8.267406e+00, -2.829990e+00, -2.894825e+00, 9.936614e-01, -9.970276e-01, -1.918873e-01, 1.159399e+00, 1.196834e+00, 2.927031e-01, 1.222421e+00, 1.510809e+00, 1.455974e+00, -2.361124e-01, -1.168944e+00, -2.098960e+00, -1.699771e+00, 7.517332e-03, 1.816315e-01, 3.902620e-01, -3.455281e-01, -2.452208e-01, -1.617719e-01, -7.106320e-01, -5.168278e-01, -8.029260e-01, 1.800197e-01, 7.171424e-02, 3.953680e-01, 5.738304e-01, -7.401578e-03 ], [ -4.679300e+00, -2.317812e+01, -5.777994e-01, -1.090323e+01, -1.773932e+01, -3.056363e+00, -4.850668e+00, 1.847520e+01, 1.554637e-01, -8.145046e+00, -5.016462e+00, -3.548824e+00, 1.000000e+00, -7.062657e-01, 1.255049e-01, 1.227021e-01, -1.742893e-01, 2.614480e-01, 4.152198e-01, 1.704610e-01, -4.592960e-02, 9.517462e-02, -4.944558e-01, -9.176090e-01, -5.496670e-01, 1.843350e-03, 4.214767e-01, 2.275055e-01, -5.783656e-01, -8.267415e-01, -2.690008e-01, -6.347027e-01, -9.392050e-01, -1.088750e+00, -6.555431e-02, 2.584541e-01, 8.261142e-01, 8.615100e-01, -5.349388e-03 ], [ -5.039043e+00, -2.285567e+01, -1.988579e+00, -1.236157e+01, -1.669317e+01, -4.063885e+00, -4.548844e+00, 1.811908e+01, 1.588668e+00, -9.188303e+00, -4.906452e+00, -4.238289e+00, 9.999387e-01, 3.074194e-04, 4.170531e-02, -8.331995e-01, -1.648457e+00, -5.129647e-02, -3.066546e-01, -1.482461e+00, -1.428380e+00, -3.240738e-01, -3.979310e-01, -1.560897e-01, 6.459251e-01, -1.109232e-03, 5.914558e-01, 1.447010e-01, -3.698424e-01, -2.614930e-01, -2.379599e-01, -3.071502e-01, -8.654104e-01, -9.751623e-01, -2.588414e-01, 3.338255e-01, 9.100384e-01, 8.658913e-01, -4.769736e-03 ], [ -4.745595e+00, -2.333554e+01, -4.551358e+00, -1.535441e+01, -1.746766e+01, -1.504144e+00, -6.059576e+00, 1.541317e+01, 2.697018e+00, -7.995371e+00, -2.880983e+00, -5.871685e-01, 9.973097e-01, 6.545821e-01, 9.114037e-02, -9.433860e-01, -1.469063e+00, -4.489590e-01, 1.847529e-01, -1.617276e+00, -2.124964e+00, -5.797045e-01, 2.443566e-01, 1.065121e+00, 1.506727e+00, -5.771380e-03, 5.974856e-01, 3.222021e-01, 1.184963e-01, 7.826490e-01, -7.407951e-02, -4.043330e-01, -3.859515e-01, -5.287699e-01, -3.860550e-01, -1.046112e-01, 4.986993e-01, 3.908327e-01, -6.098467e-03 ], [ -3.631593e+00, -2.450166e+01, -5.423533e+00, -1.905079e+01, -1.945535e+01, -4.651355e+00, -1.164923e+01, 1.066649e+01, -2.179882e+00, -1.033172e+01, -4.678272e+00, -1.146063e+00, 9.894610e-01, 1.279829e+00, 5.487746e-01, -1.567750e-01, 5.368466e-01, -5.418938e-01, -1.980120e-01, -1.922416e+00, -2.380239e+00, -1.192883e+00, 1.307755e-01, 1.459865e+00, 1.601482e+00, -1.252421e-02, 3.835591e-01, 4.835010e-01, 3.622566e-01, 1.399534e+00, 1.932966e-01, -4.817923e-01, 1.580537e-01, -4.482182e-02, -1.587683e-01, -4.263003e-01, 7.625756e-02, -4.941015e-02, -6.395079e-03 ], [ -2.110161e+00, -2.189944e+01, -3.577273e+00, -1.490368e+01, -1.860307e+01, -1.838648e+00, -9.386616e+00, 1.157644e+01, -8.588143e-01, -6.351494e+00, 1.952272e-01, 2.438842e+00, 9.763833e-01, 1.641409e+00, 1.482953e+00, 3.769910e-01, 2.646360e+00, 1.363502e-01, -1.660754e+00, -1.539307e+00, -2.213823e+00, -1.400694e+00, -1.281913e+00, 7.679644e-01, 9.266631e-01, -2.294159e-02, 6.459616e-02, 3.304029e-01, 2.195285e-01, 9.616777e-01, 3.012913e-01, -6.065879e-01, 2.680732e-01, 2.129893e-01, 4.940115e-03, -6.909447e-01, -4.549990e-01, -3.945748e-01, -3.847875e-03 ], [ 4.239744e-02, -2.082980e+01, -3.259567e+00, -9.902978e+00, -1.883495e+01, -4.886611e+00, -1.249734e+01, 8.136093e+00, -2.597832e+00, -9.356349e+00, 8.548814e-01, 2.256215e+00, 9.477813e-01, 1.424646e+00, 1.763293e+00, 3.178869e-01, 3.291507e+00, 6.225432e-01, -1.792925e+00, -7.311083e-01, -1.608112e+00, -7.074323e-01, -1.766296e+00, 3.738186e-01, 6.888947e-01, -2.449871e-02, -1.485645e-01, 8.271299e-02, -3.127924e-02, 2.166191e-01, 2.373298e-01, -4.095209e-01, 2.766629e-01, 2.636068e-01, 1.393205e-01, -5.765115e-01, -5.688236e-01, -4.159141e-01, -9.079840e-04 ], [ 1.624431e+00, -1.775662e+01, -3.748471e+00, -6.696738e+00, -1.709612e+01, -9.690301e+00, -1.333205e+01, 5.609426e+00, -4.097603e+00, -1.489259e+01, -1.807804e+00, 2.345169e+00, 9.034401e-01, 9.051237e-01, 1.135900e+00, -8.310343e-02, 1.962012e+00, 4.752693e-01, -2.050676e+00, -8.725550e-01, -1.446113e+00, -7.977447e-01, -2.261846e+00, -6.668284e-01, -9.852156e-03, -1.902324e-02, -1.992105e-01, -1.321529e-01, -1.321248e-01, -2.698160e-01, 5.306010e-02, -1.037717e-01, 1.192216e-01, 1.697413e-01, 1.115534e-01, -2.455495e-01, -3.910235e-01, -2.571701e-01, 1.331323e-03 ]]) my_warper = DTW() my_warper.setX(X) my_warper.setY(Y) my_warper.perform_dtw() print(my_warper.get_dtw_dist())