开发者

store/load numpy array from binary files

I would like to store and load numpy arrays from binary files. For that purposes, I created two small functions. Each binary file should contain the dimensionality of the given matrix.

def saveArrayToFile(data, fileName):
    with open(fileName, 'w') as file:
        a = array.array('f')
        nSamples, ndim = data.shape
        a.extend([nSamples, ndim]) # write number of elements and dimensions
        a.fromstring(data.tostring())
        a.tofile(file)


def readArrayFromFile(fileName):
    _featDesc = np.fromfile(fileName, 'f')
    _ndesc = int(_featDesc[0])
    _ndim  = int(_featDesc[1])
    _featDesc = _featDesc[2:]
    _featDesc = _featDesc.reshape([_ndesc, _ndim])

    return _featDesc, _ndesc, _ndim

An example on how to use the functions is:

myarr=np.array([[7, 4],[3, 9],[1, 3]])
saveArrayToFile(myarr,'myfile.txt')
_featDesc, _ndesc, _ndim = readArrayFromFile('myfile.txt')

However, an e开发者_开发百科rror message of 'ValueError: total size of new array must be unchanged' is shown. My arrays can be of size MxN and MxM. Any suggestions are more than welcomed. I think the problem might be in the saveArrayToFile function.

Best wishes,

Javier


Use numpy.save (and numpy.load) to dump (retrieve) numpy arrays to (from) a binary file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜