Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NetworkX: Degree of neighbors, by degree
import networkx as nx
# Mean function (no statistics module in python2)
mean = lambda l: sum(l)/len(l)
# Declare some graph
G = nx.erdos_renyi_graph(200,0.1)
# Prepare raw results container
result = dict()
for v in G: # For each node
# Get mean degree of neighbors
mean_deg_neighbors = mean([G.degree(x) for x in G.neighbors(v)])
# Add to raw results container
try:
result[G.degree(v)].append(mean_deg_neighbors)
except:
result[G.degree(v)] = [mean_deg_neighbors]
# Average
for i in result:
print str(i) + " " + str(mean(result[i]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.