def lmds(matrix, k=2): # Find Landmark points N = matrix.shape[0] landmarks = _getLandmarkPoints(N) num_landmarks = len(landmarks) landmark_matrix = matrix[landmarks] sqdistances = compute_distances(landmark_matrix, landmark_matrix) # Do normal MDS on landmark points means = np.mean(sqdistances, axis=1) # this is called mu_n in the paper global_mean = np.mean(means) # this is called mu in the paper # this is called B in the paper distances_balanced = -(sqdistances - means[np.newaxis, :] - means[:, np.newaxis] + global_mean) / 2 # find the eigenvectors and eigenvalues with our all-purpose hammer # for the paper, Lambda = lambda, Q = V Q, Lambda, _ = np.linalg.svd(distances_balanced) k = min(k, len(Lambda)) mdsarray_sharp = Q[:, :k] # called L^sharp transpose in the paper mdsarray = multiply( mdsarray_sharp, np.sqrt(Lambda)[np.newaxis, :k]) # called L transpose in the paper # Make Triangulation Object return LMDSProjection(landmark_matrix, mdsarray_sharp, means)
def lmds(matrix, k=2): # Find Landmark points N = matrix.shape[0] landmarks = _getLandmarkPoints(N) num_landmarks = len(landmarks) landmark_matrix = matrix[landmarks] sqdistances = compute_distances(landmark_matrix, landmark_matrix) # Do normal MDS on landmark points means = np.mean(sqdistances, axis=1) # this is called mu_n in the paper global_mean = np.mean(means) # this is called mu in the paper # this is called B in the paper distances_balanced = -(sqdistances - means[np.newaxis, :] - means[:, np.newaxis] + global_mean) / 2 # find the eigenvectors and eigenvalues with our all-purpose hammer # for the paper, Lambda = lambda, Q = V Q, Lambda, _ = np.linalg.svd(distances_balanced) k = min(k, len(Lambda)) mdsarray_sharp = Q[:, :k] # called L^sharp transpose in the paper mdsarray = multiply(mdsarray_sharp, np.sqrt(Lambda)[np.newaxis, :k]) # called L transpose in the paper # Make Triangulation Object return LMDSProjection(landmark_matrix, mdsarray_sharp, means)
def compute_distances(matrix1, matrix2): nmatrix1 = normalize(matrix1) nmatrix2 = normalize(matrix2) distances = np.arccos(np.maximum(-1, np.minimum(1, dot(nmatrix1, nmatrix2.T)))) assert isinstance(distances, DenseMatrix) assert distances.same_row_labels_as(nmatrix1) return multiply(distances, distances)
def compute_distances(matrix1, matrix2): nmatrix1 = normalize(matrix1) nmatrix2 = normalize(matrix2) distances = np.arccos( np.maximum(-1, np.minimum(1, dot(nmatrix1, nmatrix2.T)))) assert isinstance(distances, DenseMatrix) assert distances.same_row_labels_as(nmatrix1) return multiply(distances, distances)