def jaccard(input_graph, vertex_pair=None): """ Compute the Jaccard similarity between each pair of vertices connected by an edge, or between arbitrary pairs of vertices specified by the user. Jaccard similarity is defined between two sets as the ratio of the volume of their intersection divided by the volume of their union. In the context of graphs, the neighborhood of a vertex is seen as a set. The Jaccard similarity weight of each edge represents the strength of connection between vertices based on the relative similarity of their neighbors. If first is specified but second is not, or vice versa, an exception will be thrown. NOTE: If the vertex_pair parameter is not specified then the behavior of cugraph.jaccard is different from the behavior of networkx.jaccard_coefficient. cugraph.jaccard, in the absence of a specified vertex pair list, will use the edges of the graph to construct a vertex pair list and will return the jaccard coefficient for those vertex pairs. networkx.jaccard_coefficient, in the absence of a specified vertex pair list, will return an upper triangular dense matrix, excluding the diagonal as well as vertex pairs that are directly connected by an edge in the graph, of jaccard coefficients. Technically, networkx returns a lazy iterator across this upper triangular matrix where the actual jaccard coefficient is computed when the iterator is dereferenced. Computing a dense matrix of results is not feasible if the number of vertices in the graph is large (100,000 vertices would result in 4.9 billion values in that iterator). If your graph is small enough (or you have enough memory and patience) you can get the interesting (non-zero) values that are part of the networkx solution by doing the following: >>> gdf = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(gdf, source='0', destination='1') >>> pairs = cugraph.get_two_hop_neighbors(G) >>> df = cugraph.jaccard(G, pairs) But please remember that cugraph will fill the dataframe with the entire solution you request, so you'll need enough memory to store the 2-hop neighborhood dataframe. Parameters ---------- graph : cugraph.Graph cuGraph graph descriptor, should contain the connectivity information as an edge list (edge weights are not used for this algorithm). The graph should be undirected where an undirected edge is represented by a directed edge in both direction. The adjacency list will be computed if not already present. vertex_pair : cudf.DataFrame A GPU dataframe consisting of two columns representing pairs of vertices. If provided, the jaccard coefficient is computed for the given vertex pairs. If the vertex_pair is not provided then the current implementation computes the jaccard coefficient for all adjacent vertices in the graph. Returns ------- df : cudf.DataFrame GPU data frame of size E (the default) or the size of the given pairs (first, second) containing the Jaccard weights. The ordering is relative to the adjacency list, or that given by the specified vertex pairs. df['source'] : cudf.Series The source vertex ID (will be identical to first if specified) df['destination'] : cudf.Series The destination vertex ID (will be identical to second if specified) df['jaccard_coeff'] : cudf.Series The computed Jaccard coefficient between the source and destination vertices Examples -------- >>> gdf = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(gdf, source='0', destination='1') >>> df = cugraph.jaccard(G) """ if type(input_graph) is not Graph: raise Exception("input graph must be undirected") # FIXME: Add support for multi-column vertices if type(vertex_pair) == cudf.DataFrame: for col in vertex_pair.columns: null_check(vertex_pair[col]) if input_graph.renumbered: vertex_pair = input_graph.add_internal_vertex_id( vertex_pair, col, col) elif vertex_pair is None: pass else: raise ValueError("vertex_pair must be a cudf dataframe") df = jaccard_wrapper.jaccard(input_graph, None, vertex_pair) if input_graph.renumbered: df = input_graph.unrenumber(df, "source") df = input_graph.unrenumber(df, "destination") return df
def jaccard(input_graph, first=None, second=None): """ Compute the Jaccard similarity between each pair of vertices connected by an edge, or between arbitrary pairs of vertices specified by the user. Jaccard similarity is defined between two sets as the ratio of the volume of their intersection divided by the volume of their union. In the context of graphs, the neighborhood of a vertex is seen as a set. The Jaccard similarity weight of each edge represents the strength of connection between vertices based on the relative similarity of their neighbors. If first is specified but second is not, or vice versa, an exception will be thrown. Parameters ---------- graph : cugraph.Graph cuGraph graph descriptor, should contain the connectivity information as an edge list (edge weights are not used for this algorithm). The graph should be undirected where an undirected edge is represented by a directed edge in both direction. The adjacency list will be computed if not already present. first : cudf.Series Specifies the first vertices of each pair of vertices to compute for, must be specified along with second. second : cudf.Series Specifies the second vertices of each pair of vertices to compute for, must be specified along with first. Returns ------- df : cudf.DataFrame GPU data frame of size E (the default) or the size of the given pairs (first, second) containing the Jaccard weights. The ordering is relative to the adjacency list, or that given by the specified vertex pairs. df['source'] : cudf.Series The source vertex ID (will be identical to first if specified) df['destination'] : cudf.Series The destination vertex ID (will be identical to second if specified) df['jaccard_coeff'] : cudf.Series The computed Jaccard coefficient between the source and destination vertices Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> sources = cudf.Series(M['0']) >>> destinations = cudf.Series(M['1']) >>> G = cugraph.Graph() >>> G.add_edge_list(sources, destinations, None) >>> df = cugraph.jaccard(G) """ if (type(first) == cudf.Series and type(second) == cudf.Series): null_check(first) null_check(second) elif first is None and second is None: pass else: raise ValueError("Specify first and second or neither") df = jaccard_wrapper.jaccard(input_graph.graph_ptr, first, second) return df
def jaccard_w(input_graph, weights, vertex_pair=None): """ Compute the weighted Jaccard similarity between each pair of vertices connected by an edge, or between arbitrary pairs of vertices specified by the user. Jaccard similarity is defined between two sets as the ratio of the volume of their intersection divided by the volume of their union. In the context of graphs, the neighborhood of a vertex is seen as a set. The Jaccard similarity weight of each edge represents the strength of connection between vertices based on the relative similarity of their neighbors. If first is specified but second is not, or vice versa, an exception will be thrown. Parameters ---------- graph : cugraph.Graph cuGraph graph descriptor, should contain the connectivity information as an edge list (edge weights are not used for this algorithm). The adjacency list will be computed if not already present. weights : cudf.DataFrame Specifies the weights to be used for each vertex. Vertex should be represented by multiple columns for multi-column vertices. weights['vertex'] : cudf.Series Contains the vertex identifiers weights['weight'] : cudf.Series Contains the weights of vertices vertex_pair : cudf.DataFrame A GPU dataframe consisting of two columns representing pairs of vertices. If provided, the jaccard coefficient is computed for the given vertex pairs, else, it is computed for all vertex pairs. Returns ------- df : cudf.DataFrame GPU data frame of size E (the default) or the size of the given pairs (first, second) containing the Jaccard weights. The ordering is relative to the adjacency list, or that given by the specified vertex pairs. df['source'] : cudf.Series The source vertex ID df['destination'] : cudf.Series The destination vertex ID df['jaccard_coeff'] : cudf.Series The computed weighted Jaccard coefficient between the source and destination vertices. Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, source='0', destination='1') >>> df = cugraph.jaccard_w(G, M[2]) """ if type(input_graph) is not Graph: raise Exception("input graph must be undirected") if type(vertex_pair) == cudf.DataFrame: vertex_pair = renumber_vertex_pair(input_graph, vertex_pair) elif vertex_pair is None: pass else: raise ValueError("vertex_pair must be a cudf dataframe") if input_graph.renumbered: vertex_size = input_graph.vertex_column_size() if vertex_size == 1: weights = input_graph.add_internal_vertex_id( weights, 'vertex', 'vertex' ) else: cols = weights.columns[:vertex_size].to_list() weights = input_graph.add_internal_vertex_id( weights, 'vertex', cols ) jaccard_weights = cudf.Series(np.ones(len(weights))) for i in range(len(weights)): jaccard_weights[weights['vertex'].iloc[i]] = weights['weight'].iloc[i] df = jaccard_wrapper.jaccard(input_graph, jaccard_weights, vertex_pair) if input_graph.renumbered: df = input_graph.unrenumber(df, "source") df = input_graph.unrenumber(df, "destination") return df
def sorensen_w(input_graph, weights, vertex_pair=None): """ Compute the weighted Sorensen similarity between each pair of vertices connected by an edge, or between arbitrary pairs of vertices specified by the user. Sorensen coefficient is defined between two sets as the ratio of twice the volume of their intersection divided by the volume of each set. Parameters ---------- input_graph : cugraph.Graph cuGraph Graph instance, should contain the connectivity information as an edge list (edge weights are not used for this algorithm). The adjacency list will be computed if not already present. weights : cudf.DataFrame Specifies the weights to be used for each vertex. Vertex should be represented by multiple columns for multi-column vertices. weights['vertex'] : cudf.Series Contains the vertex identifiers weights['weight'] : cudf.Series Contains the weights of vertices vertex_pair : cudf.DataFrame, optional (default=None) A GPU dataframe consisting of two columns representing pairs of vertices. If provided, the sorensen coefficient is computed for the given vertex pairs, else, it is computed for all vertex pairs. Returns ------- df : cudf.DataFrame GPU data frame of size E (the default) or the size of the given pairs (first, second) containing the Sorensen weights. The ordering is relative to the adjacency list, or that given by the specified vertex pairs. df['source'] : cudf.Series The source vertex ID df['destination'] : cudf.Series The destination vertex ID df['sorensen_coeff'] : cudf.Series The computed weighted Sorensen coefficient between the source and destination vertices. Examples -------- >>> import random >>> M = cudf.read_csv(datasets_path / 'karate.csv', delimiter=' ', ... dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, source='0', destination='1') >>> # Create a dataframe containing the vertices with their >>> # corresponding weight >>> weights = cudf.DataFrame() >>> # Sample 10 random vertices from the graph and drop duplicates if >>> # there are any to avoid duplicates vertices with different weight >>> # value in the 'weights' dataframe >>> weights['vertex'] = G.nodes().sample(n=10).drop_duplicates() >>> # Reset the indices and drop the index column >>> weights.reset_index(inplace=True, drop=True) >>> # Create a weight column with random weights >>> weights['weight'] = [random.random() for w in range( ... len(weights['vertex']))] >>> df = cugraph.sorensen_w(G, weights) """ if type(input_graph) is not Graph: raise TypeError("input graph must a Graph") if type(vertex_pair) == cudf.DataFrame: vertex_pair = renumber_vertex_pair(input_graph, vertex_pair) elif vertex_pair is not None: raise ValueError("vertex_pair must be a cudf dataframe") if input_graph.renumbered: vertex_size = input_graph.vertex_column_size() if vertex_size == 1: weights = input_graph.add_internal_vertex_id( weights, 'vertex', 'vertex') else: cols = weights.columns[:vertex_size].to_list() weights = input_graph.add_internal_vertex_id( weights, 'vertex', cols) jaccard_weights = weights['weight'] df = jaccard_wrapper.jaccard(input_graph, jaccard_weights, vertex_pair) df.jaccard_coeff = ((2 * df.jaccard_coeff) / (1 + df.jaccard_coeff)) df.rename({'jaccard_coeff': 'sorensen_coeff'}, axis=1, inplace=True) if input_graph.renumbered: df = input_graph.unrenumber(df, "source") df = input_graph.unrenumber(df, "destination") return df
def jaccard_w(input_graph, weights, vertex_pair=None): """ Compute the weighted Jaccard similarity between each pair of vertices connected by an edge, or between arbitrary pairs of vertices specified by the user. Jaccard similarity is defined between two sets as the ratio of the volume of their intersection divided by the volume of their union. In the context of graphs, the neighborhood of a vertex is seen as a set. The Jaccard similarity weight of each edge represents the strength of connection between vertices based on the relative similarity of their neighbors. If first is specified but second is not, or vice versa, an exception will be thrown. Parameters ---------- graph : cugraph.Graph cuGraph graph descriptor, should contain the connectivity information as an edge list (edge weights are not used for this algorithm). The adjacency list will be computed if not already present. weights : cudf.Series Specifies the weights to be used for each vertex. vertex_pair : cudf.DataFrame A GPU dataframe consisting of two columns representing pairs of vertices. If provided, the jaccard coefficient is computed for the given vertex pairs, else, it is computed for all vertex pairs. Returns ------- df : cudf.DataFrame GPU data frame of size E (the default) or the size of the given pairs (first, second) containing the Jaccard weights. The ordering is relative to the adjacency list, or that given by the specified vertex pairs. df['source'] : cudf.Series The source vertex ID df['destination'] : cudf.Series The destination vertex ID df['jaccard_coeff'] : cudf.Series The computed weighted Jaccard coefficient between the source and destination vertices. Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, source='0', destination='1') >>> df = cugraph.jaccard_w(G, M[2]) """ if type(input_graph) is not Graph: raise Exception("input graph must be undirected") if (type(vertex_pair) == cudf.DataFrame): null_check(vertex_pair[vertex_pair.columns[0]]) null_check(vertex_pair[vertex_pair.columns[1]]) elif vertex_pair is None: pass else: raise ValueError("vertex_pair must be a cudf dataframe") df = jaccard_wrapper.jaccard(input_graph, weights, vertex_pair) return df
def sorensen(input_graph, vertex_pair=None): """ Compute the Sorensen coefficient between each pair of vertices connected by an edge, or between arbitrary pairs of vertices specified by the user. Sorensen coefficient is defined between two sets as the ratio of twice the volume of their intersection divided by the volume of each set. If first is specified but second is not, or vice versa, an exception will be thrown. cugraph.sorensen, in the absence of a specified vertex pair list, will use the edges of the graph to construct a vertex pair list and will return the sorensen coefficient for those vertex pairs. Parameters ---------- input_graph : cugraph.Graph cuGraph Graph instance, should contain the connectivity information as an edge list (edge weights are not used for this algorithm). The graph should be undirected where an undirected edge is represented by a directed edge in both direction. The adjacency list will be computed if not already present. vertex_pair : cudf.DataFrame, optional (default=None) A GPU dataframe consisting of two columns representing pairs of vertices. If provided, the Sorensen coefficient is computed for the given vertex pairs. If the vertex_pair is not provided then the current implementation computes the Sorensen coefficient for all adjacent vertices in the graph. Returns ------- df : cudf.DataFrame GPU data frame of size E (the default) or the size of the given pairs (first, second) containing the Sorensen index. The ordering is relative to the adjacency list, or that given by the specified vertex pairs. df['source'] : cudf.Series The source vertex ID (will be identical to first if specified) df['destination'] : cudf.Series The destination vertex ID (will be identical to second if specified) df['sorensen_coeff'] : cudf.Series The computed Sorensen coefficient between the source and destination vertices Examples -------- >>> gdf = cudf.read_csv(datasets_path / 'karate.csv', delimiter=' ', ... dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(gdf, source='0', destination='1') >>> df = cugraph.sorensen(G) """ if type(input_graph) is not Graph: raise TypeError("input graph must a Graph") if type(vertex_pair) == cudf.DataFrame: vertex_pair = renumber_vertex_pair(input_graph, vertex_pair) elif vertex_pair is not None: raise ValueError("vertex_pair must be a cudf dataframe") df = jaccard_wrapper.jaccard(input_graph, None, vertex_pair) df.jaccard_coeff = ((2 * df.jaccard_coeff) / (1 + df.jaccard_coeff)) df.rename({'jaccard_coeff': 'sorensen_coeff'}, axis=1, inplace=True) if input_graph.renumbered: df = input_graph.unrenumber(df, "source") df = input_graph.unrenumber(df, "destination") return df