使用 python networkx 对数分箱分布
Log binning distribution using python networkx
我正在尝试使用以下示例绘制对数分级网络度分布:
Example
所以我尝试了以下代码:
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)
为什么我在最后一行代码处出错?
我收到以下错误:
TypeError: '<' not supported between instances of 'dict_keys' and 'float'
尝试将您的键和值转换为 np.array
:
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])
也可能你假装:
import matplotlib as mpl
import matplotlib.pyplot as plt
完整代码:
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()
我正在尝试使用以下示例绘制对数分级网络度分布: Example
所以我尝试了以下代码:
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)
为什么我在最后一行代码处出错? 我收到以下错误:
TypeError: '<' not supported between instances of 'dict_keys' and 'float'
尝试将您的键和值转换为 np.array
:
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])
也可能你假装:
import matplotlib as mpl
import matplotlib.pyplot as plt
完整代码:
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()