### Walklets Embedding Example Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html Shows how to create a Walklets embedding with minimal changes from the DeepWalk example, highlighting the API-driven design. ```python import networkx as nx from karateclub.node_embedding.neighbourhood import Walklets g = nx.newman_watts_strogatz_graph(100, 20, 0.05) model = Walklets() model.fit(g) embedding = model.get_embedding() ``` -------------------------------- ### Install Karate Club Source: https://karateclub.readthedocs.io/en/latest/notes/installation.html Install the package by using pip: ```bash pip install karateclub ``` -------------------------------- ### DeepWalk Embedding Example Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html Demonstrates how to create a DeepWalk embedding for a Watts-Strogatz graph using the Karate Club library. ```python import networkx as nx from karateclub import DeepWalk g = nx.newman_watts_strogatz_graph(100, 20, 0.05) model = DeepWalk() model.fit(g) embedding = model.get_embedding() ``` -------------------------------- ### Upgrade Karate Club Source: https://karateclub.readthedocs.io/en/latest/notes/installation.html Upgrade your outdated Karate Club version by using: ```bash pip install karateclub --upgrade ``` -------------------------------- ### MNMF Example Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/community_detection/overlapping/mnmf.html This example demonstrates how to use the MNMF algorithm to detect overlapping communities in a graph. ```python import networkx as nx from karateclub import MNMF # Load a graph (example: Zachary's Karate Club) G = nx.karate_club_graph() # Initialize and train the MNMF model model = MNMF(dimensions=16, iteration=20, lambd=1.0, gamma=0.1) model.fit(G) # Get the community embeddings embeddings = model.get_embedding() # Get the detected communities (overlapping) communities = model.get_communities() print("Community Embeddings:", embeddings) print("Detected Communities:", communities) ``` -------------------------------- ### Check Karate Club version Source: https://karateclub.readthedocs.io/en/latest/notes/installation.html To check your current package version just simply run: ```bash pip freeze | grep karateclub ``` -------------------------------- ### Setup Q Method Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/community_detection/overlapping/danmf.html Method to set up the Q matrices, which are used in the update steps. ```python def _setup_Q(self): """ Setting up Q matrices. """ self._Q_s = [None for _ in range(self._p + 1)] self._Q_s[self._p] = np.eye(self.layers[self._p - 1]) for i in range(self._p - 1, -1, -1): self._Q_s[i] = np.dot(self._U_s[i], self._Q_s[i + 1]) ``` -------------------------------- ### Setup Target Matrices Method Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/community_detection/overlapping/danmf.html Method to set up the target matrices (adjacency, Laplacian, and degree) for the pre-training process. ```python def _setup_target_matrices(self, graph): """ Setup target matrix for pre-training process. Arg types: * **graph** *(NetworkX graph)* - The graph being clustered. """ self._graph = graph self._A = nx.adjacency_matrix( self._graph, nodelist=range(self._graph.number_of_nodes()) ) self._L = nx.laplacian_matrix( self._graph, nodelist=range(self._graph.number_of_nodes()) ) self._D = self._L + self._A ``` -------------------------------- ### GEMSEC Helper Methods Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/community_detection/non_overlapping/gemsec.html Internal helper methods for GEMSEC, including setup for sampling weights, initializing embeddings and cluster centers, sampling negative examples, and calculating noise and cluster vectors. ```python def _setup_sampling_weights(self, graph): """ Creating a negative sampling table. Arg types: * **graph** *(NetworkX graph)* - The graph for negative sampling. """ self._sampler = {} index = 0 for node in graph.nodes(): for _ in range(graph.degree(node)): self._sampler[index] = node index = index + 1 self._global_index = index - 1 def _initialize_node_embeddings(self, graph): """ Creating a node embedding array. Arg types: * **graph** *(NetworkX graph)* - The graph for negative sampling. """ shape = (graph.number_of_nodes(), self.dimensions) self._base_embedding = np.random.normal(0, 1.0 / self.dimensions, shape) def _initialize_cluster_centers(self, graph): """ Creating a cluster center array. Arg types: * **graph** *(NetworkX graph)* - The graph for negative sampling. """ shape = (self.dimensions, self.clusters) self._cluster_centers = np.random.normal(0, 1.0 / self.dimensions, shape) def _sample_negative_samples(self): """ Sampling a batch of nodes as negative samples. Return types: * **negative_samples** *(list)*: List of negative sampled nodes. """ negative_samples = [ self._sampler[random.randint(0, self._global_index)] for _ in range(self.negative_samples) ] return negative_samples def _calculcate_noise_vector(self, negative_samples, source_node): """ Getting the noise vector for the weight update. Arg types: * **negative_samples** *(list)*: List of negative sampled nodes. * **source_node** *(int)* - Source node in the walk. Return types: * **noise_vector** *(NumPy array) - Noise update vector. """ noise_vectors = self._base_embedding[negative_samples, :] source_vector = self._base_embedding[int(source_node), :] raw_scores = noise_vectors.dot(source_vector.T) raw_scores = np.exp(np.clip(raw_scores, -15, 15)) scores = raw_scores / np.sum(raw_scores) scores = scores.reshape(-1, 1) noise_vector = np.sum(scores * noise_vectors, axis=0) return noise_vector def _calculate_cluster_vector(self, source_node): """ Getting the cluster vector for the weight update. Arg types: * **source_node** *(int)* - Source node in the walk. Return types: * **cluster_vector** *(NumPy array) - Cluster update vector. * **cluster_index** *(int)*: Node cluster membership index. """ distances = ( self._base_embedding[int(source_node), :].reshape(-1, 1) - self._cluster_centers ) ``` -------------------------------- ### Loading the Reddit 10K dataset Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet shows how to load the Reddit 10K dataset using GraphSetReader, obtaining both the graphs and their corresponding target vectors. ```python from karateclub.dataset import GraphSetReader reader = GraphSetReader("reddit10k") graphs = reader.get_graphs() y = reader.get_target() ``` -------------------------------- ### Loading Facebook Dataset for Community Detection Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet shows how to load the Facebook page-page network dataset using GraphReader and retrieve the graph and target data. ```python from karateclub import GraphReader reader = GraphReader("facebook") graph = reader.get_graph() target = reader.get_target() ``` -------------------------------- ### Node Embedding with Diff2Vec Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet demonstrates applying the Diff2Vec model for node embedding with custom parameters and retrieving the embedding features. ```python from karateclub import Diff2Vec model = Diff2Vec(diffusion_number=2, diffusion_cover=20, dimensions=16) model.fit(graph) X = model.get_embedding() ``` -------------------------------- ### TENET Model Example Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/node_embedding/attributed/tene.html This example demonstrates how to use the TENET model for node embedding. It includes initializing the model, fitting it to the data, and obtaining node embeddings. ```python from karateclub.node_embedding.attributed import TENET from karateclub.utils.data import load_data # Load a sample dataset adj, features, labels = load_data("cora") # Initialize the TENET model model = TENET(dimensions=128, epochs=10, learning_rate=0.01, decay=0.001, lambd=1.0, nu=1.0, seed=42) # Fit the model to the data model.fit(adj, features) # Get node embeddings embeddings = model.get_embedding() print(f"Shape of embeddings: {embeddings.shape}") ``` -------------------------------- ### Fitting a FEATHER graph level embedding Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet demonstrates fitting a FEATHER graph embedding model to the loaded graphs and retrieving the resulting graph embeddings. ```python from karateclub import FeatherGraph model = FeatherGraph() model.fit(graphs) X = model.get_embedding() ``` -------------------------------- ### Loading Twitch UK Dataset for Node Embedding Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet shows how to load the Twitch UK user friendship dataset using GraphReader and retrieve the graph and target data. ```python from karateclub.dataset import GraphReader reader = GraphReader("twitch") graph = reader.get_graph() y = reader.get_target() ``` -------------------------------- ### get_embedding Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/graph_embedding/waveletcharacteristic.html Getting the embedding of graphs. ```python def get_embedding(self) -> np.array: r"""Getting the embedding of graphs. Return types: * **embedding** *(Numpy array)* - The embedding of graphs. """ return np.array(self._embedding) ``` -------------------------------- ### NetLSD get_embedding method Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/graph_embedding/netlsd.html Getting the embedding of graphs. ```python def get_embedding(self) -> np.array: r"""Getting the embedding of graphs. Return types: * **embedding** *(Numpy array)* - The embedding of graphs. """ return np.array(self._embedding) ``` -------------------------------- ### Training a logistic regression model and evaluating performance Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet demonstrates training a logistic regression model on the training data, predicting probabilities on the test set, and evaluating performance using the ROC AUC score. ```python from sklearn.metrics import roc_auc_score from sklearn.linear_model import LogisticRegression downstream_model = LogisticRegression(random_state=0).fit(X_train, y_train) y_hat = downstream_model.predict_proba(X_test)[:, 1] auc = roc_auc_score(y_test, y_hat) print('AUC: {:.4f}'.format(auc)) >>> AUC: 0.7127 ``` -------------------------------- ### Setup Base Model Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/node_embedding/neighbourhood/boostne.html Initializes the base model by setting up the shape, indices, and performing the initial NMF fit and score. ```python def _setup_base_model(self): """ Fitting NMF on the starting matrix. """ self._shape = self._residuals.shape indices = self._residuals.nonzero() self._index_1 = indices[0] self._index_2 = indices[1] base_score, embedding = self._fit_and_score_NMF(self._residuals) self._embeddings = [embedding] ``` -------------------------------- ### NMF-ADMM get_embedding method Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/node_embedding/neighbourhood/nmfadmm.html Getting the node embedding. ```python def get_embedding(self) -> np.array: r"""Getting the node embedding. Return types: * **embedding** *(Numpy array)* - The embedding of nodes. """ embedding = np.concatenate([self._W_plus, self._H_plus.T], axis=1) return embedding ``` -------------------------------- ### Get Embedding Method Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/graph_embedding/gl2vec.html Retrieves the computed graph embeddings. ```python def get_embedding(self) -> np.array: r"""Getting the embedding of graphs. Return types: * **embedding** *(Numpy array)* - The embedding of graphs. """ return np.array(self._embedding) ``` -------------------------------- ### Clustering a Random Watts-Strogatz Graph Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet illustrates clustering an arbitrary NetworkX graph, such as a randomly generated Watts-Strogatz graph, using Label Propagation. ```python import networkx as nx from karateclub import LabelPropagation graph = nx.newman_watts_strogatz_graph(100, 20, 0.05) model = LabelPropagation() model.fit(graph) cluster_membership = model.get_memberships() ``` -------------------------------- ### GraphWave get_embedding method Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/node_embedding/structural/graphwave.html Getting the node embedding. ```python def get_embedding(self) -> np.array: r"""Getting the node embedding. Return types: * **embedding** *(Numpy array)* - The embedding of nodes. """ embedding = [self._real_and_imaginary.real, self._real_and_imaginary.imag] embedding = np.concatenate(embedding, axis=1) return embedding ``` -------------------------------- ### Evaluating Clustering with Normalized Mutual Information Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet shows how to evaluate the quality of the detected clusters using normalized mutual information (NMI) against the ground truth. ```python from sklearn.metrics.cluster import normalized_mutual_info_score cluster_membership = [cluster_membership[node] for node in range(len(cluster_membership))] nmi = normalized_mutual_info_score(target, cluster_membership) print('NMI: {:.4f}'.format(nmi)) ``` -------------------------------- ### Getting the node embedding Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/node_embedding/attributed/fscnmf.html This method returns the learned node embedding. ```python embedding = np.concatenate([self._B_1, self._U], axis=1) return embedding ``` -------------------------------- ### Role2Vec Embedding Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/node_embedding/structural/role2vec.html This snippet shows how to get the node embedding after the model has been trained. ```python alpha=self.learning_rate, seed=self.seed, ) self._embedding = [model.dv[str(i)] for i, _ in enumerate(documents)] ``` -------------------------------- ### Logistic Regression for Abusive User Prediction Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet demonstrates training a logistic regression model on the node embeddings and evaluating its performance using AUC. ```python from sklearn.metrics import roc_auc_score from sklearn.linear_model import LogisticRegression downstream_model = LogisticRegression(random_state=0).fit(X_train, y_train) y_hat = downstream_model.predict_proba(X_test)[:, 1] auc = roc_auc_score(y_test, y_hat) print('AUC: {:.4f}'.format(auc)) ``` -------------------------------- ### Get Embedding Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/community_detection/overlapping/mnmf.html This method returns the node embedding, which is stored in the U matrix. ```python def get_embedding(self) -> np.array: r"""Getting the node embedding. Return types: * **embedding** *(Numpy array)* - The embedding of nodes. """ embedding = self._U return embedding ``` -------------------------------- ### Community Detection with Label Propagation Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet demonstrates applying the Label Propagation algorithm for community detection on a graph and obtaining cluster memberships. ```python from karateclub import LabelPropagation model = LabelPropagation() model.fit(graph) cluster_membership = model.get_memberships() ``` -------------------------------- ### GEMSEC Get Memberships Method Source: https://karateclub.readthedocs.io/en/latest/modules/root.html Retrieves the cluster memberships for each node from the GEMSEC model. ```python get_memberships() -> Dict[int, int] ``` -------------------------------- ### Get Embedding Source: https://karateclub.readthedocs.io/en/latest/_modules/karateclub/node_embedding/neighbourhood/boostne.html Retrieves the final node embedding by concatenating all learned embeddings. ```python def get_embedding(self) -> np.array: r"""Getting the node embedding. Return types: * **embedding** *(Numpy array)* - The embedding of nodes. """ embedding = np.concatenate(self._embeddings, axis=1) return embedding ``` -------------------------------- ### LabelPropagation Class Initialization Source: https://karateclub.readthedocs.io/en/latest/modules/root.html Initializes the Label Propagation model for community detection. ```python LabelPropagation(seed: int = 42, iterations: int = 100) ``` -------------------------------- ### Train-Test Split for Node Embedding Features Source: https://karateclub.readthedocs.io/en/latest/notes/introduction.html This snippet shows how to split the node embedding features and target variable into training and testing sets for downstream classification. ```python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ```