python – 我的候选消除算法不起作用

python – 我的候选消除算法不起作用,第1张

概述我试图在 Python中实现“候选消除算法”,但我的代码不起作用. 我写了3个函数: >一致地检查假设和训练样例之间的一致性 > more_general用于查找更一般的参数 > more_specific用于查找更具体的参数 但我的算法没有添加或删除G和S的假设.我无法找到问题所在.你能帮助我吗? # the general hypothesisG = [ ('?', '?', '?', ' 我试图在 Python中实现“候选消除算法”,但我的代码不起作用.

我写了3个函数:

>一致地检查假设和训练样例之间的一致性
> more_general用于查找更一般的参数
> more_specific用于查找更具体的参数

但我的算法没有添加或删除G和S的假设.我无法找到问题所在.你能帮助我吗?

# the general hypothesisG = [ ('?','?','?') ]# the specific hypothesisS = [('0','0','0')]# attributes:AV = (['short','far'],['cheap','expensive'],['many','none'],['yes','no'])# training examples:D = [    {'sample': ('far','cheap','many','no' ),'positive': True },{'sample': ('short','expensive',{'sample': ('far','none','yes'),'positive': False},'positive': True }] def consistent(hypothesis,sample):   return all([hypothesis[i] == sample[i] or hypothesis[i] == '?' for i in    range(len(hypothesis))]) def more_general(a,b):    result = False    if a == '0' and b != '0':       result = True    elif a != '?' and b == '?':       result = True    return result def more_specific(a,b):    result = False    if a == '?' and b != '?':       result = True    elif a != '0' and b == '0':       result = True    return resultfor d in D:    if d['positive']:        G = [g for g in G if consistent(g,d['sample'])]        for s in S:            if not consistent(s,d['sample']):                S.remove(s)                # Adding to S all minimal generalizations of s by h:                dd = d['sample']                 if s == 0:                    h = dd[s]                else:                    h = '?'                  if consistent(h,d['sample']) and any([more_general(g,h) for g in G]):                    S.append(h)                #Removing from S any hypothesis that is more general than     another hypothesis in S                for s2 in S:                    if any([more_general(s2,s3) and not s2 == s3 for s3 in S]):                        S.remove(s2)    else:        S = [s for s in S if not consistent(s,d['sample'])]        for g in G:            if consistent(g,d['sample']):                G.remove(g)               # Add to G all minimal specializations h of g                for ai in range(len(AV)):                    if g[ai] == '?':                        h = List(g)                        h[ai] = AV[ai][1 - AV[ai].index(d['sample'][ai])]                        h = tuple(h)                        if not consistent(h,d['sample']) and any([more_specific(s,h) for s in S]):                            G.append(h)    print('Sample: {} {}\nG: {}\nS: {}\n'.format('+' if d['positive'] else '-',d['sample'],G,S))
解决方法 这是您的代码崩溃的地方:

S.remove(s)        dd = d['sample']         if s == 0:            h = dd[s]        else:            h = '?'          if consistent(h,h) for g in G]):            S.append(h)

s是一个元组,但在if条件下,当你比较零时,你将它视为标量值.在if之后,h包含一个标量,但是在下一个if中,对consistent()的调用期望它的第一个参数,h是一个元组,而不是一个标量.

总结

以上是内存溢出为你收集整理的python – 我的候选消除算法不起作用全部内容,希望文章能够帮你解决python – 我的候选消除算法不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1196088.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存