Hi guys,

Having a bit of a problem with reading and writing to a file. I have a file that has a lot of symbolic stuff I want to remove. And get a new kinda clean file with just the numbers and names. And it finds the line where it says class, and prints out a confusion matrix. However, I was printing the first value of all matrices and see that it does not match with the name I am printing. The fileout is being updated i cycle to late or to early. I am not sure. Maybe you guys can see the mistake in here somewhere. Much appriciated!

import numpy as np
import os
import matplotlib.pyplot as plt
import subprocess

path = r'C:\NeuralNetwork\ResultNN'
extension=".res"

def getFilenameforGivenExtension(sourceFolder,extension):
    names=[]
    for fileName in os.listdir(sourceFolder):
        if fileName.endswith(extension):
            names.append(os.path.join(sourceFolder,fileName))
    return names

resfiles = getFilenameforGivenExtension(path, extension)

def getSNNSNameFromFileName(fileName):
    SNNSName=''
    if os.path.isfile(fileName):
        #print 'The file exists'
        basename=os.path.basename(fileName)
        SNNSName=basename.rstrip('SNNS_'+'_'+'_'+'.res')
        #print basename
    return SNNSName

for s in range(len(resfiles)):
    filename = resfiles[s]+'.txt'
    if NNname.endswith('dtd_nrm'):
        b = 'WTA'
        command = ['analyze.exe -i ' + resfiles[s] + ' -o ' + resfiles[s]+'.txt' + ' -s -m -c -e ' + b + '']
        #print command
        subprocess.call(command[0])
        fin = open(filename, 'r')
        fileout = path+'\\temp.txt'
        fout = open(fileout, 'w')
        linesIn = fin.readlines()

        for line in linesIn:
            fout.write(line.lower().replace(':', '').replace('(','').replace(')', '').replace('+', '').replace('|', '').replace('-', ''))
        fin.close
        fout.close

        fIn = open(fileout, 'r')

        lines = fIn.readlines()

        flag = False

        z = [line.strip().split() for line in lines]

        confusedMatrixList = []

        for row in z:
            if len(row) and row[0] == 'class':
                flag = True

            if flag and len(row):
                confusedMatrixList.append(row)

        confusedMatrixUpdated = []
        for line in confusedMatrixList[1:]:
            confusedMatrixUpdated.append(line[1:])

        ConfusionMatrix = np.array(confusedMatrixUpdated, dtype = float)
        B = ConfusionMatrix[0, 0]
        print str(B)+' for '+str(getSNNSNameFromFileName(resfiles[s]))

print outs:

4.0 for SNNS_L_dif_nrm
44.0 for SNNS_L_dtd_nrm
1.0 for SNNS_L_set_fzz
0.0 for SNNS_T_dtd_nrm
42.0 for SNNS_V_dtd_nrm

It is supposed to be:

44.0 for SNNS_L_dif_nrm
1.0 for SNNS_L_dtd_nrm
0.0 for SNNS_L_set_fzz
42.0 for SNNS_T_dtd_nrm
4.0 for SNNS_V_dtd_nrm

Sorry, my bad!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.