defcompute_distances_two_loops(self, X): """ Compute the distance between each test point in X and each training point in self.X_train using a nested loop over both the training data and the test data. Inputs: - X: A numpy array of shape (num_test, D) containing test data. Returns: - dists: A numpy array of shape (num_test, num_train) where dists[i, j] is the Euclidean distance between the ith test point and the jth training point. """ num_test = X.shape[0] num_train = self.X_train.shape[0] dists = np.zeros((num_test, num_train)) for i inrange(num_test): for j inrange(num_train): dists[i, j] = np.sqrt(np.sum((X[i] - self.X_train[j]) ** 2)) return dists
defcompute_distances_no_loops(self, X): """ Compute the distance between each test point in X and each training point in self.X_train using no explicit loops. 采用向量化的形式来计算样本之间的距离,是效率最高的计算方式 Input / Output: Same as compute_distances_two_loops """ num_test = X.shape[0] dists = np.sqrt( np.sum(X ** 2, axis=1).reshape([num_test, 1]) + np.sum(self.X_train ** 2, axis=1) - 2 * np.matmul(X, self.X_train.T) ) return dists