Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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) 
like image 507
user1419224 Avatar asked Nov 09 '12 14:11

user1419224


People also ask

How do you fix set an array element with a sequence?

Easiest way to fix this problem is to use the data-type which support all type of data-type. Second way to fix this problem is to match the default data-type of array and assigning value.

What is the meaning of setting an array element with a sequence?

0. Python throws valueerror: setting an array element with a sequence, when you are trying to create an array with the list which is not proper multi-dimensional in shape. Another reason is related to the type of content in array. For example, you are defining a float array and inserting string values in it.

How do you convert a list to an array in Python?

To convert a list to array in Python, use the np. array() method. The np. array() is a numpy library function that takes a list as an argument and returns an array containing all the list elements.


2 Answers

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.

like image 196
DSM Avatar answered Sep 21 '22 12:09

DSM


It's a pity that both of the answers analyze the problem but didn't give a direct answer. 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 an array. Here every item in kOUT is an int, can't directly assign to another datatype. Hence you should declare the data type of kOUT when you create it. 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.

like image 38
Junning Huang Avatar answered Sep 24 '22 12:09

Junning Huang