Numpy数组到SQL表 [英] Numpy Array to SQL Table
问题描述
我正在尝试将多个腌制的Numpy数组保存到SQL数据库中. Numpy数组是3D且格式为(Name (text), Data (floats), Date (int)
.
I am trying to save a number of pickled Numpy arrays into an SQL database. The Numpy arrays are 3D and of the form (Name (text), Data (floats), Date (int)
.
我目前正在按照以下步骤进行操作(对于包含数据的数组arr
和names
和dates
作为包含与arr
相对应的实际名称和日期的引用)
I am currently doing it as following (for array arr
containing the data and names
and dates
as references containing actual names and dates respective to arr
)
name_size, ~, date_size = arr.shape
for i in range(name_size):
for j in range(date_size):
insert_into_db(name[i], date[j], arr[i,:,j)
但是,这非常慢.我想知道是否仅通过将对象arr
视为一个整体就没有更有效的方法了.
However this is very slow. I am wondering if there is not a far more efficient way by just considering the object arr
as a whole.
例如,首先将names
和dates
中的引用插入数据库中,然后以某种方式直接一次直接复制arr
中的值(在此,它们相对于Date
值.
For example insert the references in names
and dates
into the database first and then somehow just copy the values in arr
straight in all at once (where they are ordered and flattened correctly in reference to the Name
and Date
values we have just inserted.
推荐答案
如果数据库无法保存本机numpy数组,则可以使用dumps
或tostring
方法.
If your database cannot hold native numpy arrays, you can use the dumps
or tostring
methods.
dumps
将数据腌制到Python 3.x中的bytes
对象和Python 2.x中的str
对象中,然后可以将它们作为字符串或原始字节序列存储在数据库中.问题在于,pickle格式可以在python或numpy版本之间改变,因此,不同版本的numpy或python不一定能够读取它(尽管numpy开发人员试图使pickle阅读器尽可能地向后兼容):
dumps
pickles the data to a bytes
object in Python 3.x and a str
object in Python 2.x, which can then be stored in the database as a string or raw byte sequence. The catch is that the pickle format can change between python or numpy versions, so a different version of numpy or python won't necessarily be able to read it (although numpy developers try to keep the pickle reader as backwards-compatible as possible):
testarr = np.arange(20)
data = testarr.dumps()
哪一个给你(在python 3.x中,它在python 2.x中是不同的):
Which gives you (in python 3.x, it is different in python 2.x):
b'\x80\x02cnumpy.core.multiarray\n_reconstruct\nq\x00cnumpy\nndarray\nq\x01K\x00\x85q\x02c_codecs\nencode\nq\x03X\x01\x00\x00\x00bq\x04X\x06\x00\x00\x00latin1q\x05\x86q\x06Rq\x07\x87q\x08Rq\t(K\x01K\x14\x85q\ncnumpy\ndtype\nq\x0bX\x02\x00\x00\x00i8q\x0cK\x00K\x01\x87q\rRq\x0e(K\x03X\x01\x00\x00\x00<q\x0fNNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\x10b\x89h\x03X\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\r\x00\x00\x00\x00\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00q\x11h\x05\x86q\x12Rq\x13tq\x14b.'
tostring
的工作原理类似,它将数组转换为字符串格式.它的优点是在python和numpy版本之间应该相同,但缺点是它不存储维度,因此您需要保留维度(以及数组是C
还是Fortran
顺序)在数据库中正确地重建数组(除非它总是相同的):
tostring
works similarly in that it converts the array to a string format. It has the advantage that it should be the same across python and numpy versions, but has the disadvantage that it doesn't store the dimensions, so you would need to keep the dimensions (and whether the array is C
or Fortran
order) in the database to properly reconstruct the array (unless it is always the same):
testarr = np.arange(20)
data = testarr.tostring()
哪有(在Python 2.x和3.x中这是相同的,除了在Python 3.x中它将是bytes
类型,在python 2.x中它将是str
类型):
Which gives you (this will be the same in Python 2.x and 3.x, except that in Python 3.x it will be a bytes
type and in python 2.x it will be a str
type):
b'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\r\x00\x00\x00\x00\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00'
这篇关于Numpy数组到SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!