微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

给定系数列表计算互补 CDF

如何解决给定系数列表计算互补 CDF

我有一个图表中节点的聚类系数列表,这是我从 NetworkX 获得的:

coefficients = nx.clustering(G)

现在我想绘制这些系数的互补 CDF,以便在 X 轴上我有系数值 x,在 Y 轴上是聚类系数大于或等于 x,即 P(X >= x)

如何在 Python 中执行此操作?我应该使用 scipy.stats.rv_discrete.cdf 吗? https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_discrete.cdf.html

解决方法

使用 scipy.stats.rv_discrete.cdf 似乎有点矫枉过正。只需对系数列表进行排序并将它们与范围 [1,0] 作图,就可以得到所需的互补 CDF。

这是一些示例代码,显示了不同随机图的互补 CDF:

import networkx as nx
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import PercentFormatter

for edges in range(200,2001,200):
    G = nx.gnm_random_graph(100,edges)
    coefficients = nx.clustering(G)
    x = np.concatenate([[0],np.sort(list(coefficients.values())),[1]])
    plt.plot(x,np.linspace(1,len(x)),label=f'100 nodes,{edges} edges')
plt.xlabel('Clustering coefficient')
plt.ylabel('P(X ≥ x)')
plt.margins(x=0.02)
plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
plt.legend(title='Random graphs')
plt.tight_layout()
plt.show()

example plot

使用 np.linspace(0,1,len(x)) 你会得到 P(X ≤ x)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。