Numpy ValueError:设置一个带有序列的数组元素 [英] Numpy ValueError: setting an array element with a sequence
问题描述
当尝试将mysql数据转换为numpy数组(最终是列表)时,我得到了Numpy ValueError: setting an array element with a sequence
.最初,我希望有多个字段,但是我已将代码简化为仅列出一个字段(整数)以进行故障排除.
I am getting Numpy ValueError: setting an array element with a sequence
when attempting to turn mysql data into a numpy array (eventually a list). Originally I wanted to have multiple fields but I've simplified the code to only making a list with one field (integer) for troubleshooting purposes.
我是numpy的新手,所以我不确定{c1>代表什么(尽管文档说这意味着整数....好的).至于count
,这似乎是指字段数.但是,该fromiter
行上的某些内容仍然导致其命中异常.
I'm new to numpy so I'm not exactly sure what i4
represents (although docs say it means an integer.... ok). As for the count
, that seems to refer to the number of fields. However, something on that fromiter
line is still causing it to hit an exception.
import MySQLdb
import numpy
conn = MySQLdb.connect(host="localhost", user="x", passwd="x", db="x")
curs = conn.cursor()
numrows = curs.execute("select id from table")
A = numpy.fromiter(curs.fetchall(), count=numrows, dtype=('i4'))
print A
ids = A['f0']
跟踪:
A = numpy.fromiter(curs.fetchall(), count=1, dtype=('i4'))
ValueError: setting an array element with a sequence.
推荐答案
更正-要使用fetchall
中的元组列表,dtype
应生成结构化数组
correction - to work with a list of tuples from fetchall
, the dtype
should produce a structured array
查看文档,我看到fetch_all
返回一个元组列表,而不是生成器.但这不是这里的问题.两者都是可迭代的.问题出在dtype.要从元组列表中创建一维数组,fromiter
需要结构化的复合dtype.
Looking at the documentation I see that fetch_all
returns a list of tuples, not a generator. But that isn't the issue here. Both are iterables. The problem is with the dtype. To make a 1d array from a list of tuples, fromiter
requires a structured, compound, dtype.
此dtype使用1个元素的元组:
This dtype works with a 1 element tuple:
In [355]: np.fromiter([(1,)],dtype=[('f0','i4')])
Out[355]:
array([(1,)], dtype=[('f0', '<i4')])
这适用于2个字段(列)
This works with 2 fields (columns)
In [356]: np.fromiter([(1,1)],dtype=('i4,i4'))
Out[356]:
array([(1, 1)], dtype=[('f0', '<i4'), ('f1', '<i4')])
但是它们是相同的-一个普通的非结构化数组.
But these are the same - a plain, non-structured array.
np.fromiter([(1,)],dtype=('i4'))
np.fromiter([(1,)],dtype=int)
[(1,)]
的处理方式与[[1]]
相同,这是2d数组的输入,而不是fromiter
期望的1d迭代.
[(1,)]
is handled the same as [[1]]
, the input for a 2d array, not the 1d iterable that fromiter
expects.
np.array
在结构化情况下的作用与fromiter
相同:
np.array
works the same as fromiter
for the structured cases:
np.array([(1,)],dtype=[('f0','i4')])
np.array([(1,1)],dtype=('i4,i4'))
它也可以与int
(或i4
)一起使用,但是结果是2d数组:
It also works with int
(or i4
), but the result is a 2d array:
In [366]: np.array([(1,)],dtype=('i4'))
Out[366]: array([[1]])
(早期版本)
(earlier version)
我可以通过赋予fromiter
可迭代的[(1,)]
来重现您的错误消息.
I can reproduce your error message by giving fromiter
a [(1,)]
iterable.
In [288]: np.fromiter([(1,)],dtype=int)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-288-ba24373a9489> in <module>()
----> 1 np.fromiter([(1,)],dtype=int)
ValueError: setting an array element with a sequence.
fromiter
想要1d输入,例如[1,2,3]
(或等效的发电机).
fromiter
wants a 1d input, e.g. [1,2,3]
(or the generator equivalent).
自从使用sql
以来已经有一段时间了,但是我的猜测是curs.fetchall()
给出了一个可迭代的元组,而不是单个数字的迭代.
It's been a while since I worked with sql
, but my guess is that curs.fetchall()
gives a iterable of tuples, not an iterable of single numbers.
您需要显示(打印)curs.fetchall()
或list(curs.fetchall())
才能查看传递给fromiter
的内容.
You need to display (print) curs.fetchall()
or list(curs.fetchall())
to see what what is being passed to fromiter
.
为什么要使用fromiter
?您尝试过np.array(curs.fetchall())
吗?
Why are you using fromiter
? Have you tried np.array(curs.fetchall())
?
让我们尝试使用生成器表达式来更好地模拟生成元组的fetchall
:
Let's try a generator expression to better simulate a fetchall
that generates a tuple:
In [298]: np.fromiter((i for i in [(1,2,3)]),dtype=int)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-298-f8fbf106b4d1> in <module>()
----> 1 np.fromiter((i for i in [(1,2,3)]),dtype=int)
ValueError: setting an array element with a sequence.
In [299]: np.array((i for i in [(1,2,3)]),dtype=int)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-299-71dd7463b539> in <module>()
----> 1 np.array((i for i in [(1,2,3)]),dtype=int)
TypeError: int() argument must be a string or a number, not 'generator'
这有效:
In [300]: np.array(list(i for i in [(1,2,3)]),dtype=int)
Out[300]: array([[1, 2, 3]])
In [301]: list(i for i in [(1,2,3)])
Out[301]: [(1, 2, 3)]
创建numpy数组的最简单方法是使用列表-它可以是数字列表,列表列表(大小均相同)或元组列表.
The simplest way to create a numpy array is with a list - it can be a list of numbers, a list of lists (all the same size) or a list of tuples.
将MySQL结果集转换为NumPy数组的最有效方法是什么?是前面关于使用fetchall
和fromiter
的讨论.
What's the most efficient way to convert a MySQL result set to a NumPy array? is an earlier discussion of using fetchall
and fromiter
.
这篇关于Numpy ValueError:设置一个带有序列的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!