Numpy is a comprehensive python library that allows you to easily work with mathematical data. It provides tons of mathematical functions, random number generators, linear algebra routines, and more. While using Numpy, you may sometimes get an error saying ‘ValueError:setting an array element with a sequence’. You may see this error while Numpy library in python. This error occurs commonly when numpy tries to create an array and the data type of array is different from that of the value assigned to its items. This is especially true if you try to assign an array to an array item whose data type is int, str or other non-array type. In this article, we will learn how to fix ValueError:setting an array element with sequence
How to Fix ValueError: setting an array element with sequence
Here is a simple example of code that will give you ValueError.
# In this program we are demonstrating how different # Data-type can cause value error import numpy # Creating multi-dimension array array1 = [1, 2, 4, [5, [6, 7]]] # Data type of array element Data_type = int # This cause Value error np_array = numpy.array(array1, dtype=Data_type) print(np_array)# In this program we are demonstrating how different # Data-type can cause value error import numpy # Creating multi-dimension array array1 = [1, 2, 4, [5, [6, 7]]] # Data type of array element Data_type = int # This causes Value error np_array = numpy.array(array1, dtype=Data_type)
In the above example, we have created a multi dimensional array and then set the data type of array to int. This gives error since some of the array elements are arrays and not int. There are a couple of ways to fix this problem. We will learn them one by one.
1. Use Common Data type
In this method, we use a data type that accepts all kinds of data. Here is the syntax for this command.
numpy.array( Array ,dtype = Common_DataType )
In the above example, we got an error since we assigned an int data type an array whose elements were also arrays. So instead, we can set its data type to object which accommodates multiple data types.
# Object Data type is accept all data-type Data_type = object # Now we fix the error np_array = numpy.array(array1, dtype=Data_type)
2. Check Data type Before Assignment
In this approach, we check the data type of array before assigning value to its element.
Here is another example that gives you ValueError.
# In this program we are demonstrating how mismatch # of data-type can cause value error import numpy # Creating array array1 = ["Hello", "World"] # Default Data type of Array Data_type = str np_array = numpy.array(array1, dtype=Data_type) # This cause error np_array = ["for", "you"] print(np_array)
In this code, we create a numpy array whose each element is a string, and we also set its data type to string. But when we assign an array of strings as its second element, we get an error since Numpy expects it to be a string and not an array of strings.
So we check the data type of array before assigning values to it.
Variable = ["for", "you"] if np_array.dtype == type(Variable): np_array = Variable else: print("Variable value is not the type of numpy array")
In this case, we first define an array of strings to be assigned to our original array. Then we check if the type of variable is same as that of Numpy array. If they are the same then assignment occurs, else you get a message. Since our variable is an array of strings and our array expects each element to be a string, the assignment does not happen in this case, but there is no error either.
In this article, we have learnt a couple of easy ways to avoid ValueError in our python programs that use Numpy. They help continue program execution without breaking it.
How to Fix ‘ASCII codec can’t encode’ in Python
How to Print Curly Braces in String in Python
How to Set Timeout on Function Call in Python
How to Set Default Value for Datetime Column in MySQL