我必须保存并加载一个cython类实例.我的cython类是以及几种方法:
import numpy as np
cimport numpy as np
cimport cython
cdef class Perceptron_avg_my:
cdef int wlen,freePos
cdef np.ndarray w,wtot,wac,wtotc #np.ndarray[np.int32_t]
cdef np.ndarray wmean #np.ndarray[np.float32_t]
cdef public dict fpos
def __cinit__(self,np.int64_t wlen=4*10**7):
self.fpos= dict()
self.freePos=1
self.wlen=wlen
self.w=np.zeros(wlen,np.int32)
self.wtot=np.zeros(wlen,np.int32)
self.wac=np.zeros(wlen,np.int32)
self.wtotc=np.zeros(wlen,np.int32)
self.wmean=np.zeros(wlen,np.float32)
cpdef evaluate_noavg(self,list f):
cdef np.ndarray[np.int32_t] w = self.w
cdef dict fpos = self.fpos
cdef bytes ff
cdef int i
cdef long int score=0
for ff in f:
i=fpos.get(ff,0)
if i != 0:
score += w[i]
return score
Run Code Online (Sandbox Code Playgroud)
我在考虑使用cPickle模块.我明白我必须实现一个__reduce __(自我)方法但是我有一些问题需要找到一个例子并且很好地理解文档 …
我正在尝试创建一个Pandas数据结构的子类,在我的代码中替换一个dict带有a的子类的子类Series,我不明白为什么这个示例代码不起作用
from pandas import Series
class Support(Series):
def supportMethod1(self):
print 'I am support method 1'
def supportMethod2(self):
print 'I am support method 2'
class Compute(object):
supp=None
def test(self):
self.supp()
class Config(object):
supp=None
@classmethod
def initializeConfig(cls):
cls.supp=Support()
@classmethod
def setConfig1(cls):
Compute.supp=cls.supp.supportMethod1
@classmethod
def setConfig2(cls):
Compute.supp=cls.supp.supportMethod2
Config.initializeConfig()
Config.setConfig1()
c1=Compute()
c1.test()
Config.setConfig2()
c1.test()
Run Code Online (Sandbox Code Playgroud)
可能它不是更改某些对象配置的最佳方法,无论如何我发现这在我的代码中很有用,最重要的是我想了解为什么用dict而不是系列它按预期工作.
非常感谢!