我有以下代码:
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
函数的情况下,我可能没有度量来计算平均值,但是我可能是(在我的情况下)我正在应用上一个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。
这是您的脚本的修改。主要是我加了repr
。但是我也将None
情况更改为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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句