spacenet.node_metrics.clustering_coefficient#
- spacenet.node_metrics.clustering_coefficient(spatial_network, nodes=None, edge_weight_name='Distance', add_as_node_label=False, node_label_name='clustering')#
Computes the clustering coefficient for the nodes in the spatial network. The clustering coefficient of a node is a measure of how connected its neighbors are to each other, and is defined as the ratio of the number of edges between the neighbors of the node to the number of possible edges between those neighbors. The clustering coefficient can be computed using weighted edges by specifying the name of the edge attribute to use as the weight for computing the cluster coefficient.
- Parameters:
- spatial_networkNetworkX graph
The spatial network for which to compute the clustering coefficient.
- nodeslist or np.ndarray, optional
The set of nodes to compute the clustering coefficient. Default is ‘None’, computing for all nodes.
- edge_weight_namestr, optional
The name of the edge attribute to use as the weight for computing the cluster coefficient. Default is ‘Distance’.
- add_as_node_labelbool, optional
Whether to add the computed clustering coefficient values as a node attribute in the spatial network. Default is False.
- node_label_namestr, optional
The name of the node attribute to which to add the computed clustering coefficient values if add_as_node_label is True. Default is ‘clustering’.
- Returns:
- clustering_coefficientsnp.ndarray
An array of clustering coefficient values for the specified nodes in the spatial network. The order of the values corresponds to the order of the nodes in the ‘nodes’ parameter.
- nodesnp.ndarray
An array of the node ids for which the clustering coefficient values were computed. The order of the nodes corresponds to the order of the values in the ‘clustering_coefficients’ array.
Examples
You can compute the clustering coefficient of nodes in a spatial network using the clustering_coefficient function. Below is an example of how to use this function to compute the clustering coefficient for all nodes in a spatial network generated from a set of points.
import spacenet as sn # Load the spiral dataset and extract the 'x' and 'y' columns as points spiral_data = sn.datasets.load_dataset('spiral') points = spiral_data[['x', 'y']].to_numpy() # generate a spatial network G = sn.utils.spatial_network_from_points(points,max_edge_distance=50) # compute the clustering coefficient for each node in the spatial network and add it as a node label cluster_vals,node_ids=sn.node_metrics.clustering_coefficient(G,add_as_node_label=True,node_label_name='clustering') # plot the spatial network sn.utils.plot_spatial_network(G,node_label_name='clustering')