Warm tip: This article is reproduced from serverfault.com, please click

Log binning distribution using python networkx

发布于 2020-11-26 17:41:46

Im trying to plot a log-binned network degree distribution using the followinf example: Example

So i tried the following code:

from collections import Counter
import math
import networkx as nx
import matplotlib as plt
import numpy as np


def drop_zeros(a_list):
    return [i for i in a_list if i>0]


def log_binning(counter_dict, bin_count=35):
    max_x = math.log10(max(counter_dict.keys()))
    max_y = math.log10(max(counter_dict.values()))
    max_base = max([max_x, max_y])
    min_x = math.log10(min(drop_zeros(counter_dict.keys())))
    bins = np.logspace(min_x, max_base, num=bin_count)
    bin_means_y = (np.histogram(counter_dict.keys(), bins, weights=counter_dict.values())[0] / np.histogram(counter_dict.keys(), bins)[0])
    bin_means_x = (np.histogram(counter_dict.keys(), bins, weights=counter_dict.keys())[0] / np.histogram(counter_dict.keys(), bins)[0])
    return bin_means_x, bin_means_y


mygraph = nx.read_edgelist('graph.txt', create_using= nx.Graph(), nodetype=int)
ba_c = nx.degree_centrality(mygraph)
# To convert normalized degrees to raw degrees
ba_c2 = dict(Counter(ba_c.values()))
ba_x, ba_y = log_binning(ba_c2, 50)

Why im getting error at the last line code? Im getting the following error:

TypeError: '<' not supported between instances of 'dict_keys' and 'float'
Questioner
Lee Yaan
Viewed
0
willcrack 2020-11-29 02:08:36

Try converting your keys and values to np.array:

data_x = np.array(list(counter_dict.keys()))
data_y = np.array(list(counter_dict.values()))

then:

bin_means_x = (np.histogram(data_x, bins, weights=data_x)[0] /np.histogram(data_x, bins)[0])
bin_means_y = (np.histogram(data_y, bins, weights=data_y)[0] /np.histogram(data_y, bins)[0])

Also maybe you pretended to:

import matplotlib as mpl
import matplotlib.pyplot as plt

Full code:

from collections import Counter
import math
import networkx as nx
#import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np


def drop_zeros(a_list):
    return [i for i in a_list if i>0]


def log_binning(counter_dict, bin_count=35):
    
    max_x = math.log10(max(counter_dict.keys()))
    max_y = math.log10(max(counter_dict.values()))
    max_base = max([max_x, max_y])
    
    min_x = math.log10(min(drop_zeros(counter_dict.keys())))
    
    bins = np.logspace(min_x, max_base, num=bin_count)
    
    data_x = np.array(list(counter_dict.keys()))
    data_y = np.array(list(counter_dict.values()))
    
    
    bin_means_x = (np.histogram(data_x, bins, weights=data_x)[0] /np.histogram(data_x, bins)[0])
    bin_means_y = (np.histogram(data_y, bins, weights=data_y)[0] /np.histogram(data_y, bins)[0])
    return bin_means_x, bin_means_y

mygraph = nx.read_edgelist('graph.txt', create_using= nx.Graph(), nodetype=int)

ba_c = nx.degree_centrality(mygraph)
# To convert normalized degrees to raw degrees
ba_c2 = dict(Counter(ba_c.values()))

ba_x,ba_y = log_binning(ba_c2,50)

plt.xscale("log")
plt.yscale("log")

plt.scatter(ba_x,ba_y,c='r',marker='s',s=50)
plt.scatter(ba_c2.keys(),ba_c2.values(),c='b',marker='x')

plt.xlim((1e-4,1e-1))
plt.ylim((.9,1e4))

plt.xlabel('Connections (normalized)')
plt.ylabel('Frequency')

plt.show()