Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

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) 

回答1:

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.



回答2:

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:

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:

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

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


回答4:

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

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

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:

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

kOUT=np.zeros(N+1) 

to:

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

Or:

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


回答6:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!