python setting an array_python-ValueError:使用序列设置数组元素 (python-ValueError: setting an array element wi...

2015-02-14 02:36:41

0

I generate a random sample from the two dimensional distribution, But I got this run time error. Could someone tell me how to fix this error?

This is the error message:

ValueError Traceback (most recent call last)

in ()

25 # Do the accept/reject comparison

26 if x < P(x,y):

---> 27 samples[accepted] = x,y

28 accepted += 1

29

ValueError: setting an array element with a sequence.

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()

ValueError: setting an array element with a sequence是一个常见的机器学习问题。这个错误通常出现在尝试将一个序列赋值给一个数组的元素时。根据引用、和的描述,这个问题可能与numpy数组的形状不匹配有关。 首先,需要检查代码中numpy数组的形状是否正确。如果数组的形状不正确,尝试重新调整数组的维度,以确保每个元素都具有相同的形状。 另外,还需要确认是否在使用数组的索引操作时出现了错误。在numpy中,使用索引操作时需要确保索引的维度和数组的形状是一致的。 如果仍然无法解决问题,可以尝试使用其他方法来解决该问题。例如,可以尝试使用循环来逐个赋值数组的元素,而不是直接赋值一个序列。 综上所述,当遇到ValueError: setting an array element with a sequence时,需要检查numpy数组的形状和索引操作,同时可以尝试使用其他方法来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [ValueError: setting an array element with a sequence.](https://blog.csdn.net/weixin_51197745/article/details/116591165)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [解决python-报错----ValueError: setting an array element with a sequence.](https://blog.csdn.net/qwerpoiu66/article/details/130902870)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值