Ejemplo n.º 1
0
def predict(tree, d):
    data = [item for item in d]
    tag = [item for item in t]
    while isinstance(tree, dict):
        fName = tree.keys()[0]
        findex = -1
        for i in range(len(data)):
            if ("feature" + str(i) == fName):
                findex = i
                break
        if tag[findex] == 1:  #discrete
            try:
                tree = tree[fName][data[findex]]
            except:
                rand = numpy.random.randint(0, 2)
                if rand == 0:
                    return name1
                else:
                    return name2
                return 999
        else:  #numerical
            minv = 0.0
            maxv = 1.0
            delta = (maxv - minv) / NforNumerical + 0.0001
            for j in range(NforNumerical):
                if (data[findex] >= minv + delta * j
                        and data[findex] < minv + delta * (j + 1)):
                    try:
                        tree = tree[fName][j]
                    except:
                        rand = numpy.random.randint(0, 2)
                        if rand == 0:
                            return name1
                        else:
                            return name2
                        return 999
                    break
        datatmp = data[:findex]
        datatmp.extend(data[findex + 1:])
        data = datatmp
        tagtmp = tag[:findex]
        tagtmp.extend(tag[findex + 1:])
        tag = tagtmp
    return tree
Ejemplo n.º 2
0
def predict(tree,d):
    data = [item for item in d]
    tag = [item for item in t]
    while isinstance(tree, dict):
        fName = tree.keys()[0]
        findex = -1
        for i in range(len(data)):
            if ("feature"+str(i) == fName):
                findex = i
                break
        if tag[findex] == 1: #discrete
            try:
                tree = tree[fName][data[findex]]
            except:
                rand = numpy.random.randint(0,2)
                if rand == 0:
                    return name1
                else:
                    return name2
                return 999
        else: #numerical
            minv = 0.0
            maxv = 1.0
            delta = (maxv - minv) / NforNumerical + 0.0001
            for j in range(NforNumerical):
                if (data[findex]>=minv+delta*j and data[findex]<minv+delta*(j+1)):
                    try:
                        tree = tree[fName][j]
                    except:
                        rand = numpy.random.randint(0,2)
                        if rand == 0:
                            return name1
                        else:
                            return name2
                        return 999
                    break
        datatmp = data[:findex]
        datatmp.extend(data[findex+1:])
        data = datatmp
        tagtmp = tag[:findex]
        tagtmp.extend(tag[findex+1:])
        tag = tagtmp
    return tree
Ejemplo n.º 3
0
def predict(tree,d,t):
    data = [item for item in d]
    tag = [item for item in t]
    while isinstance(tree, dict):
        fName = tree.keys()[0]
        findex = -1
        for i in range(len(data)):
            if ("feature"+str(i) == fName):
                findex = i
                break
        if tag[findex] == 1: #discrete
            try:
                tree = tree[fName][data[findex]]
            except:
                rand = numpy.random.randint(0,2)
                if rand == 0:
                    return name1
                else:
                    return name2
                return 999
        else: #numerical
            try:
                if (data[findex]<=tree[fName][3]):
                    tree = tree[fName][0]
                else:
                    tree = tree[fName][1]
            except:
                rand = numpy.random.randint(0,2)
                if rand == 0:
                    return name1
                else:
                    return name2
                return 999

        datatmp = data[:findex]
        datatmp.extend(data[findex+1:])
        data = datatmp
        tagtmp = tag[:findex]
        tagtmp.extend(tag[findex+1:])
        tag = tagtmp
    return tree
Ejemplo n.º 4
0
def predict(tree, d, t):
    data = [item for item in d]
    tag = [item for item in t]
    while isinstance(tree, dict):
        fName = tree.keys()[0]
        findex = -1
        for i in range(len(data)):
            if ("feature" + str(i) == fName):
                findex = i
                break
        if tag[findex] == 1:  #discrete
            try:
                tree = tree[fName][data[findex]]
            except:
                rand = numpy.random.randint(0, 2)
                if rand == 0:
                    return name1
                else:
                    return name2
                return 999
        else:  #numerical
            try:
                if (data[findex] <= tree[fName][3]):
                    tree = tree[fName][0]
                else:
                    tree = tree[fName][1]
            except:
                rand = numpy.random.randint(0, 2)
                if rand == 0:
                    return name1
                else:
                    return name2
                return 999

        datatmp = data[:findex]
        datatmp.extend(data[findex + 1:])
        data = datatmp
        tagtmp = tag[:findex]
        tagtmp.extend(tag[findex + 1:])
        tag = tagtmp
    return tree