def __init__(self, algorithm: BaseRanking, n_jobs: Optional[int] = None, verbose: bool = False): super(RankClassifier, self).__init__() VerboseMixin.__init__(self, verbose) self.algorithm = algorithm self.n_jobs = check_n_jobs(n_jobs) self.verbose = verbose
def __init__(self, embedding_method: BaseEmbedding = GSVD(10), n_neighbors: int = 5, factor_distance: float = 2, leaf_size: int = 16, p: float = 2, tol_nn: float = 0.01, n_jobs: Optional[int] = None): super(KNN, self).__init__() self.embedding_method = embedding_method self.n_neighbors = n_neighbors self.factor_distance = factor_distance self.leaf_size = leaf_size self.p = p self.tol_nn = tol_nn self.n_jobs = check_n_jobs(n_jobs) if self.n_jobs is None: self.n_jobs = -1 self.bipartite = None
def distance(adjacency: sparse.csr_matrix, sources: Optional[Union[int, Iterable]] = None, method: str = 'D', return_predecessors: bool = False, unweighted: bool = False, n_jobs: Optional[int] = None): """Compute distances between nodes. * Graphs * Digraphs Based on SciPy (scipy.sparse.csgraph.shortest_path) Parameters ---------- adjacency : The adjacency matrix of the graph sources : If specified, only compute the paths for the points at the given indices. Will not work with ``method =='FW'``. method : The method to be used. * ``'D'`` (Dijkstra), * ``'BF'`` (Bellman-Ford), * ``'J'`` (Johnson). return_predecessors : If ``True``, the size predecessor matrix is returned unweighted : If ``True``, the weights of the edges are ignored n_jobs : If an integer value is given, denotes the number of workers to use (-1 means the maximum number will be used). If ``None``, no parallel computations are made. Returns ------- dist_matrix : np.ndarray The matrix of distances between graph nodes. ``dist_matrix[i,j]`` gives the shortest distance from point ``i`` to point ``j`` along the graph. If no path exists between nodes ``i`` and ``j``, then ``dist_matrix[i, j] = np.inf``. predecessors : np.ndarray, optional Returned only if ``return_predecessors == True``. The matrix of predecessors, which can be used to reconstruct the shortest paths. Row i of the predecessor matrix contains information on the shortest paths from point ``i``: each entry ``predecessors[i, j]`` gives the index of the previous node in the path from point ``i`` to point ``j``. If no path exists between nodes ``i`` and ``j``, then ``predecessors[i, j] = -9999``. Examples -------- >>> from sknetwork.data import cyclic_digraph >>> adjacency = cyclic_digraph(3) >>> distance(adjacency, sources=0) array([0., 1., 2.]) >>> distance(adjacency, sources=0, return_predecessors=True) (array([0., 1., 2.]), array([-9999, 0, 1])) """ n_jobs = check_n_jobs(n_jobs) if method == 'FW' and n_jobs != 1: raise ValueError( 'The Floyd-Warshall algorithm cannot be used with parallel computations.' ) if sources is None: sources = np.arange(adjacency.shape[0]) elif np.issubdtype(type(sources), np.integer): sources = np.array([sources]) n = len(sources) directed = not is_symmetric(adjacency) local_function = partial(sparse.csgraph.shortest_path, adjacency, method, directed, return_predecessors, unweighted, False) if n_jobs == 1 or n == 1: res = sparse.csgraph.shortest_path(adjacency, method, directed, return_predecessors, unweighted, False, sources) else: with Pool(n_jobs) as pool: res = np.array(pool.map(local_function, sources)) if return_predecessors: if n == 1: return res[0].ravel(), res[1].astype(int).ravel() else: return res[0], res[1].astype(int) else: if n == 1: return res.ravel() else: return res
def shortest_path(adjacency: sparse.csr_matrix, method: str = 'auto', directed: bool = True, return_predecessors: bool = False, unweighted: bool = False, overwrite: bool = False, indices: Optional[np.ndarray] = None, n_jobs: Optional[int] = None): """Compute the shortest paths in the graph. * Graphs * Digraphs Based on SciPy (scipy.sparse.csgraph.shortest_path) Parameters ---------- adjacency: The adjacency matrix of the graph method: The method to be used. Must be ``'auto'`` (default), ``'FW'`` (Floyd-Warshall), ``'D'`` (Dijkstra), ``'BF'`` (Bellman-Ford) or ``'J'`` (Johnson). directed: Denotes if the graph is directed return_predecessors: If ``True``, the size predecessor matrix is returned unweighted: If ``True``, the weights of the edges are ignored overwrite: If ``True`` and ``method == 'FW'``, overwrites the given adjacency indices: If specified, only compute the paths for the points at the given indices. Will not work with ``method =='FW'``. n_jobs: If an integer value is given, denotes the number of workers to use (-1 means the maximum number will be used). If ``None``, no parallel computations are made. Returns ------- dist_matrix: np.ndarray The matrix of distances between graph nodes. ``dist_matrix[i,j]`` gives the shortest distance from point ``i`` to point ``j`` along the graph. predecessors: np.ndarray, optional Returned only if ``return_predecessors == True``. The matrix of predecessors, which can be used to reconstruct the shortest paths. Row i of the predecessor matrix contains information on the shortest paths from point ``i``: each entry ``predecessors[i, j]`` gives the index of the previous node in the path from point ``i`` to point ``j``. If no path exists between point ``i`` and ``j``, then ``predecessors[i, j] = -9999``. """ n_jobs = check_n_jobs(n_jobs) if method == 'FW': raise ValueError( 'The Floyd-Warshall algorithm cannot be used with parallel computations.' ) if indices is None: indices = np.arange(adjacency.shape[0]) local_function = partial(sparse.csgraph.shortest_path, adjacency, method, directed, return_predecessors, unweighted, overwrite) with Pool(n_jobs) as pool: res = pool.map(local_function, indices) if return_predecessors: paths = [el[0] for el in res] predecessors = [el[1] for el in res] return np.vstack(paths), np.vstack(predecessors) else: return np.vstack(res)