How to Solve Python ValueError: setting an array element with a sequence 

by | Programming, Python, Tips

If you try to put a sequence of more than one element in the place of an array element, you will raise the error: ValueError: setting an array element with a sequence.

To solve this error, ensure that each element in the array is of consistent length and that you do not have a sequence in place of a single element.

The error can also happen if you attempt to create a numpy array with elements of different data-type than that specified with the dtype parameter. To solve this error, you can set the dtype of the numpy array to object.

This tutorial will go through the error in detail and how to solve it with the help of code examples.


What is a ValueError?

In Python, a value is the information stored within a particular object. You will encounter a ValueError in Python when you use a built-in operation or function that receives an argument with the right type but an inappropriate value.

Example #1: Setting An Array Element With A Sequence in Numpy

Let’s look at an example where we create a numpy array using a list of values. We can select the data type of the numpy array using the dtype parameter. In this example, we will set the data type to an integer. Let’s look at the code:

import numpy as np

arr = [2, 4, 5, [10, [12, 14]]]

data_type=int

np_arr = np.array(arr, dtype=data_type)

print(np_arr)

Let’s run the code to see the result:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
      5 data_type=int
      6
  ---≻7 np_arr = np.array(arr, dtype=data_type)
      8 
      9 print(np_arr)

ValueError: setting an array element with a sequence.

The first error is a TypeError, which we throw because the int() method expects certain data types, but it received a list. The cause of the TypeError is the ValueError. The ValueError occurs because NumPy interprets [10, [12, 14]] as a list, but the data type of the numpy array to create is int. The array can only accept integers as elements.

Solution #1: Changing dtype to object

To solve this error, we can set the data type to object; the array will then support all data types, including list. Let’s look at the revised code:

import numpy as np

arr = [2, 4, 5, [10, [12, 14]]]

data_type=object

np_arr = np.array(arr, dtype=data_type)

print(np_arr)

Let’s run the code to see the result:

[2 4 5 list([10, [12, 14]])]

We have a NumPy object array, which contains references to numpy.str_ objects. Let’s look at the type of the elements in the array:

for i in np_arr:

    print(type(i))
≺class 'numpy.str_'≻
≺class 'numpy.str_'≻
≺class 'numpy.str_'≻
≺class 'numpy.str_'≻

We can still manipulate the integer elements as integers and the list element as a list

val = np_arr[0]
val_sq = val ** 2
print(val_sq)
4
lst = np_arr[3]
print(lst[0])
10

Solution #2: Fixing List Structure

Another way to resolve this is to fix the structure of the original list. If we define the list as a two-dimensional nested list, where each list has the same length, we can pass it to the np.array() method. Let’s look at the revised code:

import numpy as np

arr = [[2, 4, 5], [10, 12, 14]]

data_type=int

np_arr = np.array(arr, dtype=data_type)

print(np_arr)

Let’s run the code to see the result:

[[ 2  4  5]
 [10 12 14]]

The result is a two-dimensional numpy array, which is we can treat as a matrix. For further reading on matrices, go to the articles:

If the dimensions of the nested list are different, the array creation will fail if the data type is not object.

import numpy as np

arr = [[2, 4], [10, 12, 14]]

data_type=int

np_arr = np.array(arr, dtype=data_type)

print(np_arr)
ValueError: setting an array element with a sequence.

To fix this, we either need to ensure the elements are of consistent length or set the data type of the array to object. The resultant array will contain numpy.str_ objects that each refer to a list:

import numpy as np

arr = [[2, 4], [10, 12, 14]]

data_type=object

np_arr = np.array(arr, dtype=data_type)

print(np_arr)
[list([2, 4]) list([10, 12, 14])]

Example #2: Setting An Array Element With A Sequence in Numpy

Let’s look at an example where we try to assign a sequence to an element of an array that only accepts a specific data type.

import numpy as np

arr = ["Python", "is", "really", "fun", "to", "learn"]

data_type = str

np_arr = np.array(arr, dtype=data_type)

np_arr[1] = ["Python", "is"]

In the above code, we attempt to assign a list of strings to a single element in the numpy array, which has the data type str. Let’s run the code to see what happens:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
----≻ 1 np_arr[1] = ["Python", "is"]

ValueError: setting an array element with a sequence

The error occurs because the array expects a string value, but it receives a list with multiple strings.

Solution

To solve this error, we need to set the data type of the numpy array to object. Let’s look at the revised code.

import numpy as np

arr = ["Python", "is", "really", "fun", "to", "learn"]

data_type = object

np_arr = np.array(arr, dtype=data_type)

np_arr[1] = ["Python", "is"]

Let’s run the code to get the result:

['Python' list(['Python', 'is']) 'really' 'fun' 'to' 'learn']

The updated array now has a list object as the first element, and the rest are string objects.

If we want the numpy array to be one specific data type, we need to use an if statement. The if statement will only assign an element if the object has the same data type as the numpy array. Let’s look at the revised code:

import numpy as np

arr = ["Python", "is", "really", "fun", "to", "learn"]

data_type = str

np_arr = np.array(arr, dtype=data_type)

variable = ["Python", "is"]

if np_arr.dtype == type(variable):

    np_arr[1] = variable

else:

    print(f'Variable value does not match the type of the numpy array {data_type}')

    print('Array:  ', np_arr)

The if statement checks if the object we want to assign to the element has the same data type as the numpy array. If it does, then the assignment happens. Otherwise, we get a print statement telling us there is a mismatch in data types. Let’s run the code to see what happens:

Variable value does not match the type of the numpy array ≺class 'str'≻
Array:   ['Python' 'is' 'really' 'fun' 'to' 'learn']

Example #3: Setting An Array Element With A Sequence in Scikit-Learn

Let’s look at another common source of the ValueError. This example will attempt to create a Scikit-Learn pipeline to fit a classifier with some training data.

import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier

# Training data

X = np.array([[-1, 1], [2, -1], [1, -1], [2]])

# Labels

y = np.array([1, 2, 2, 1])

# Pipeline

clf = make_pipeline(StandardScaler(), MLPClassifier())

# Fitting

clf.fit(X, y)

Let’s run the code to see what happens:

ValueError: setting an array element with a sequence.

We raise the ValueError because the fourth element in the array is a single value, whereas the other three elements contain two values. Therefore, the array has mismatching dimensions. If you want to manipulate multi-dimensional arrays in Python, the elements must be of consistent length.

Solution

To solve this error, we must ensure all elements have the same length. Let’s look at the revised code:

import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier

# Training data

X = np.array([[-1, 1], [2, -1], [1, -1], [2, 1]])

#Labels

y = np.array([1, 2, 2, 1])

#Pipeline

clf = make_pipeline(StandardScaler(), MLPClassifier())

# Fitting

clf.fit(X, y)

The fourth element now has two values like the rest of the elements. This code will run without any errors.

Summary

Congratulations on reading to the end of this tutorial! The error ValueError: setting an array element with a sequence occurs when you attempt to set an element to a sequence that contains more than one item. The common sources of the error are

  • Attempting to create an array with a nested list of mixed dimensions
  • Creating an array with a given value but the data-type of the value is not the same as the data-type of the numpy array.

To create a multi-dimensional array, ensure your elements have consistent lengths. Otherwise, if you do not need consistent element lengths, you can set the data type of the numpy array to object.

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨