def test_solvers(self): for solver in ['piteration', 'lanczos', 'bicgstab', 'RH']: pagerank = PageRank(solver=solver) scores = pagerank.fit_transform(self.adjacency) self.assertAlmostEqual(0, np.linalg.norm(scores - self.truth)) with self.assertRaises(ValueError): PageRank(solver='toto').fit_transform(self.adjacency)
def test_seeding(self): pr = PageRank() seeds_array = np.zeros(self.n) seeds_array[0] = 1. seeds_dict = {0: 1} scores1 = pr.fit_transform(self.adjacency, seeds_array) scores2 = pr.fit_transform(self.adjacency, seeds_dict) self.assertAlmostEqual(np.linalg.norm(scores1 - scores2), 0.)
def test_copagerank(self): seeds = {0: 1} biadjacency = test_bigraph() adjacency = co_neighbor_graph(biadjacency, method='exact', normalized=True) pagerank = PageRank(damping_factor=0.85, solver='lanczos') pagerank.fit(biadjacency, seeds_row=seeds) scores1 = pagerank.scores_row_ scores1 /= scores1.sum() scores2 = PageRank(damping_factor=0.85**2, solver='lanczos').fit_transform(adjacency, seeds) self.assertAlmostEqual(np.linalg.norm(scores1 - scores2), 0., places=6)
def test_copagerank(self): seeds = {0: 1} biadjacency = test_bigraph() adjacency = co_neighbors_graph(biadjacency, method='exact') scores1 = CoPageRank().fit_transform(biadjacency, seeds) scores2 = PageRank().fit_transform(adjacency, seeds) self.assertAlmostEqual(np.linalg.norm(scores1 - scores2), 0.) adjacency = co_neighbors_graph(biadjacency.T.tocsr(), method='exact') scores1 = CoPageRank().fit(biadjacency, seeds_col=seeds).scores_col_ scores2 = PageRank().fit_transform(adjacency, seeds) self.assertAlmostEqual(np.linalg.norm(scores1 - scores2), 0.) with self.assertRaises(ValueError): CoPageRank(solver='diteration').fit_transform(biadjacency, seeds)
def __init__(self, damping_factor: float = 0.85, solver: str = 'piteration', n_iter: int = 10, tol: float = 0., n_jobs: Optional[int] = None, verbose: bool = False): algorithm = PageRank(damping_factor, solver, n_iter, tol) super(PageRankClassifier, self).__init__(algorithm, n_jobs, verbose)
def test_damping(self): pr = PageRank(damping_factor=0.99) scores = pr.fit_transform(self.adjacency) self.assertAlmostEqual(np.linalg.norm(scores - self.truth), 0.) pr = PageRank(damping_factor=0.01) scores = pr.fit_transform(self.adjacency) self.assertAlmostEqual(np.linalg.norm(scores - self.truth), 0.)
def test_params(self): with self.assertRaises(ValueError): PageRank(damping_factor=1789)
def test_input(self): pagerank = PageRank() scores = pagerank.fit_transform(self.adjacency, force_bipartite=True) self.assertEqual(len(scores), len(pagerank.scores_col_))
def test_pagerank(self): ground_truth = np.ones(3) / 3 adjacency = rock_paper_scissors() pagerank_sps = PageRank(solver='spsolve') pagerank_sps.fit(adjacency) scores = pagerank_sps.scores_ self.assertAlmostEqual(np.linalg.norm(scores - ground_truth), 0.) pagerank_sps.fit(adjacency, personalization=np.array([0, 1, 0])) pagerank_sps.fit(adjacency, personalization={1: 1}) pagerank_high_damping = PageRank(damping_factor=0.99) pagerank_high_damping.fit(adjacency) scores = pagerank_high_damping.scores_ self.assertAlmostEqual(np.linalg.norm(scores - ground_truth), 0., places=1) pagerank_lcz = PageRank(solver='lanczos') pagerank_lcz.fit(adjacency) scores = pagerank_lcz.scores_ self.assertAlmostEqual(np.linalg.norm(scores - ground_truth), 0.) pagerank_lsq = PageRank(solver='lsqr') pagerank_lsq.fit(adjacency) scores = pagerank_lsq.scores_ self.assertAlmostEqual(np.linalg.norm(scores - ground_truth), 0.) pagerank_naive = PageRank(solver=None) pagerank_naive.fit(adjacency)