求分布函数python的中值x值

2024-10-06 12:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本我想画的字典。我试着在x值处画一条垂直线,其中50%的y值在上面,50%在下面。在

我试图通过找到dict.值(y值)并获取匹配键。但我得到的x值太低了,这显然不是中等x值。有人知道我怎么做吗?以下是我目前所得到的:

hist1, hist2 = np.genfromtxt('./Unfold_Accessibility/social_shortest_path_durations_histogram.txt', unpack=True)

hist = np.genfromtxt('./Unfold_Accessibility/social_shortest_path_durations_histogram.txt')
dl1 = dict(hist)

median1 = dl1.keys()[dl1.values().index(np.median(dl1.values()))]

%matplotlib inline
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(hist1, hist2, '-', color='blue', label='data')
ax.axvline(median1, color='blue', linestyle='--', label='mean p=1')
plt.title('Shortest Path Duration Cumulative') 
plt.legend(bbox_to_anchor=(1.6, 1))
plt.xlabel('Time')
plt.ylabel('Probability')
plt.grid(True)

plt.show()

median_plot 编辑:好的这就是我现在所做的。不是很贵,但似乎很管用:

^{pr2}$

Tags: pathtxtnpsocialpltdicthistogramaccessibility
1条回答
网友
1楼 · 发布于 2024-10-06 12:33:58

您需要在遍历键之前对它们进行排序:

例如:

def Quantile(self, distribution, quantile):
  target = np.sum(distribution.values()) * quantile
  currentsum = 0.0
  for key in sorted(distribution.keys()):
    if currentsum < target:
      currentsum = currentsum + distribution[key]
    if currentsum >= target:
      return key

相关问题 更多 >