Exemplo n.º 1
0
    def fit(self, biadjacency: Union[sparse.csr_matrix, np.ndarray],
            seeds_row: Optional[Union[dict, np.ndarray]] = None,
            seeds_col: Optional[Union[dict, np.ndarray]] = None) -> 'CoPageRank':
        """Fit algorithm to data.

        Parameters
        ----------
        biadjacency :
            Biadjacency matrix.
        seeds_row :
            Seed rows, as a dict or a vector.
        seeds_col :
            Seed columns, as a dict or a vector.
            If both seeds_row and seeds_col are ``None``, the uniform distribution is used.

        Returns
        -------
        self: :class:`CoPageRank`
        """
        biadjacency = check_format(biadjacency)
        n_row, n_col = biadjacency.shape

        operator = CoNeighborsOperator(biadjacency, True)
        seeds_row = seeds2probs(n_row, seeds_row)
        self.scores_row_ = get_pagerank(operator, seeds_row, damping_factor=self.damping_factor, solver=self.solver,
                                        n_iter=self.n_iter, tol=self.tol)

        operator = CoNeighborsOperator(biadjacency.T.tocsr(), True)
        seeds_col = seeds2probs(n_col, seeds_col)
        self.scores_col_ = get_pagerank(operator, seeds_col, damping_factor=self.damping_factor, solver=self.solver,
                                        n_iter=self.n_iter, tol=self.tol)

        self.scores_ = self.scores_row_

        return self
Exemplo n.º 2
0
    def test_diteration(self):
        # test convergence by tolerance
        for adjacency in [house(), test_graph(), test_digraph()]:
            seeds = np.ones(adjacency.shape[0]) / adjacency.shape[0]
            pr = get_pagerank(adjacency,
                              damping_factor=0.85,
                              n_iter=100,
                              tol=10,
                              solver='diteration',
                              seeds=seeds)
            self.assertTrue(is_proba_array(pr))

        # test graph with some null out-degree
        adjacency = edgelist2adjacency([(0, 1)])
        seeds = np.ones(adjacency.shape[0]) / adjacency.shape[0]
        pr = get_pagerank(adjacency,
                          damping_factor=0.85,
                          n_iter=100,
                          tol=10,
                          solver='diteration',
                          seeds=seeds)
        self.assertTrue(is_proba_array(pr))

        # test invalid entry
        adjacency = Regularizer(house(), 0.1)
        seeds = np.ones(adjacency.shape[0]) / adjacency.shape[0]
        with self.assertRaises(ValueError):
            get_pagerank(adjacency,
                         damping_factor=0.85,
                         n_iter=100,
                         tol=10,
                         solver='diteration',
                         seeds=seeds)
Exemplo n.º 3
0
    def fit(self, adjacency: Union[sparse.csr_matrix, np.ndarray, LinearOperator],
            seeds: Optional[Union[dict, np.ndarray]] = None) -> 'PageRank':
        """Fit algorithm to data.

        Parameters
        ----------
        adjacency :
            Adjacency matrix.
        seeds :
            Parameter to be used for Personalized PageRank.
            Restart distribution as a vector or a dict (node: weight).
            If ``None``, the uniform distribution is used (no personalization, default).

        Returns
        -------
        self: :class:`PageRank`
        """
        if not isinstance(adjacency, LinearOperator):
            adjacency = check_format(adjacency)
        check_square(adjacency)
        seeds = seeds2probs(adjacency.shape[0], seeds)
        self.scores_ = get_pagerank(adjacency, seeds, damping_factor=self.damping_factor, n_iter=self.n_iter,
                                    solver=self.solver, tol=self.tol)

        return self
Exemplo n.º 4
0
 def test_piteration(self):
     # test on SparseLR matrix
     adjacency = Regularizer(house(), 0.1)
     seeds = np.ones(adjacency.shape[0]) / adjacency.shape[0]
     pr = get_pagerank(adjacency,
                       damping_factor=0.85,
                       n_iter=100,
                       tol=10,
                       solver='piteration',
                       seeds=seeds)
     self.assertTrue(is_proba_array(pr))
Exemplo n.º 5
0
 def test_push(self):
     # test convergence by tolerance
     adjacency = karate_club()
     seeds = np.ones(adjacency.shape[0]) / adjacency.shape[0]
     pr = get_pagerank(adjacency,
                       damping_factor=0.85,
                       n_iter=100,
                       tol=1e-1,
                       solver='push',
                       seeds=seeds)
     self.assertTrue(is_proba_array(pr))
Exemplo n.º 6
0
    def fit(self,
            input_matrix: Union[sparse.csr_matrix, np.ndarray, LinearOperator],
            seeds: Optional[Union[dict, np.ndarray]] = None,
            seeds_row: Optional[Union[dict, np.ndarray]] = None,
            seeds_col: Optional[Union[dict, np.ndarray]] = None,
            force_bipartite: bool = False) -> 'PageRank':
        """Fit algorithm to data.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.
        seeds :
            Parameter to be used for Personalized PageRank.
            Restart distribution as a vector or a dict (node: weight).
            If ``None``, the uniform distribution is used (no personalization, default).
        seeds_row, seeds_col :
            Parameter to be used for Personalized PageRank on bipartite graphs.
            Restart distribution as vectors or dicts on rows, columns (node: weight).
            If both seeds_row and seeds_col are ``None`` (default), the uniform distribution on rows is used.
        force_bipartite :
            If ``True``, consider the input matrix as the biadjacency matrix of a bipartite graph.
        Returns
        -------
        self: :class:`PageRank`
        """
        adjacency, seeds, self.bipartite = get_adjacency_seeds(
            input_matrix,
            force_bipartite=force_bipartite,
            seeds=seeds,
            seeds_row=seeds_row,
            seeds_col=seeds_col,
            default_value=0,
            which='probs')
        self.scores_ = get_pagerank(adjacency,
                                    seeds,
                                    damping_factor=self.damping_factor,
                                    n_iter=self.n_iter,
                                    solver=self.solver,
                                    tol=self.tol)
        if self.bipartite:
            self._split_vars(input_matrix.shape)
        return self
Exemplo n.º 7
0
    def fit(self, adjacency: Union[sparse.csr_matrix, np.ndarray, LinearOperator],
            seeds: Optional[Union[dict, np.ndarray]] = None) -> 'PageRank':
        """Fit algorithm to data.

        Parameters
        ----------
        adjacency :
            Adjacency matrix.
        seeds :
            If ``None``, the uniform distribution is used.
            Otherwise, a non-negative, non-zero vector or a dictionary must be provided.

        Returns
        -------
        self: :class:`PageRank`
        """
        if not isinstance(adjacency, LinearOperator):
            adjacency = check_format(adjacency)
        check_square(adjacency)
        seeds = seeds2probs(adjacency.shape[0], seeds)
        self.scores_ = get_pagerank(adjacency, seeds, damping_factor=self.damping_factor, n_iter=self.n_iter,
                                    solver=self.solver, tol=self.tol)

        return self