コード例 #1
0
ファイル: HMM.py プロジェクト: letsdeckersousa/bigdata_basic
def aPosterioriDecoding(seq, fileNameEmis, fileNameTrans):
    statesGraph = graph.createGraph(fileNameEmis,
                                    fileNameTrans)  # it's a dictionary
    f = forward(seq, fileNameEmis, fileNameTrans)
    b = backward(seq, fileNameEmis, fileNameTrans)
    seq = seq[1:-1]
    psm = f[len(f) - 1][len(f[0]) - 1]
    pi = matrix.createMatrix(len(statesGraph["A"]["A", "States"]), len(seq),
                             0.)
    print "F:"
    matrix.printMatrix(f)
    print "B:"
    matrix.printMatrix(b)
    print "Seq:", seq
    print "Size of Seq: ", len(seq)

    for i in range(len(seq)):  #pi(i) -> position in the sequence = collunms
        scores = []
        for k in range(1, len(statesGraph["A"][
                "A", "States"])):  #num of states that produce chars = lines
            scores.append((f[k][i + 1] * b[k][i + 1]) / psm)
        pi[0][i] = scores.index(max(scores)) + 1  #states are from 1 to lines-1
        pi[1][i] = scores[0]
        pi[2][i] = scores[1]

        del (scores)
    return pi
コード例 #2
0
def main(argv):
    inputfile1 = ''
    inputfile2 = ''
    print(len(argv) / 2) - 1

    try:
        inputfileEmission = argv[0]
        inputfileTransmission = argv[1]
        seqfile = argv[2]
        seq = readSequence(seqfile)
        print "seqfile: " + str(seqfile)
        process = str(seqfile[len(seqfile) - 1])
        print "Process: " + process

        print 'Input file is "', inputfile1
        print 'Input file is "', inputfile2

        # To test, please, uncomment what you want to test:
        filename = "output.txt" + process
        #filename  = "output.txt"
        stringtowrite = "method: aPosterioriDecoding \n"
        #m = forward(seq, inputfileEmission, inputfileTransmission)
        #print backward(seq, inputfileEmission, inputfileTransmission)
        #trace = viterbi(seq, inputfileEmission, inputfileTransmission)
        #print trace
        #print giveViterbiTrace(trace)
        apd = aPosterioriDecoding(seq, inputfileEmission,
                                  inputfileTransmission)
        matrix.printMatrix(apd)
        stringtowrite += matrix.stringToMatrix(apd)
        writeFile(filename, stringtowrite)

    except getopt.GetoptError:
        print 'HMM.py <inputfile1> <inputfile2> -o <outputfile>'
        sys.exit(2)
コード例 #3
0
ファイル: HMM.py プロジェクト: letsdeckersousa/bigdata_basic
def forward(seq, filenameEmission, filenameTransmission):
    forwardGraph = graph.createGraph(filenameEmission, filenameTransmission)
    #print forwardGraph
    char = seq[1]
    states = forwardGraph["E"]["E", "States"]
    statesCompl = states[:]
    statesCompl.insert(0, "0")
    statesCompl.append("0")
    statesCompl = ["0", "Y", "N", "0"]
    print statesCompl

    m = matrix.createMatrix(len(states) + 2, len(seq), 0)
    # intialisation part
    matrix.atualiseValueIntoMatrix(m, 0, 0, 1)
    # recurrence part
    # recurrence 1
    for i in range(1, len(m) - 1):
        m[i][1] = float(forwardGraph["A"]["0", statesCompl[i]]) * float(
            forwardGraph["E"][statesCompl[i], char])
    # recurrence 2
    for j in range(2, len(m[0]) - 1):  #jump the first 2 collumns
        char = seq[j]
        for i in range(1, len(m) - 1):  # jump the begin and end states
            for l in range(1, len(m) - 1):
                #print "L:",statesCompl[l],", K:",statesCompl[i]
                m[i][j] += float(m[l][j - 1]) * float(
                    forwardGraph["A"][statesCompl[l], statesCompl[i]]) * float(
                        forwardGraph["E"][statesCompl[i], char])
                '''print "partial value: ",float(m[i][j])
                print "lines value:", float(m[l][j-1])
                print "AKL: ",float(forwardGraph["A"][statesCompl[l],statesCompl[i]])
                print "Elc: ", float(forwardGraph["E"][statesCompl[i],char])'''
    for i in range(1, len(m) - 1):
        m[len(m) - 1][len(m[0]) - 1] += float(m[i][len(m[0]) - 2]) * float(
            forwardGraph["A"][statesCompl[i], "0"])
    matrix.printMatrix(m)
    return m
コード例 #4
0
ファイル: Hw1.py プロジェクト: s410385015/MeachinLearning
def main():
    """
    path=input("Enter file name with path(eg. D:\lecture1\data.txt):")
    base=input("Enter the number of polynomial bases:")
    l=input("Enter lambda:")

    =ReadFile(path)
    base=int(base)
    l=float(l)
    """

    d = ReadFile('D:\桌面用\MeachinLearning\MLclass\lecture1\data.txt')
    #d=ReadFile('data.txt')
    base = 3
    l = 0.5

    print("Problem A.")
    A, B = FindAandB(d, base)
    #matrix.printMatrix(A,"A")
    #matrix.printMatrix(B,"B")

    AT = matrix.TransposeMatrix(A)
    #matrix.printMatrix(AT,"AT")

    ATA = matrix.Mul(AT, A)
    #matrix.printMatrix(ATA,"ATA")

    eye = matrix.eyeMatrix(base, l)
    _ATA = matrix.Add(ATA, eye)

    #matrix.printMatrix(_ATA,"ATA+λi")

    L, U = matrix.FindLU(_ATA)
    #matrix.printMatrix(L,"L")
    #matrix.printMatrix(U,"U")

    ATAi = matrix.FindInverseFromLU(L, U)
    print("1.")
    matrix.printMatrix(ATAi, "Inverse of ATA+λi")

    print("2.")
    _x = matrix.Mul(matrix.Mul(ATAi, AT), B)
    #matrix.printMatrix(_x,"_x")
    printEquation(_x)

    error = matrix.Sub(matrix.Mul(A, _x), B)

    w_error = matrix.Mul(matrix.TransposeMatrix(_x), _x)
    w_error = matrix.Factor(w_error, l)
    #matrix.printMatrix(w_error,"weight error")
    value_error = matrix.Mul(matrix.TransposeMatrix(error), error)
    #matrix.printMatrix(value_error,"value error")
    matrix.printMatrix(matrix.Add(value_error, w_error), "a_error")

    print("------------------------")
    #------------------------------------------------------------------
    print("Problem b.")
    h = HessianMatrix(d, base)
    #matrix.printMatrix(h,"Hessian Matrix")
    hL, hU = matrix.FindLU(h)
    hi = matrix.FindInverseFromLU(hL, hU)

    # Random generate the coefficient
    _x0 = [[random.random()] for i in range(base)]
    #matrix.printMatrix(_x0,"_x0")
    g = GenerateGradientMatrix(_x0, d, base)
    #matrix.printMatrix(g,"Gradient")

    print("1.")
    _x0 = matrix.Sub(_x0, matrix.Mul(hi, g))
    #matrix.printMatrix(_x0,"solution")
    printEquation(_x0)
    error = matrix.Sub(matrix.Mul(A, _x0), B)
    value_error = matrix.Mul(matrix.TransposeMatrix(error), error)
    matrix.printMatrix(value_error, "b_error")
コード例 #5
0
ファイル: main.py プロジェクト: igomez10/Algorithms
                arr[ci][cj-1] = 0
                seen[(ci, cj-1)] = True

            if ci < len(arr)-1 and (ci+1, cj) not in seen and arr[ci+1][cj] == 1:
                queue.append((ci+1, cj))
                arr[ci+1][cj] = 0
                seen[(ci+1, cj)] = True

            if cj < len(arr[ci])-1 and (ci, cj+1) not in seen and arr[ci][cj+1] == 1:
                queue.append((ci, cj + 1))
                arr[ci][cj+1] = 0
                seen[(ci, cj+1)] = True


arr = [
    [1, 1, 1, 0],
    [1, 1, 1, 0],
    [1, 0, 0, 0],
    [1, 0, 0, 0],
    [1, 0, 0, 0],
    [1, 0, 0, 0],
    [1, 0, 0, 0],
    [1, 0, 0, 0],
]

expectedResult = 2

s = Solution()

printMatrix(arr)
コード例 #6
0
ファイル: HMM.py プロジェクト: letsdeckersousa/bigdata_basic
    matrix.printMatrix(b)
    print "Seq:", seq
    print "Size of Seq: ", len(seq)

    for i in range(len(seq)):  #pi(i) -> position in the sequence = collunms
        scores = []
        for k in range(1, len(statesGraph["A"][
                "A", "States"])):  #num of states that produce chars = lines
            scores.append((f[k][i + 1] * b[k][i + 1]) / psm)
        pi[0][i] = scores.index(max(scores)) + 1  #states are from 1 to lines-1
        pi[1][i] = scores[0]
        pi[2][i] = scores[1]

        del (scores)
    return pi


# To test, please, uncomment what you want to test:
filename = "output.txt"
stringtowrite = "method: aPosterioriDecoding \n"
#m = forward(seq, "probabilityEmission.txt", "probabilityTransmission.txt")
#print backward(seq, "probabilityEmission.txt", "probabilityTransmission.txt")
#trace = viterbi(seq, "probabilityEmission.txt", "probabilityTransmission.txt")
#print trace
#print giveViterbiTrace(trace)
apd = aPosterioriDecoding(seq, "probabilityEmission.txt",
                          "probabilityTransmission.txt")
matrix.printMatrix(apd)
stringtowrite += matrix.stringToMatrix(apd)
writeFile(filename, stringtowrite)