Skip to content

Instantly share code, notes, and snippets.

@jg-you
Created April 13, 2015 20:18
Show Gist options
  • Save jg-you/beae5cdf511cc70befca to your computer and use it in GitHub Desktop.
Save jg-you/beae5cdf511cc70befca to your computer and use it in GitHub Desktop.
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