예제 #1
0
    def fit(self,
            input_matrix: Union[sparse.csr_matrix, np.ndarray],
            seeds: Union[np.ndarray, dict] = None,
            seeds_row: Union[np.ndarray, dict] = None,
            seeds_col: Union[np.ndarray, dict] = None) -> 'RankClassifier':
        """Fit algorithm to data.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.
        seeds :
            Seed nodes (labels as dictionary or array; negative values ignored).
        seeds_row, seeds_col :
            Seed rows and columns (for bipartite graphs).
        Returns
        -------
        self: :class:`RankClassifier`
        """
        adjacency, seeds_labels, bipartite = get_adjacency_seeds(
            input_matrix,
            seeds=seeds,
            seeds_row=seeds_row,
            seeds_col=seeds_col)
        seeds_labels = seeds_labels.astype(int)
        classes, n_classes = check_labels(seeds_labels)
        seeds_all = self._process_seeds(seeds_labels)
        local_function = partial(self.algorithm.fit_transform, adjacency)
        with Pool(self.n_jobs) as pool:
            scores = np.array(pool.map(local_function, seeds_all))
        scores = scores.T

        scores = self._process_scores(scores)
        scores = normalize(scores)

        membership = sparse.coo_matrix(scores)
        membership.col = classes[membership.col]

        labels = np.argmax(scores, axis=1)
        self.labels_ = classes[labels]
        self.membership_ = sparse.csr_matrix(membership,
                                             shape=(adjacency.shape[0],
                                                    np.max(seeds_labels) + 1))

        if bipartite:
            self._split_vars(input_matrix.shape)

        return self
예제 #2
0
    def _process_seeds(labels_seeds: np.ndarray) -> list:
        """Make one-vs-all seed labels from seeds.

        Parameters
        ----------
        labels_seeds

        Returns
        -------
        List of seeds vectors.
        """
        seeds_all = []
        classes, _ = check_labels(labels_seeds)

        for label in classes:
            seeds = np.array(labels_seeds == label).astype(int)
            seeds_all.append(seeds)

        return seeds_all
예제 #3
0
    def fit(self, adjacency: Union[sparse.csr_matrix, np.ndarray],
            seeds: Union[np.ndarray, dict]) -> 'RankClassifier':
        """Fit algorithm to the data.

        Parameters
        ----------
        adjacency:
            Adjacency matrix of the graph.
        seeds:
            Seed nodes (labels as dictionary or array; negative values ignored).

        Returns
        -------
        self: :class:`RankClassifier`
        """
        n = adjacency.shape[0]
        seeds_labels = check_seeds(seeds, n).astype(int)
        classes, n_classes = check_labels(seeds_labels)

        seeds_all = self._process_seeds(seeds_labels)
        local_function = partial(self.algorithm.fit_transform, adjacency)
        with Pool(self.n_jobs) as pool:
            scores = np.array(pool.map(local_function, seeds_all))
        scores = scores.T

        scores = self._process_scores(scores)
        scores = normalize(scores)

        membership = sparse.coo_matrix(scores)
        membership.col = classes[membership.col]

        labels = np.argmax(scores, axis=1)
        self.labels_ = classes[labels]
        self.membership_ = sparse.csr_matrix(membership,
                                             shape=(n,
                                                    np.max(seeds_labels) + 1))

        return self
예제 #4
0
def hot_and_cold_seeds(labels_seeds):
    """Make one-vs-all seed labels from seeds.

    Parameters
    ----------
    labels_seeds :

    Returns
    -------
    seeds_all: list
        Personalization vectors.
    """
    seeds_all = []
    classes, _ = check_labels(labels_seeds)

    for label in classes:
        seeds = -np.ones_like(labels_seeds)
        seeds[labels_seeds == label] = 1
        ix = np.logical_and(labels_seeds != label, labels_seeds >= 0)
        seeds[ix] = 0
        seeds_all.append(seeds)

    return seeds_all
예제 #5
0
def process_seeds(labels_seeds, temperature_max: float = 1):
    """Make one-vs-all seed labels from seeds.

    Parameters
    ----------
    labels_seeds :

    temperature_max
    Returns
    -------
    personalizations: list
        Personalization vectors.
    """
    personalizations = []
    classes, _ = check_labels(labels_seeds)

    for label in classes:
        personalization = -np.ones(labels_seeds.shape[0])
        personalization[labels_seeds == label] = temperature_max
        ix = np.logical_and(labels_seeds != label, labels_seeds >= 0)
        personalization[ix] = 0
        personalizations.append(personalization)

    return personalizations