我有这段代码:
import numpy as np
class B():
def __init__(self, a,b):
self.a = a
self.b = b
class Criteria():
def __init__(self, method, minimum, maximum, measures=None):
self.method = method
self.minimum = minimum
self.maximum = maximum
self.measures = measures if measures is not None else None
def calcs(self):
if self.measures is not None:
for x in self.measures:
if (x.a > self.minimum and x.a < self.maximum):
x.a = 999
return self.measures
def avg(self):
if self.measures is not None:
return np.average([x.value for x in self.measures])
else:
return np.average(3)# Here should be the result where None is defined
# Now I put just an arbitrary number
class Evaluate():
def __init__(self, criteria):
self.criteria = criteria
testdata = np.array([Evaluate(
np.array([Criteria('V', 1,100,
np.array([B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)],dtype=object)
),
Criteria('AVG', 22, 220, None)])
)])
for x in testdata:
for idx, el in enumerate(x.criteria):
if el.method == 'V':
el.calcs() # this result must be passed as input to the `el.avg()`
if el.method == 'AVG':
el.avg()
我有一个类B
,它包含一些数据(a和b字段)。
我正在将这些数据加载到Criteria
类,以便通过标准并相应地进行更改。
然后,Evaluate
类将保留以上所有内容。
我正在使用measures=None
到Criteria类,因为在例如avg
函数的情况下,我可能没有测量它们的平均值,但我可能有(这是我的case)来自我之前应用平均值的之前Criteria类的测量值。
现在,我想要完成的是这个。
最初加载数据:
B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)
通过传递标准(通过运行calcs
函数),这些数据将会变为:
B(100, 0.1),
B(999, 0.3),
B(300, 0.2),
B(999, 0.1)
现在,上述数据(来自第一个标准的结果/输出,必须作为输入传递给第二个标准,并使用avg
函数计算平均值。我不知道这是否可以在avg
函数中没有任何参数。只需拥有self
。
所以,我最终的结果将是599.5。
答案 0 :(得分:1)
以下是对脚本的修改。主要是我添加了repr
。但我也将None
的{{1}}案例更改为空列表measures
:
[]
考虑制作一组import numpy as np
class B():
def __init__(self, a,b):
self.a = a
self.b = b
def __repr__(self):
return 'B(%s, %s)'%(self.a, self.b)
class Criteria():
def __init__(self, method, minimum, maximum, measures=None):
self.method = method
self.minimum = minimum
self.maximum = maximum
self.measures = measures # may be None
def __repr__(self):
# **edit** work with None
if self.measures is None:
measures = 'measures: None'
else:
measures = [' measures:[']
for m in self.measures:
measures.append(' {}'.format(m))
measures.append(' ]')
measures = '\n'+ '\n'.join(measures)
return 'C({0.method},{0.minimum},{0.maximum}, {1})'.format(self, measures)
def calcs(self):
if self.measures is not None:
for x in self.measures:
if (x.a > self.minimum and x.a < self.maximum):
x.a = 999
return self.measures
def avg(self, calcs=None):
# **edit** work with None and calcs
if calcs is None:
calcs = self.measures
if calcs is None:
return 'none'
elif len(calcs)==0:
return '[]'
else:
return np.average([x.a for x in calcs])
class Evaluate():
def __init__(self, criteria):
self.criteria = criteria
def __repr__(self):
#return 'E({})'.format(self.criteria)
astr = 'Evaluate \n'
for c in self.criteria:
astr += '{}\n'.format(c)
return astr
个对象。 Criteria
必须以某种方式知道AVG
使用的measures
。一种方法是在构造期间使用measures
参数。
b1 = np.array([B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)],dtype=object)
b2 = np.array([B(1, 0.1), B(2,.5)])
c1 = Criteria('V', 1, 100, b1)
c2 = Criteria('V', 2, 200, b2)
c3 = Criteria('AVG', 22, 220, None)
c4 = Criteria('AVG', 22, 220, c2.measures)
c5 = Criteria('AVG', 22, 222, c1.measures)
编辑更改迭代以保存上一个calcs
结果,如果AVG
度量为无,则使用该结果。 C.avg
现在采用可选参数。
last_calcs = None
for c in alist:
if c.method=='V':
last_calcs = c.calcs()
print('calcs', c.measures)
if c.method=='AVG':
if c.measures is None:
avg = c.avg(last_calcs)
else:
avg = c.avg()
print('AVG', avg)
使用:
alist = [c3,c1,c3,c2,c3,c4, c5]
这会产生:
evaluate
AVG none # c3 with nothing preceeding
calcs [B(100, 0.1) B(999, 0.3) B(300, 0.2) B(999, 0.1)]
AVG 599.5 # c3 following c1
calcs [B(1, 0.1) B(2, 0.5)]
AVG 1.5 # c3 following c2
AVG 1.5 # c4 with same measures as c2
AVG 599.5 # c5 with same measures as c1