如何修复:ValueError:使用序列设置数组元素
在本文中,我们将讨论如何修复 ValueError: setting array element with a sequence using Python。
我们在使用 Numpy 库时基本上遇到的错误是 ValueError: setting array element with a sequence。当我们创建数组或处理 numpy.array 时,我们基本上都会遇到这个错误。
发生此错误是因为 numpy.array 创建具有给定值的数组但 value 的数据类型与提供给 numpy 的数据类型不同。
防止此错误所需的步骤:
- 解决此问题的最简单方法是使用支持所有数据类型的数据类型。
- 解决此问题的第二种方法是匹配数组的默认数据类型并赋值。
方法一:使用通用数据类型
示例:显示错误代码的程序:
Python
# 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)
Python
# In this program we fix problem by different data-type
import numpy
# Creating multi-dimension array
array1 = [1, 2, 4, [5, [6, 7]]]
# Object Data type is accept all data-type
Data_type = object
# Now we fix the error
np_array = numpy.array(array1, dtype=Data_type)
print(np_array)
Python
# In this program we are demonstrating how mismatch
# of data-type can cause value error
import numpy
# Creating array
array1 = ["Geeks", "For"]
# Default Data type of Array
Data_type = str
np_array = numpy.array(array1, dtype=Data_type)
# This cause error
np_array[1] = ["for", "Geeks"]
print(np_array)
Python
# In this program we fix error by mismatch
# of data-type
import numpy
# Creating array
array1 = ["Geeks", "For"]
# Default Data type of Array
Data_type = str
np_array = numpy.array(array1, dtype=Data_type)
Variable = ["for", "Geeks"]
# First we match the data-type
if np_array.dtype == type(Variable):
np_array[1] = Variable
else:
print("Variable value is not the type of numpy array")
print(np_array)
输出:
File “C:\Users\computers\Downloads\he.py”, line 13, in
np_array = numpy.array(array1,dtype=Data_type);
ValueError: setting an array element with a sequence.
如果我们为数组的元素提供支持所有数据类型的数据类型,我们可以修复此错误:
句法:
numpy.array( Array ,dtype = Common_DataType );
示例:固定代码
Python
# In this program we fix problem by different data-type
import numpy
# Creating multi-dimension array
array1 = [1, 2, 4, [5, [6, 7]]]
# Object Data type is accept all data-type
Data_type = object
# Now we fix the error
np_array = numpy.array(array1, dtype=Data_type)
print(np_array)
输出:
[1 2 4 list([5, [6, 7]])]
方法2:通过匹配value和Array的默认数据类型
示例:显示错误的程序
Python
# In this program we are demonstrating how mismatch
# of data-type can cause value error
import numpy
# Creating array
array1 = ["Geeks", "For"]
# Default Data type of Array
Data_type = str
np_array = numpy.array(array1, dtype=Data_type)
# This cause error
np_array[1] = ["for", "Geeks"]
print(np_array)
输出:
File “C:\Users\computers\Downloads\he.py”, line 15, in
np_array[1] = [“for”,”Geeks”];
ValueError: setting an array element with a sequence
在这里我们已经看到这个错误是因为我们将数组作为一个元素分配给接受字符串数据类型的数组。我们可以通过匹配值和数组的数据类型来修复这个错误,然后将其分配为数组的元素。
句法:
if np_array.dtype == type( Variable ):
expression;
示例:固定代码
Python
# In this program we fix error by mismatch
# of data-type
import numpy
# Creating array
array1 = ["Geeks", "For"]
# Default Data type of Array
Data_type = str
np_array = numpy.array(array1, dtype=Data_type)
Variable = ["for", "Geeks"]
# First we match the data-type
if np_array.dtype == type(Variable):
np_array[1] = Variable
else:
print("Variable value is not the type of numpy array")
print(np_array)
输出:
Variable value is not the type of numpy array
['Geeks' 'For']