# -*- coding: utf-8 -*- """ Created on Tue Apr 24 22:16:43 2018 @author: Rostislav """ import numpy as np # ============================================================================= # Имитируем обучающие и тестовые выборки # 5 раз произнесли слово "Вставить", обработали и получили # на выходе 5 фреймов по 4 уникальных коэффициента в каждом фрейме # причем, 3 произношения будут относиться к обучающей выборке, а 2 к тестовой # Все то же самое делаем со словами "Копировать" и "Открыть" # ============================================================================= VstTrain1 = np.random.randint(-100, 100, (4,5)) VstTrain2 = np.random.randint(-100, 100, (4,5)) VstTrain3 = np.random.randint(-100, 100, (4,5)) KopTrain1 = np.random.randint(-100, 100, (4,5)) KopTrain2 = np.random.randint(-100, 100, (4,5)) KopTrain3 = np.random.randint(-100, 100, (4,5)) OtkTrain1 = np.random.randint(-100, 100, (4,5)) OtkTrain2 = np.random.randint(-100, 100, (4,5)) OtkTrain3 = np.random.randint(-100, 100, (4,5)) VstTest1 = np.random.randint(-100, 100, (4,5)) VstTest2 = np.random.randint(-100, 100, (4,5)) KopTest1 = np.random.randint(-100, 100, (4,5)) KopTest2 = np.random.randint(-100, 100, (4,5)) OtkTest1 = np.random.randint(-100, 100, (4,5)) OtkTest2 = np.random.randint(-100, 100, (4,5)) # ============================================================================= # Составим 2 словаря с одинаковыми ключами, где элементы словаря представлены # матрицами, имитирующими обучающую и тестовую выборки # ============================================================================= Train = {'Вставить':[VstTrain1, VstTrain2, VstTrain3], 'Копировать':[KopTrain1, KopTrain2, KopTrain3], 'Открыть':[OtkTrain1, OtkTrain2, OtkTrain3]} Test = {'Вставить':[VstTest1, VstTest2], 'Копировать':[KopTest1, KopTest2], 'Открыть':[OtkTest1, OtkTest2]} Ddist = dict() for class_name in Train: Ddist[class_name] = [] for Test1 in Test[class_name]: this_row = [] for Train1 in Train[class_name]: dist = np.linalg.norm(Train1 - Test1) #Вместо DTW будем вычислять норму матриц this_row.append(dist) Ddist[class_name].append(this_row)
As a result, I find the distance (norm) between the test matrix and all the matrices in the training set only inside the key, and it is necessary that the test matrix first finds the norm with all the elements of the training set inside the first key, then finds the norm with all the elements of the second and third training set key. Then the next element of the test sample (VstTest2 matrix) again searches for the norms for all elements of all keys. And so with the entire test sample.
As a result, you need to get either a 6x9 matrix (6 test words (matrices) are compared with 9 words (matrices) from the training sample), where the matrix elements are the distance between the test word i and the training word j, or in cycles to keep the key at the minimum rate and correlate test word (matrix) with this key.