[FIXED] ValueError: setting an array element with a sequence

Rate this post

[toc]

Introduction

In this article, we will look at how you can set an array element with a sequence, and then we will also learn the ways to solve the error – “ValueError: setting an array element with a sequence“.

In Python, the ValueError generally gets raised when a function gets the argument of the right type yet an improper value. e.g., when you define an integer array and insert the string values.

The ValueError: setting an array element with a sequence occurs when:

  • An array does not have a proper shape, i.e., a multidimensional array has improper dimensions at different levels.
  • The error also occurs when you work with the NumPy library, and the NumPy array is not in sequence.

Note: The number of elements in each dimension of an array is known as its shape. The number of indices required to specify an individual array element is its dimension.

If you want to learn more about the dimensions of arrays in Python, please refer to this tutorial.

Now that you know what ValueError is let’s look at the different ways to solve ValueError: setting an array element with a sequence.

Solution 1: Using Proper Array Dimensions

Consider the following example where we have a certain NumPy array with dimensions as shown below.

Example:

# Importing the NumPy array
import numpy as np
print(np.array([[1, 2, 3], [4, 5, 6, 7]], dtype = int))

Output:

Traceback (most recent call last):
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\Errors\ValueError-array_sequence.py", line 4, in <module>
    print(np.array([[1, 2, 3], [4, 5, 6, 7]], dtype=int))
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

Explanation: Here, the ValueError occurred because the array improper dimensions, i.e. it has a shape that is not permissible. In this case, [1, 2, 3] has a dimension of 3, while [4, 5, 6, 7] has dimension 4. 

Solution: To eliminate the occurrence of the above error, you have to rectify the shape of the array. As this is a 2D array having 4 elements in the second dimension. So, you must ensure that the first dimension also has 4 elements.

# Importing the NumPy array
import numpy as np

print(np.array([[1, 2, 3, 0], [4, 5, 6, 7]], dtype=int))

Output:

[[1, 2, 3, 0]  
[4, 5, 6, 7]]

Solution 2: Dealing with Pandas Library

In Python, Pandas is an open-source library that provides high performance with easy-to-use data structures and data analysis tools. You need to import the Pandas library to utilize it. Use the following code to import it.

import pandas as pd 

Now, consider the following example that leads to the occurrence of the ValueError:

Example:

# Importing the pandas module
import pandas as pd

output = pd.DataFrame(data=[[500.0]], columns=['s count'], index=['Project'])
print(output.loc['Project', 's count'])

output.loc['Project', 's count'] = [200.0]
print(output.loc['Project', 's count'])

Output:

500.0
TypeError: float() argument must be a string or a number, not 'list'

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

Traceback (most recent call last):
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\Errors\ValueError-array_sequence.py", line 7, in <module>
    output.loc['Project', 's count'] = [200.0]
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\pandas\core\indexing.py", line 723, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\pandas\core\indexing.py", line 1732, in _setitem_with_indexer
    self._setitem_single_block(indexer, value, name)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\pandas\core\indexing.py", line 1968, in _setitem_single_block
    self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\pandas\core\internals\managers.py", line 355, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\pandas\core\internals\managers.py", line 327, in apply
    applied = getattr(b, f)(**kwargs)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\pandas\core\internals\blocks.py", line 953, in setitem
    values[indexer] = value
ValueError: setting an array element with a sequence.

Explanation: The rows and columns of the table are marked by file names or named strings. The above error occurred because Python was unable to recover the user input into the input list.

Solution: You can easily retrieve the input with the help of the DataFrame() function that is used to return a list of cells in a two-dimensional table. Also, DataFrame.astype() method helps us to cast a pandas object to a specified dtype that will help us to solve the above problem.

Recommended Read: Pandas DataFrame Indexing

# Importing the pandas module
import pandas as pd

output = pd.DataFrame(data=[[500.0]], columns=['s count'], index=['Project'])
print(output.loc['Project', 's count'])

output['s count'] = output['s count'].astype(object)
output.loc['Project', 's count'] = [200.0]
print(output)

Output:

500.0
         s count
Project  [200.0]

Solution 3: Dealing with Sklearn

Sklearn is one of the most popular libraries in Python that is utilized to execute AI and ML strategies on a dataset. While working with ML models and datasets that involve multidimensional arrays can also cause a ValueError in the code. For example, it throws an error if the array is not uniform or if a few elements are not the same. Consider the following snippet:

Example:

import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

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

clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))
clf.fit(X, y)

Output:

"C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\Scripts\python.exe" "C:/Users/SHUBHAM SAYON/PycharmProjects/Finxer/Errors/ValueError-array_sequence.py"
C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\Errors\ValueError-array_sequence.py:7: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  X = np.array([[1, 1], [-2, 1], [1, -1], [1]])
TypeError: float() argument must be a string or a number, not 'list'

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

Traceback (most recent call last):
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\Errors\ValueError-array_sequence.py", line 11, in <module>
    clf.fit(X, y)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\pipeline.py", line 390, in fit
    Xt = self._fit(X, y, **fit_params_steps)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\pipeline.py", line 348, in _fit
    X, fitted_transformer = fit_transform_one_cached(
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\joblib\memory.py", line 349, in __call__
    return self.func(*args, **kwargs)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\pipeline.py", line 893, in _fit_transform_one
    res = transformer.fit_transform(X, y, **fit_params)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\base.py", line 850, in fit_transform
    return self.fit(X, y, **fit_params).transform(X)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\preprocessing\_data.py", line 806, in fit
    return self.partial_fit(X, y, sample_weight)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\preprocessing\_data.py", line 841, in partial_fit
    X = self._validate_data(
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\base.py", line 561, in _validate_data
    X = check_array(X, **check_params)
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\venv\lib\site-packages\sklearn\utils\validation.py", line 738, in check_array
    array = np.asarray(array, order=order, dtype=dtype)
ValueError: setting an array element with a sequence.

Explanation: The reason behind getting an error in this case, is once again similar to the example we discussed previously. Here, SVC() throws an error as all the elements in the array have length 2 except the last element that has length 1. Hence, to solve this error you have to ensure that all arrays have equal lengths as shown below.

Example:

# Importing the numpy and sklearn libraries
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

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

clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))
clf.fit(X, y)
print(X)
print(y)

Output:

[[ 1  1]
 [-2  1]
 [ 1 -1]
 [ 1  2]]
[1 2 2 1]

Bonus Read: ValueError: could not convert string to float: ‘Python’

Another situation which results in a similar kind of ValueError is when you feed in values that re of different type within the same Numpy array. This is not permissible and results in an error.

Example:

import numpy as np

print(np.array([1.4, 1.6, 2.4, "Python"], dtype=float))

Output:

Traceback (most recent call last):
  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\Errors\ValueError-array_sequence.py", line 2, in <module>
    print(np.array([1.4, 1.6, 2.4, "Python"], dtype = float))
ValueError: could not convert string to float: 'Python'

Solution: To solve this error, you have to set the data type (dtype) as an object instead of setting it as a particular data type like float, string, and int. This way, you will be able to access the array with different data types values as an object has an unrestricted data type.

# Importing the NumPy array
import numpy as np
print(np.array([1.4, 1.6, 2.4, "Python"], dtype = float))

Output:

[[1.4, 1.6, 2.4, "Python"]

Conclusion

In this tutorial, we learned how to solve ValueError: setting an array element with a sequence. I hope this discussion helped you to solve your problem. Please stay tuned and subscribe for more interesting solutions and discussions in the future. Happy learning!

Post Credits: Rashi Agarwal and Shubham Sayon


Learn Pandas the Fun Way by Solving Code Puzzles

If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).

Coffee Break Pandas Book

It contains 74 hand-crafted Pandas puzzles including explanations. By solving each puzzle, you’ll get a score representing your skill level in Pandas. Can you become a Pandas Grandmaster?

Coffee Break Pandas offers a fun-based approach to data science mastery—and a truly gamified learning experience.