Created
April 13, 2015 20:18
-
-
Save jg-you/beae5cdf511cc70befca to your computer and use it in GitHub Desktop.
NetworkX: Degree of neighbors, by degree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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