Numpy ValueError:使用序列设置数组元素.此消息可能会在不存在序列的情况下出现? [英] Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?

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

问题描述

为什么我会收到此错误消息?ValueError:使用序列设置数组元素.谢谢

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)

推荐答案

您收到错误消息

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)

这一行试图将 kOUTith<​​/code> 元素设置为 func(TempLAke[i], Z) 返回的任何内容.查看 i=0 案例:

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.])

您正在尝试将一个 4 元素数组加载到只有一个浮点数的 kOUT[0] 中.因此,您尝试使用序列(右侧,func(TempLake[i], Z) 设置数组元素(左侧,kOUT[i])).

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

可能 func 没有做你想做的,但我不确定你真正想要它做什么(不要忘记你通常可以使用像 A*B 这样的矢量化操作而不是在 numpy 中循环.)无论如何,这应该可以解释问题.

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天全站免登陆