def test_knn_graph_sparse(): k = 3 n_pca = 20 pca = TruncatedSVD(n_pca, random_state=42).fit(data) data_nu = pca.transform(data) pdx = squareform(pdist(data_nu, metric="euclidean")) knn_dist = np.partition(pdx, k, axis=1)[:, :k] epsilon = np.max(knn_dist, axis=1) K = np.empty_like(pdx) for i in range(len(pdx)): K[i, pdx[i, :] <= epsilon[i]] = 1 K[i, pdx[i, :] > epsilon[i]] = 0 K = K + K.T W = np.divide(K, 2) np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph( sp.coo_matrix(data), n_pca=n_pca, decay=None, knn=k - 1, random_state=42, use_pygsp=True, ) assert G.N == G2.N np.testing.assert_allclose(G2.W.toarray(), G.W.toarray()) assert isinstance(G2, graphtools.graphs.kNNGraph)
def test_knn_graph_multiplication_symm(): k = 3 n_pca = 20 pca = PCA(n_pca, svd_solver="randomized", random_state=42).fit(data) data_nu = pca.transform(data) pdx = squareform(pdist(data_nu, metric="euclidean")) knn_dist = np.partition(pdx, k, axis=1)[:, :k] epsilon = np.max(knn_dist, axis=1) K = np.empty_like(pdx) for i in range(len(pdx)): K[i, pdx[i, :] <= epsilon[i]] = 1 K[i, pdx[i, :] > epsilon[i]] = 0 W = K * K.T np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph( data, n_pca=n_pca, decay=None, knn=k - 1, random_state=42, use_pygsp=True, kernel_symm="*", ) assert G.N == G2.N np.testing.assert_equal(G.dw, G2.dw) assert (G.W - G2.W).nnz == 0 assert (G2.W - G.W).sum() == 0 assert isinstance(G2, graphtools.graphs.kNNGraph)
def test_knn_graph(): k = 3 n_pca = 20 pca = PCA(n_pca, svd_solver='randomized', random_state=42).fit(data) data_nu = pca.transform(data) pdx = squareform(pdist(data_nu, metric='euclidean')) knn_dist = np.partition(pdx, k, axis=1)[:, :k] epsilon = np.max(knn_dist, axis=1) K = np.empty_like(pdx) for i in range(len(pdx)): K[i, pdx[i, :] <= epsilon[i]] = 1 K[i, pdx[i, :] > epsilon[i]] = 0 K = K + K.T W = np.divide(K, 2) np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph(data, n_pca=n_pca, decay=None, knn=k, random_state=42, use_pygsp=True) assert(G.N == G2.N) assert(np.all(G.d == G2.d)) assert((G.W != G2.W).nnz == 0) assert((G2.W != G.W).sum() == 0) assert(isinstance(G2, graphtools.graphs.kNNGraph))
def test_knn_graph(): k = 3 n_pca = 20 pca = PCA(n_pca, svd_solver="randomized", random_state=42).fit(data) data_nu = pca.transform(data) pdx = squareform(pdist(data_nu, metric="euclidean")) knn_dist = np.partition(pdx, k, axis=1)[:, :k] epsilon = np.max(knn_dist, axis=1) K = np.empty_like(pdx) for i in range(len(pdx)): K[i, pdx[i, :] <= epsilon[i]] = 1 K[i, pdx[i, :] > epsilon[i]] = 0 K = K + K.T W = np.divide(K, 2) np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph(data, n_pca=n_pca, decay=None, knn=k - 1, random_state=42, use_pygsp=True) assert G.N == G2.N np.testing.assert_equal(G.dw, G2.dw) assert (G.W - G2.W).nnz == 0 assert (G2.W - G.W).sum() == 0 assert isinstance(G2, graphtools.graphs.kNNGraph) K2 = G2.build_kernel_to_data(G2.data_nu, knn=k) K2 = (K2 + K2.T) / 2 assert (G2.K - K2).nnz == 0 assert (G2.build_kernel_to_data( G2.data_nu, knn=data.shape[0]).nnz == data.shape[0] * data.shape[0]) with assert_warns_message( UserWarning, "Cannot set knn ({}) to be greater than " "n_samples ({}). Setting knn={}".format(data.shape[0] + 1, data.shape[0], data.shape[0]), ): G2.build_kernel_to_data( Y=G2.data_nu, knn=data.shape[0] + 1, )