【发布时间】:2015-02-14 10:54:53
【问题描述】:
我从二维分布中生成了一个随机样本,但是我得到了这个运行时错误。有人能告诉我如何解决这个错误吗? 这是错误信息:
ValueError Traceback(最近一次调用最后一次) 在 () 25 # 做接受/拒绝比较 26 如果 x 27 个样本[接受] = x,y 28 接受 += 1 29
ValueError: 使用序列设置数组元素。
P = lambda x, y: np.exp(-x**2-y**2-x**2*y**2)
# domain limits
xmin = -2 # the lower limit of our domain
xmax = 2 # the upper limit of our domain
# range limit (supremum) for y
ymin = -2
ymax = 2
N = 10000 # the total of samples we wish to generate
accepted = 0 # the number of accepted samples
samples = np.zeros(N)
count = 0 # the total count of proposals
# generation loop
while (accepted < N):
# pick a uniform number on [xmin, xmax) (e.g. 0...10)
x = np.random.uniform(xmin, xmax)
# pick a uniform number on [0, ymax)
y = np.random.uniform(ymin,ymax)
# Do the accept/reject comparison
if x < P(x,y):
samples[accepted] = x,y
accepted += 1
count +=1
print count, accepted
# get the histogram info
hinfo = np.histogram(samples,30)
# plot the histogram
plt.hist(samples,bins=30, label=u'Samples', alpha=0.5);
# plot our (normalized) function
xvals=np.linspace(xmin, xmax, 1000)
plt.plot(xvals, hinfo[0][0]*P(xvals), 'r', label=u'P(x)')
# turn on the legend
plt.legend()
【问题讨论】:
-
请在您的问题中包含完整的错误信息。
标签: python