简体   繁体   English

如何在 KDB 中以二进制格式保存列表/矩阵?

[英]How can i save a list/matrix in binary format in KDB?

I am trying to save a matrix to file in binary format in KDB as per below:我正在尝试save 矩阵以二进制格式save到 KDB 中,如下所示:

matrix: (til 10)*/:til 10;
save matrix;

However, I get the error 'type .但是,我收到错误'type

I guess save only works with tables?我猜save只适用于表格? In which case does anyone know of a workaround?在这种情况下,有人知道解决方法吗?

Finally, I would like to read the matrix from the binary file into Python with NumPy , which I presume is just:最后,我想用NumPy将二进制文件中的矩阵读入 Python 中,我认为它只是:

import numpy as np
matrix = np.fromfile('C:/q/w32/matrix', dtype='f')

Is that right?那正确吗?

Note: I'm aware of KDB-Python libraries , but have been unable to install them thus far.注意:我知道KDB-Python 库,但到目前为止还无法安装它们。

save does work, you just have to reference it by name. save确实有效,您只需按名称引用它。

save`matrix

You can also save using您也可以使用保存

`:matrix set matrix;
`:matrix 1: matrix;

But I don't think you'll be able to read this into python directly using numpy as it is stored in kdb format.但是我认为您无法直接使用 numpy 将其读入 python,因为它以 kdb 格式存储。 It could be read into python using one of the python-kdb interfaces (eg PyQ) or by storing it in a common format such as csv.可以使用 python-kdb 接口之一(例如 PyQ)或通过将其存储在通用格式(例如 csv)中将其读入 python。

Another option is to save in KDB+ IPC format and then read it into Python with qPython as a Pandas DataFrame.另一种选择是保存为 KDB+ IPC 格式,然后使用qPython作为 Pandas DataFrame 将其读入 Python。 On the KDB+ side you can save it with在 KDB+ 方面,您可以使用

matrix:(til 10)*/:til 10;
`:matrix.ipc 1: -8!matrix;

On the Python side you do在 Python 方面你做

from pandas import DataFrame
from qpython.qreader import QReader

with open('matrix.ipc',"rb") as f:
    matrix = DataFrame(QReader(f).read().data)

print(matrix)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM