How to use karateclub - 10 common examples

To help you get started, we’ve selected a few karateclub examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github benedekrozemberczki / karateclub / karateclub / node_embedding / attributed / musae.py View on Github external
def fit(self, graph, X):
        """
        Fitting a MUSAE model.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph to be embedded.
            * **X** *(Scipy COO array)* - The binary matrix of node features.
        """
        self._set_seed()
        self._check_graph(graph)
        self.graph = graph
        self._walker = RandomWalker(self.walk_length, self.walk_number)
        self._walker.do_walks(graph)
        self.features = self._feature_transform(graph, X)
        self._base_docs = self._create_base_docs()
        self.embeddings = [self._create_single_embedding(self._base_docs)]
        self._learn_musae_embedding()
github benedekrozemberczki / karateclub / karateclub / node_embedding / neighbourhood / walklets.py View on Github external
def fit(self, graph):
        """
        Fitting a Walklets model.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph to be embedded.
        """
        self._set_seed()
        self._check_graph(graph)
        walker = RandomWalker(self.walk_length, self.walk_number)
        walker.do_walks(graph)
        num_of_nodes = graph.number_of_nodes()

        self._embedding = []
        for power in range(1, self.window_size+1):
            walklets = self._select_walklets(walker.walks, power)
            model = Word2Vec(walklets,
                             hs=0,
                             alpha=self.learning_rate,
                             iter=self.epochs,
                             size=self.dimensions,
                             window=1,
                             min_count=self.min_count,
                             workers=self.workers,
                             seed=self.seed)
github benedekrozemberczki / karateclub / karateclub / node_embedding / attributed / sine.py View on Github external
def fit(self, graph, X):
        """
        Fitting a SINE model.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph to be embedded.
            * **X** *(Scipy COO array)* - The matrix of node features.
        """
        self._set_seed()
        self._check_graph(graph)
        self._walker = RandomWalker(self.walk_length, self.walk_number)
        self._walker.do_walks(graph)
        self._features = self._feature_transform(graph, X)
        self._select_walklets()

        model = Word2Vec(self._walklets,
                         hs=0,
                         alpha=self.learning_rate,
                         iter=self.epochs,
                         size=self.dimensions,
                         window=1,
                         min_count=self.min_count,
                         workers=self.workers,
                         seed=self.seed)

        self.embedding = np.array([model[str(n)] for n in range(graph.number_of_nodes())])
github benedekrozemberczki / karateclub / karateclub / node_embedding / structural / role2vec.py View on Github external
def fit(self, graph):
        """
        Fitting a Role2vec model.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph to be embedded.
        """
        self._set_seed()
        self._check_graph(graph)
        walker = RandomWalker(self.walk_length, self.walk_number)
        walker.do_walks(graph)
 
        hasher = WeisfeilerLehmanHashing(graph=graph, wl_iterations=self.wl_iterations, attributed=False)
      
        node_features = hasher.get_node_features()
        documents = self._create_documents(walker.walks, node_features)

        model = Doc2Vec(documents,
                        vector_size=self.dimensions,
                        window=0,
                        min_count=self.min_count,
                        dm=0,
                        workers=self.workers,
                        sample=self.down_sampling,
                        epochs=self.epochs,
                        alpha=self.learning_rate,
github benedekrozemberczki / karateclub / karateclub / community_detection / non_overlapping / gemsec.py View on Github external
def fit(self, graph):
        """
        Fitting a GEMSEC model.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph to be embedded.
        """
        self._set_seed()
        self._check_graph(graph)
        self._setup_sampling_weights(graph)
        self._walker = RandomWalker(self.walk_length, self.walk_number)
        self._walker.do_walks(graph)
        self._initialize_node_embeddings(graph)
        self._initialize_cluster_centers(graph)
        self._do_gradient_descent()
github benedekrozemberczki / karateclub / karateclub / node_embedding / neighbourhood / deepwalk.py View on Github external
def fit(self, graph):
        """
        Fitting a DeepWalk model.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph to be embedded.
        """
        self._set_seed()
        self._check_graph(graph)
        walker = RandomWalker(self.walk_length, self.walk_number)
        walker.do_walks(graph)

        model = Word2Vec(walker.walks,
                         hs=1,
                         alpha=self.learning_rate,
                         iter=self.epochs,
                         size=self.dimensions,
                         window=self.window_size,
                         min_count=self.min_count,
                         workers=self.workers,
                         seed=self.seed)

        num_of_nodes = graph.number_of_nodes()
        self._embedding = [model[str(n)] for n in range(num_of_nodes)]
github benedekrozemberczki / karateclub / examples.py View on Github external
#-----------------------------------
# Graph reader example
#-----------------------------------

reader = GraphReader("facebook")

graphs = reader.get_graph()
target = reader.get_target()

#-----------------------------------
# Graph2Vec example
#-----------------------------------

graphs = [nx.newman_watts_strogatz_graph(50, 5, 0.3) for _ in range(1000)]

model = Graph2Vec()

model.fit(graphs)
model.get_embedding()

#-----------------------------------
# BoostNE example
#-----------------------------------

g = nx.newman_watts_strogatz_graph(100, 20, 0.05)

model = BoostNE()

model.fit(g)
model.get_embedding()

#-----------------------------------
github benedekrozemberczki / karateclub / examples.py View on Github external
reader = GraphReader("facebook")

graph = reader.get_graph()
target = reader.get_target()

#-------------------------------
# Graph2Vec attributed example
#-------------------------------

graphs = []

for i in range(50):
    graph = nx.newman_watts_strogatz_graph(50, 5, 0.3)
    nx.set_node_attributes(graph, {j: str(j) for j in range(50)}, "feature")
    graphs.append(graph)
model = Graph2Vec(attributed=True)

model.fit(graphs)
model.get_embedding()

#-------------------
# Graph2Vec example
#-------------------

graphs = [nx.newman_watts_strogatz_graph(50, 5, 0.3) for _ in range(1000)]

model = Graph2Vec()

model.fit(graphs)
model.get_embedding()

#-----------------
github benedekrozemberczki / karateclub / examples.py View on Github external
for i in range(50):
    graph = nx.newman_watts_strogatz_graph(50, 5, 0.3)
    nx.set_node_attributes(graph, {j: str(j) for j in range(50)}, "feature")
    graphs.append(graph)
model = Graph2Vec(attributed=True)

model.fit(graphs)
model.get_embedding()

#-------------------
# Graph2Vec example
#-------------------

graphs = [nx.newman_watts_strogatz_graph(50, 5, 0.3) for _ in range(1000)]

model = Graph2Vec()

model.fit(graphs)
model.get_embedding()

#-----------------
# BoostNE example
#-----------------

g = nx.newman_watts_strogatz_graph(100, 20, 0.05)

model = BoostNE()

model.fit(g)
model.get_embedding()

#------------------
github benedekrozemberczki / karateclub / examples.py View on Github external
#-----------------------------------

graphs = [nx.newman_watts_strogatz_graph(50, 5, 0.3) for _ in range(100)]

model = FGSD()

model.fit(graphs)
model.get_embedding()

#-----------------------------------
# NetMF example
#-----------------------------------

g = nx.newman_watts_strogatz_graph(100, 20, 0.05)

model = NetMF()

model.fit(g)
model.get_embedding()

#-----------------------------------
# GraphSet reader example
#-----------------------------------

reader = GraphSetReader("reddit10k")

graphs = reader.get_graphs()
y = reader.get_target()
#-----------------------------------
# Graph reader example
#-----------------------------------