My goal is to save a huge 2d matrix as binary in kdb q, so that my python code can import it as numpy matrix. I played with numpy.fromfile and numpy.memmap but I don't think kdb is writing it into a binary format that numpy can understand. I also tried using the save / set / 0x0 sv commands in kdb but nothing seems to be working. Is there anyway to do this at all?
For example, in kdb:
m:(3 4)#12?1.0
`:/home/davidmoss/matrix set raze m
Then in python:
import numpy as np
matrix = np.fromfile('/home/davidmoss/matrix', dtype=np.float64)
matrix = np.memmap('/home/davidmoss/matrix', dtype='float64', mode='r', shape=(3,4))
both of these don't work, what am I missing?
np.save/loadwrites the array data buffer as a binary copy, with a preface block with shape and dtyoe info.fromfileexpects just the data, loading it as a 1d array. You specify the dtype, and reshape as desired.