如何使ndarray子类的np.save工作? [英] How can I make np.save work for an ndarray subclass?

查看:455
本文介绍了如何使ndarray子类的np.save工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将我的数组子类保存到一个npy文件中,并在以后恢复结果.

I want to be able to save my array subclass to a npy file, and recover the result later.

类似的东西:

>>> class MyArray(np.ndarray): pass
>>> data = MyArray(np.arange(10))
>>> np.save('fname', data)
>>> data2 = np.load('fname')
>>> assert isinstance(data2, MyArray)  # raises AssertionError

文档说(强调我):

该格式明确不需要:

The format explicitly does not need to:

  • [...]
  • 完全处理numpy.ndarray的任意子类.子类将是 接受写入,但只有阵列数据会被写入.一种 常规numpy.ndarray对象将在读取文件后创建. 该API可用于为特定子类构建格式,但是 超出了常规NPY格式的范围.
  • [...]
  • Fully handle arbitrary subclasses of numpy.ndarray. Subclasses will be accepted for writing, but only the array data will be written out. A regular numpy.ndarray object will be created upon reading the file. The API can be used to build a format for a particular subclass, but that is out of scope for the general NPY format.

那么有可能使上面的代码不引发AssertionError吗?

So is it possible to make the above code not raise an AssertionError?

推荐答案

我没有看到np.save处理数组子类的证据.

I don't see evidence that np.save handles array subclasses.

我试图用它保存一个np.matrix,然后又获得了一个ndarray.

I tried to save a np.matrix with it, and got back a ndarray.

我试图保存一个np.ma数组,但出现错误

I tried to save a np.ma array, and got an error

NotImplementedError: MaskedArray.tofile() not implemented yet.

保存是通过np.lib.npyio.format.write_array完成的,

Saving is done by np.lib.npyio.format.write_array, which does

_write_array_header()   # save dtype, shape etc

如果dtype是对象,则使用pickle.dump(array, fp ...)

否则,它会执行array.tofile(fp). tofile处理写入数据缓冲区.

otherwise it does array.tofile(fp). tofile handles writing the data buffer.

我认为数组的pickle.dump最终会使用np.save,但是我不记得是如何触发的.

I think pickle.dump of an array ends up using np.save, but I don't recall how that's triggered.

例如,我可以pickle一个数组,并加载它:

I can for example pickle an array, and load it:

In [657]: f=open('test','wb')
In [658]: pickle.Pickler(f).dump(x)
In [659]: f.close()
In [660]: np.load('test')
In [664]: f=open('test','rb')
In [665]: pickle.load(f)

pickle转储/装入序列适用于测试np.manp.matrixsparse.coo_matrix情况.因此,这可能就是探索您自己的子类的方向.

This pickle dump/load sequence works for test np.ma, np.matrix and sparse.coo_matrix cases. So that's probably the direction to explore for your own subclass.

搜索numpypickle时,我发现腌制numpy数组的子类时保留自定义属性.答案涉及自定义.__reduce__.__setstate__.

Searching on numpy and pickle I found Preserve custom attributes when pickling subclass of numpy array. The answer involves a custom .__reduce__ and .__setstate__.

这篇关于如何使ndarray子类的np.save工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆