第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

一種模式,其中一個(gè)類具有許多相似的方法(相同的類型簽名,相似的語義)

一種模式,其中一個(gè)類具有許多相似的方法(相同的類型簽名,相似的語義)

拉風(fēng)的咖菲貓 2021-03-12 15:10:52
很難抽象地描述這一點(diǎn),所以讓我舉一個(gè)(簡化和摘錄的)示例:class ClassificationResults(object):  #####################################################################################################################  # These methods all represent aggregate metrics. They all follow the same interface: they return a tuple  # consisting of the numerator and denominator of a fraction, and a format string that describes the result in terms  # of that numerator, denominator, and the fraction itself.  #####################################################################################################################  metrics  = ['recall', 'precision', 'fmeasure', 'f2measure', 'accuracy']  # ...  def recall(self):    tpos, pos = 0, 0    for prediction in self.predictions:      if prediction.predicted_label == 1:        pos += 1        if prediction.true_label == 1:          tpos += 1    return tpos, pos, "{1} instances labelled positive. {0} of them correct (recall={2:.2})"  def precision(self):    tpos, true = 0, 0    for prediction in self.predictions:      if prediction.true_label == 1:        true += 1        if prediction.predicted_label == 1:          tpos += 1    return tpos, true, "{1} positive instances. We labelled {0} correctly (precision={2:.2})"  # ...  def printResults(self):    for methodname in self.metrics:      (num, denom, msg) = getattr(self, methodname)()      dec = num/float(denom)      print msg.format(num, denom, dec)有沒有更好的方法來表明這些方法都屬于同一個(gè)“族”,并允許它們?cè)谘h(huán)中被調(diào)用而無需每次都對(duì)其進(jìn)行命名?我過去做過的另一種方法是用通用前綴命名方法,例如  def metric_precision(self):    tpos, true = 0, 0    for prediction in self.predictions:      if prediction.true_label == 1:        true += 1        if prediction.predicted_label == 1:          tpos += 1    return tpos, true, "{1} positive instances. We labelled {0} correctly (precision={2:.2})"我還可以將每個(gè)方法都轉(zhuǎn)換為一個(gè)公共超類的實(shí)例,但是這感覺有點(diǎn)過頭了。
查看完整描述

3 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

  • 您可以使用類裝飾器來生成度量標(biāo)準(zhǔn)方法的列表。這樣做的好處是,您可以在類定義時(shí)生成度量標(biāo)準(zhǔn)方法的列表,而不是每次 printResults調(diào)用時(shí)都重新生成該列表。

    另一個(gè)優(yōu)點(diǎn)是您不必手動(dòng)維護(hù)ClassificationResults.metrics列表。您無需在兩個(gè)位置上拼寫方法的名稱,因此它是DRY-er,而且,如果您添加了另一個(gè)指標(biāo),則不必記住也要更新ClassificationResults.metrics。您只需要給它一個(gè)以開頭的名稱即可metrics_。

  • 由于每種度量方法都返回一個(gè)相似的對(duì)象,因此您可以考慮將該概念形式化為類(例如Metric,下面的)。這樣做的一個(gè)好處是您可以定義一種__repr__方法來處理結(jié)果的打印方式。注意printResults (下面)變得多么簡單。

def register_metrics(cls):

    for methodname in dir(cls):

        if methodname.startswith('metric_'):

            method = getattr(cls, methodname)

            cls.metrics.append(method)

    return cls



class Metric(object):

    def __init__(self, pos, total):

        self.pos = pos

        self.total = total


    def __repr__(self):

        msg = "{p} instances labelled positive. {t} of them correct (recall={d:.2g})"

        dec = self.pos / float(self.total)

        return msg.format(p=self.total, t=self.pos, d=dec)



@register_metrics

class ClassificationResults(object):

    metrics = []


    def metric_recall(self):

        tpos, pos = 1, 2

        return Metric(tpos, pos)


    def metric_precision(self):

        tpos, true = 3, 4

        return Metric(tpos, true)


    def printResults(self):

        for method in self.metrics:

            print(method(self))


foo = ClassificationResults()

foo.printResults()


查看完整回答
反對(duì) 回復(fù) 2021-03-29
  • 3 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)