Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的候选消除算法不起作用_Python_Algorithm - Fatal编程技术网

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

Python 我的候选消除算法不起作用,python,algorithm,Python,Algorithm,我试图用Python实现一个“候选消除算法”,但我的代码不起作用 我写了3个函数: 一致性用于检查假设和训练示例之间的一致性 more\u general查找更一般的参数 more_specific查找更具体的参数 但是我的算法没有添加或删除G和S中的假设。我找不到问题出在哪里。你能帮我吗 # the general hypothesis G = [ ('?', '?', '?', '?') ] # the specific hypothesis S = [('0', '0', '0'

我试图用Python实现一个“候选消除算法”,但我的代码不起作用

我写了3个函数:

  • 一致性
    用于检查假设和训练示例之间的一致性
  • more\u general
    查找更一般的参数
  • more_specific
    查找更具体的参数
但是我的算法没有添加或删除
G
S
中的假设。我找不到问题出在哪里。你能帮我吗

# the general hypothesis

G = [ ('?', '?', '?', '?') ]

# the specific hypothesis

S = [('0', '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', 'many', 'no' ), 'positive': True },
    {'sample': ('far',   'expensive', 'none', 'yes'), 'positive': False},
    {'sample': ('short', 'cheap',     'none', 'yes'), 'positive': False},
    {'sample': ('short', 'cheap',     'many', 'yes'), '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 result


for 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, d['sample']) and any([more_general(g, h) for g in G]):
            S.append(h)

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

我明白了。但是我怎么能解决这个问题呢?我应该把h参数放在pranthesis里面吗?像h=(dd[s])?我假设要传递给
consistent()
more\u general()
h
需要是四个元素的元组。如果将其设置为“?”,则算法中出现了故障。也就是说,不,仅仅用括号括起来似乎不是一个有效的解决方案。重新思考代码的这一部分。添加代码注释。感谢您的帮助。但是你能解释一下为什么它应该有四个元素吗?我是pythonDon的新手,请不要更改您正在迭代的列表over@bernard,我有一个类似的问题,你解决了吗?