开发者

How does one wrap numpy array types?

开发者 https://www.devze.com 2022-12-30 09:22 出处:网络
I\'d like to make a class extending the numpy array base type, class LemmaMatrix(numpy.ndarray): @classmethod

I'd like to make a class extending the numpy array base type,

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

But apparently, it will not allow multi-dimensional array types. Is there a way around this? Thanks in advance!

ndarray(empty([3, 3]))
TypeError: only length-1 array开发者_JS百科s can be converted to Python scalars


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]]

You may also want read this guide for more information on how to subclass ndarray.

0

精彩评论

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

关注公众号