Numpy ValueError:设置具有序列的数组元素。此消息可能在不存在序列的情况下出现?

时间:2021-02-04 18:03:30

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

为什么我会得到这个错误信息?ValueError:设置具有序列的数组元素。谢谢你!

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)

6 个解决方案

#1


38  

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:

这一行尝试将kOUT的第i个元素设置为func(TempLAke[i], Z)返回的内容。看i=0的情况:

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

您正在尝试将一个4元素的数组加载到kOUT[0]中,它只有一个浮点数。因此,您正在尝试设置一个数组元素(左手边,kOUT[i])和一个序列(右手边,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.

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

#2


16  

It's a pity that both of the answers figure out the problem but didn't give a conclusion to solve that. Let's see the code.

很遗憾,两个答案都解决了问题,但没有得出结论来解决这个问题。让我们来看看代码。

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):
    # store the i-th result of
    # function "func" in i-th item in kOUT
    kOUT[i] = func(TempLake[i], Z)

The error shows that you set the ith item of kOUT(dtype:int) into a array, every item in kOUT is just a int item, can't point to other datatype, you should change the statement to change the datatype of the kOUT. For example, like:

错误显示您将kOUT(dtype:int)的第ith项设置为一个数组,kOUT中的每个项都是一个int项,不能指向其他数据类型,您应该更改语句以更改kOUT的数据类型。例如,像:

Change the statement below:

改变下面的语句:

kOUT = np.zeros(N + 1)

into:

成:

kOUT = np.zeros(N + 1, dtype=object)

or:

或者:

kOUT = np.zeros((N + 1, N + 1))

All code:

所有的代码:

import numpy as np
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, dtype=object)
for i in xrange(N):
    kOUT[i] = func(TempLake[i], Z)

Hope it can help you.

希望它能对你有所帮助。

#3


3  

I believe python arrays just admit values. So convert it to list:

我认为python数组只允许值。把它转换成列表:

kOUT = np.zeros(N+1)
kOUT = kOUT.tolist()

#4


1  

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.vectorize(func)(TempLake,Z)

This works too , instead of looping , just vectorize however read below notes from the scipy documentation : https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html

它也可以工作,而不是循环,只是向量化,但是请阅读来自scipy文档的说明:https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.

矢量化函数的提供主要是为了方便,而不是为了性能。实现实质上是一个for循环。

If otypes is not specified, then a call to the function with the first argument will be used to determine the number of outputs. The results of this call will be cached if cache is True to prevent calling the function twice. However, to implement the cache, the original function must be wrapped which will slow down subsequent calls, so only do this if your function is expensive.

如果没有指定类型,那么将使用带有第一个参数的函数调用来确定输出的数量。如果缓存为真,则此调用的结果将被缓存,以防止调用函数两次。但是,要实现缓存,必须对原始函数进行包装,这将降低后续调用的速度,因此只有在函数昂贵时才这样做。

#5


0  

To put a sequence or another numpy array into a numpy array, Just change this line:

要将一个序列或另一个numpy数组放入一个numpy数组,只需更改这一行:

kOUT=np.zeros(N+1)

to:

:

kOUT=np.asarray([None]*(N+1))

Or:

或者:

kOUT=np.zeros((N+1), object)

#6


-1  

KOUT[i] is a single element of a list. But you are assigning a list to this element. your func is generating a list.

KOUT[i]是列表中的一个元素。但是您正在为这个元素分配一个列表。你的func正在生成一个列表。

#1


38  

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:

这一行尝试将kOUT的第i个元素设置为func(TempLAke[i], Z)返回的内容。看i=0的情况:

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

您正在尝试将一个4元素的数组加载到kOUT[0]中,它只有一个浮点数。因此,您正在尝试设置一个数组元素(左手边,kOUT[i])和一个序列(右手边,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.

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

#2


16  

It's a pity that both of the answers figure out the problem but didn't give a conclusion to solve that. Let's see the code.

很遗憾,两个答案都解决了问题,但没有得出结论来解决这个问题。让我们来看看代码。

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):
    # store the i-th result of
    # function "func" in i-th item in kOUT
    kOUT[i] = func(TempLake[i], Z)

The error shows that you set the ith item of kOUT(dtype:int) into a array, every item in kOUT is just a int item, can't point to other datatype, you should change the statement to change the datatype of the kOUT. For example, like:

错误显示您将kOUT(dtype:int)的第ith项设置为一个数组,kOUT中的每个项都是一个int项,不能指向其他数据类型,您应该更改语句以更改kOUT的数据类型。例如,像:

Change the statement below:

改变下面的语句:

kOUT = np.zeros(N + 1)

into:

成:

kOUT = np.zeros(N + 1, dtype=object)

or:

或者:

kOUT = np.zeros((N + 1, N + 1))

All code:

所有的代码:

import numpy as np
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, dtype=object)
for i in xrange(N):
    kOUT[i] = func(TempLake[i], Z)

Hope it can help you.

希望它能对你有所帮助。

#3


3  

I believe python arrays just admit values. So convert it to list:

我认为python数组只允许值。把它转换成列表:

kOUT = np.zeros(N+1)
kOUT = kOUT.tolist()

#4


1  

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.vectorize(func)(TempLake,Z)

This works too , instead of looping , just vectorize however read below notes from the scipy documentation : https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html

它也可以工作,而不是循环,只是向量化,但是请阅读来自scipy文档的说明:https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.

矢量化函数的提供主要是为了方便,而不是为了性能。实现实质上是一个for循环。

If otypes is not specified, then a call to the function with the first argument will be used to determine the number of outputs. The results of this call will be cached if cache is True to prevent calling the function twice. However, to implement the cache, the original function must be wrapped which will slow down subsequent calls, so only do this if your function is expensive.

如果没有指定类型,那么将使用带有第一个参数的函数调用来确定输出的数量。如果缓存为真,则此调用的结果将被缓存,以防止调用函数两次。但是,要实现缓存,必须对原始函数进行包装,这将降低后续调用的速度,因此只有在函数昂贵时才这样做。

#5


0  

To put a sequence or another numpy array into a numpy array, Just change this line:

要将一个序列或另一个numpy数组放入一个numpy数组,只需更改这一行:

kOUT=np.zeros(N+1)

to:

:

kOUT=np.asarray([None]*(N+1))

Or:

或者:

kOUT=np.zeros((N+1), object)

#6


-1  

KOUT[i] is a single element of a list. But you are assigning a list to this element. your func is generating a list.

KOUT[i]是列表中的一个元素。但是您正在为这个元素分配一个列表。你的func正在生成一个列表。