numpy的ValueError错误:在设置数组元素与序列。可能会出现没有一个序列的存在此消息? [英] Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?

查看:2810
本文介绍了numpy的ValueError错误:在设置数组元素与序列。可能会出现没有一个序列的存在此消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我得到这个错误信息? ValueError错误:在设置数组元素与序列。谢谢

  Z = np.array([1.0,1.0,1.0,1.0])高清FUNC(TempLake,Z):
    A = TempLake
    B =ž
    返回A * BnLayers一起= Z.size
N = 3
TempLake = np.zeros((N + 1,nLayers一起))KOUT = np.zeros(N + 1)
对于我的xrange(N):
    KOUT [I] = FUNC(TempLake [Ⅰ],Z)


解决方案

您收到的错误消息

  ValueError错误:在设置数组元素与序列。

因为你想设定一个序列的数组元素。我并不想成为可爱的,还有 - 错误消息要告诉你问题是什么。不要把它想成一个神秘的错误,它只是一个短语。什么线是给这个问题?

  KOUT [I] = FUNC(TempLake [I],Z)

此行​​试图为 KOUT 到任何 FUNC的第i 元素(TempLAke [I],Z)的回报。综观 I = 0 情况:

 在[39]:KOUT [0]
出[39]:0.0在[40]:FUNC(TempLake [0],Z)
出[40]:阵列([0,0,0,0])

您正试图加载4个元素的数组到 KOUT [0] 其中只有一个float。因此,你要设置一个数组元素(左侧, KOUT [I] )用序列(右手边, FUNC(TempLake [I],Z))。

也许 FUNC 不是做你想要什么,但我不知道你真正想要它做的事(不要忘了你通常可以使用矢量像A * B而不是numpy的循环操作。)这应该解释的问题,反正。

Why do I get this error message? ValueError: setting an array element with a sequence. Thank you

Z=np.array([1.0,1.0,1.0,1.0])  

def func(TempLake,Z):
    A=TempLake
    B=Z
    return A*B

Nlayers=Z.size
N=3
TempLake=np.zeros((N+1,Nlayers))

kOUT=np.zeros(N+1)
for i in xrange(N):
    kOUT[i]=func(TempLake[i],Z)

解决方案

You're getting the error message

ValueError: setting an array element with a sequence.

because you're trying to set an array element with a sequence. I'm not trying to be cute, there -- the error message is trying to tell you exactly what the problem is. Don't think of it as a cryptic error, it's simply a phrase. What line is giving the problem?

kOUT[i]=func(TempLake[i],Z)

This line tries to set the ith element of kOUT to whatever func(TempLAke[i], Z) returns. Looking at the i=0 case:

In [39]: kOUT[0]
Out[39]: 0.0

In [40]: func(TempLake[0], Z)
Out[40]: array([ 0.,  0.,  0.,  0.])

You're trying to load a 4-element array into kOUT[0] which only has a float. Hence, you're trying to set an array element (the left hand side, kOUT[i]) with a sequence (the right hand side, func(TempLake[i], Z)).

Probably func isn't doing what you want, but I'm not sure what you really wanted it to do (and don't forget you can usually use vectorized operations like A*B rather than looping in numpy.) That should explain the problem, anyway.

这篇关于numpy的ValueError错误:在设置数组元素与序列。可能会出现没有一个序列的存在此消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆