def test_pairwise_distance_matrix(get_covmats): n_trials, n_channels = 6, 5 covmats = get_covmats(n_trials, n_channels) n_subset = 4 A, B = covmats[:n_subset], covmats[n_subset:] pdist = pairwise_distance(A, B) assert pdist.shape == (n_subset, 2)
def _get_affinity_matrix(self, X, eps): # make matrix with pairwise distances between points distmatrix = pairwise_distance(X, metric=self.metric) # determine which scale for the gaussian kernel if self.eps is None: eps = np.median(distmatrix)**2 / 2 # make kernel matrix from the distance matrix kernel = np.exp(-distmatrix**2 / (4 * eps)) # normalize the kernel matrix q = np.dot(kernel, np.ones(len(kernel))) kernel_n = np.divide(kernel, np.outer(q, q)) return kernel_n
def test_pairwise_distance_matrix(): """Test pairwise distance""" A = np.array([2 * np.eye(3), 3 * np.eye(3)]) B = np.array([2 * np.eye(3), 3 * np.eye(3)]) pairwise_distance(A, B)
n_subjects = len(np.unique(np.array(metadata['subject']))) ############################################################## # Aggregate all signals for a class from subjects # and compute covmats. Then, compute dist between cov and # pairwise distance simi_vect, simi_mat = {}, {} events = np.unique(y) # ['13', '17', '21', 'rest'] for e in events: ev = aggregate_cov(X, n_subjects, metadata, filterbank=True, s_class=e, estimator='lwf') pdist = pairwise_distance(ev, metric=metric) norm_fact = 1. / np.triu(pdist).sum() simi = 1. / norm_fact * np.triu(pdist) simi_mat[e] = simi simi_vect[e] = simi[simi != 0.] df = pd.DataFrame(simi_vect) ############################################################## # Joint plot for f in events[events != 'rest']: g = sns.jointplot(x="rest", y=f, data=df) plt.savefig('ssvep-jointplot-{0}-rest-{1}.png'.format(f, metric)) ############################################################## # Laplacian Eigenmaps fig = plt.figure(figsize=(12, 3))
def test_pairwise_distance_matrix(): """Test pairwise distance""" A = np.array([2*np.eye(3), 3*np.eye(3)]) B = np.array([2*np.eye(3), 3*np.eye(3)]) pairwise_distance(A, B)