3

我的代码在给定范围 [0,1) 的情况下生成从 -10 到 10 的某个值该代码采用从 -10 到 10 的值,并根据其概率将其附加到列表中。例如,-10 将被放入列表 0 次,因为它对应于值 0,而 10 将被放入 100 次(作为标准化),因为它对应于范围中的 1。

这是代码:

#!/usr/bin/env python

import math
import numpy as np
import matplotlib.pyplot as plt

pos = []
ceilingValue = 0.82
pValues = np.linspace(0.00, ceilingValue, num=100*ceilingValue)

for i in xrange(int(100*ceilingValue)):
    p = pValues[i]
    y = -11.63*math.log(-2.36279*(p - 1))
    for j in xrange(i):
        pos.append(y)

avg = np.average(pos)    
std = np.std(pos)    

hist, bins = np.histogram(pos,bins = 100)
width = 0.7*(bins[1]-bins[0])
center = (bins[:-1]+bins[1:])/2
plt.bar(center, hist, align = 'center', width = width)
plt.show()  

问题是直方图会生成准确的图,但某些值会破坏趋势。例如,-5.88 对应于频率计数中的大约 30 个条目,大约为 70。我认为 python 看到这两个值并将它们简单地放在一起,但我不知道如何修复它。但如果只是直方图做错了,那没关系,我真的不需要它。我只需要列表,如果它首先是正确的。

4

2 回答 2

3

I think the underlying issue is that your bin size is uniform, whereas the differences between the unique values in pos scale exponentially. Because of that you'll always end up either with weird 'spikes' where two nearby unique values fall within the same bin, or lots of empty bins (especially if you just increase the bin count to get rid of the 'spikes').

You could try setting your bins according to the actual unique values in pos, so that their widths are non-uniform:

 # the " + [10,]" forces the rightmost bin edge to == 10
 uvals = np.unique(pos+[10,])
 hist, bins = np.histogram(pos,bins=uvals)
 plt.bar(bins[:-1],hist,width=np.diff(bins))

enter image description here

于 2013-07-19T19:29:32.673 回答
2

我相信你很好。我使用bins = 200而不是重新运行了您的代码,bins = 100并且尖峰消失了。我认为你的价值观被困在垃圾箱之间的边界上。

于 2013-07-19T19:09:24.840 回答