一个数组如何包装?

2024-06-26 01:33:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个扩展numpy数组基类型的类

class LemmaMatrix(numpy.ndarray):
    @classmethod
    def init_from_corpus(cls, ...): cls(numpy.empty(...))

但显然,它不允许多维数组类型。有办法吗?提前谢谢!在

^{pr2}$

Tags: fromnumpy类型initdefcorpus数组class
1条回答
网友
1楼 · 发布于 2024-06-26 01:33:59
import numpy as np
class LemmaMatrix(np.ndarray):
    def __new__(subtype,data,dtype=None):
        subarr=np.empty(data,dtype=dtype)
        return subarr

lm=LemmaMatrix([3,3])
print(lm)
# [[  3.15913337e-260   4.94951870e+173   4.88364603e-309]
#  [  1.63321355e-301   4.80218258e-309   2.05227026e-287]
#  [  2.10277051e-309   2.07088188e+289   7.29366696e-304]]

您可能还需要阅读this guide,以了解有关如何子类ndarray的更多信息。在

相关问题 更多 >