gpt4 book ai didi

python - 将类实例的输出作为另一个实例的输入

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:53 25 4
gpt4 key购买 nike

我有这个代码:

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 类将保存以上所有内容。

我在 Criteria 类中使用 measures=None ,因为在 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。但我还将 measuresNone 情况更改为空列表 []:

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 参数。

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度量为None时使用该结果。 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

关于python - 将类实例的输出作为另一个实例的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com