pyrdf2vec.walkers.community module

class pyrdf2vec.walkers.community.CommunityWalker(max_depth, max_walks=None, sampler=NOTHING, n_jobs=None, *, with_reverse=False, random_state=None, hop_prob=0.1, md5_bytes=8, resolution=1)

Bases: pyrdf2vec.walkers.walker.Walker

Community walking strategy which groups vertices with similar properties through probabilities and relations that are not explicitly modeled in a Knowledge Graph. Similar to the Random walking strategy, the Depth First Search (DFS) algorithm is used if a maximum number of walks is specified. Otherwise, the Breadth First Search (BFS) algorithm is chosen.

_is_support_remote

True if the walking strategy can be used with a remote Knowledge Graph, False Otherwise. Defaults to True.

hop_prob

The probability to hop. Defaults to 0.1.

kg

The global KG used later on for the worker process. Defaults to None.

max_depth

The maximum depth of one walk.

max_walks

The maximum number of walks per entity. Defaults to None.

md5_bytes

The number of bytes to keep after hashing objects in MD5. Hasher allows to reduce the memory occupied by a long text. If md5_bytes is None, no hash is applied. Defaults to 8.

random_state

The random state to use to keep random determinism with the walking strategy. Defaults to None.

resolution

The resolution to use. Defaults to The resolution to use.

sampler

The sampling strategy. Defaults to UniformSampler.

with_reverse

True to extracts parents and children hops from an entity, creating (max_walks * max_walks) walks of 2 * depth, allowing also to centralize this entity in the walks. False otherwise. Defaults to False.

extract(kg, entities, verbose=0)

Fits the provided sampling strategy and then calls the private _extract method that is implemented for each of the walking strategies.

Parameters
  • kg (KG) –

    The Knowledge Graph.

    The graph from which the neighborhoods are extracted for the provided entities.

  • entities (List[str]) – The entities to be extracted from the Knowledge Graph.

  • verbose (int) – The verbosity level. 0: does not display anything; 1: display of the progress of extraction and training of walks; 2: debugging. Defaults to 0.

Return type

List[List[Tuple[str, ...]]]

Returns

The 2D matrix with its number of rows equal to the number of provided entities; number of column equal to the embedding size.

extract_walks(kg, entity)

Extracts random walks of depth - 1 hops rooted in root.

Parameters
  • kg (KG) – The Knowledge Graph.

  • entity (Vertex) – The root node to extract walks.

Return type

List[Tuple[Any, ...]]

Returns

The list of unique walks for the provided entity.

pyrdf2vec.walkers.community.check_random_state(seed)
pyrdf2vec.walkers.community.sample_from_iterable(x)