示例#1
0
def run_test(trX, trY,res_file):
    desired_dt20 = 0.78
    desired_dt50 = 0.78
    desired_knn1 = 0.70
    desired_knn3 = 0.73
    
    print '\n\nFirst, we run DT and KNN on the training/development data to '
    print 'ensure that we are getting roughly the right accuracies.'
    print 'We use the first 80% of the data as training, and the last'
    print '20% as test.'
    
    
    decTree = DT.DT()
    res = 1

    print '\nDT (cutoff=20)...'
    sizeX = trX.shape
    end = int(np.round(sizeX[0]*0.80,decimals=0))
    testRun = tt.TrainTest(decTree, trX[:end, :], trY[:end], trX[end:, :], trY[end:], 20)
    acc = testRun.run_tt()
    res += testRun.verifyAcc(acc['acc'], desired_dt20)
    print'\nTrainTime, TestTime', acc['trainTime'], acc['testTime']
    res_file.write('\nDT (cutoff=20)')
    res_file.write('\nTrainTime, TestTime ' + str(acc['trainTime']) + ', ' + str(acc['testTime']))
 
    print '\nDT (cutoff=50)...'
    testRun = tt.TrainTest(decTree, trX[:end, :], trY[:end], trX[end:sizeX[0], :], trY[end:sizeX[0]], 50)
    acc = testRun.run_tt()
    res += testRun.verifyAcc(acc['acc'], desired_dt50)
    print'\nTrainTime, TestTime', acc['trainTime'], acc['testTime']
    res_file.write('\nDT (cutoff=50)')
    res_file.write('\nTrainTime, TestTime ' + str(acc['trainTime']) + ', ' + str(acc['testTime']))
    
    knnModel = KNN.KNN()
    print '\nKNN (K=1)'
    max_size = sizeX[0] if sizeX[0] < 10001 else 10000
    end = int(np.round(max_size*0.80,decimals=0)) 
    testRun = tt.TrainTest(knnModel, trX[:end, :], trY[:end], trX[end:sizeX[0], :], trY[end:sizeX[0]], 1)
    acc = testRun.run_tt()
    res += testRun.verifyAcc(acc['acc'], desired_knn1)
    print'\nTrainTime, TestTime', acc['trainTime'], acc['testTime']
    res_file.write('\nKNN (K=1)')
    res_file.write('\nTrainTime, TestTime ' + str(acc['trainTime']) + ', ' + str(acc['testTime']))
 
    print '\nKNN (K=3)'
    testRun = tt.TrainTest(knnModel, trX[:end, :], trY[:end], trX[end:sizeX[0], :], trY[end:sizeX[0]], 3)
    acc = testRun.run_tt()
    res += testRun.verifyAcc(acc['acc'], desired_knn3)
    print'\nTrainTime, TestTime', acc['trainTime'], acc['testTime']
    res_file.write('\nKNN (K=3)')
    res_file.write('\nTrainTime, TestTime ' + str(acc['trainTime']) + ', ' + str(acc['testTime']))

    raw_input('\nPress enter to continue...')
    
    return
#CopaLeche.updateCopa()

#MENUUU

opcion = int(
    input(
        "MENU\n 1-ORGANIZACIONES \n 2-COPAS \n 3-PAISES \n 4-LIGAS \n 5-EQUIPOS \n 6-JUGADOR \n 7- DTs \n 8- Salir \n OPCION: "
    ))
ORG = Organizacion()
COPA = Copa()
PAIS = Pais()
Ligue = Liga()
Team = Equipo()
Player = Jugador()
DeTe = DT()

while (opcion != 8):

    #opcion = int(input("\n MENU\n 1-ORGANIZACIONES \n 2-COPAS \n 3-PAISES \n 4-LIGAS \n 5-EQUIPOS \n 6-JUGADOR \n 7- DTs \n 8- Salir \n OPCION: "))

    if (opcion == 1):

        opcionOrg = int(
            input(
                "\n 1-CREAR ORGANIZACION \n 2-INSERTAR ORG EN BASE \n 3-VER ORGANIZACIONES DE LA BASE \n 4-MODIFICAR UNA ORGANIZACION \n 5-ELIMINAR UNA ORGANIZACION \n 6-VOLVER AL 1ER MENU \n OPCION: "
            ))

        if (opcionOrg == 1):

            nombre_org = input("Escriba el nombre de la organizacion: ")
示例#3
0
import sys
import time
import marshal
import stat
    

def phfunc(name, obj):
    marshal.dump(obj, open(name,'w'))
    
if __name__=='__main__':
    bt = time.time()
    fname=sys.argv[1]
    mtime=os.stat(fname)[stat.ST_MTIME]
    cform=sys.argv[1]+'.dtcc'
    try:
        cmtime=os.stat(cform)[stat.ST_MTIME]
        comp_form=marshal.load(open(cform))
    except:
        comp_form=None
        cmtime=-1
    d=DT.DT(open(fname).read(), fname, comp_form, mtime, cmtime,
            lambda x, y=cform: phfunc(y, x))
    class dumb: pass
    ns=dumb()
    text = d(ns)
    et = time.time()
    print text
    print 'elapsed time:', et - bt
    
    
示例#4
0
'''
import numpy as np
import DT as dt
import KNN as knn


if __name__ == '__main__':

    print 'running tests on DT and KNN'
    #This is the class example [mathy, test >= 80, project >= 80, early]
    #with a slight change so that non-mathy first splits on early.
    trX=np.array([[1,1,1,1],[1,1,1,0],[0,1,0,1],[0,0,1,1],[0,0,1,1],[0,0,0,0],[0,0,0,0],[1,0,1,1],[1,0,0,1],[0,0,1,1],[1,0,0,0],[0,0,1,1],[0,1,0,1],[0,0,1,0]])
    trY=np.array([[1],[1],[0],[0],[0],[1],[0],[1],[0],[0],[0],[0],[0],[1]])
    deX = np.array([[0,1,0,0],[0,0,1,0],[0,1,1,1]])
    deY = np.array([[0],[1],[0]])

    decTree = dt.DT()
    print 'DT, cutoff=0'
    trainModel = decTree.res('train',X=trX,Y=trY,h_param=0)
    decTree.DTdraw(trainModel)
    output = decTree.res('predict',model=trainModel,test_case=deX)
    print output
    
    knnMode = knn.KNN()
    print 'KNN, k=1'
    trainModel = knnMode.res('train',X=trX,Y=trY,h_param=1)
    output = knnMode.res('predict',model=trainModel,test_case=deX)
    print output
    
    print 'Done'
示例#5
0
            res_file.write('\n' + disp + '\ndone')


        base = 'baseline'+data_types[i]
        if base not in results.keys():
            print "Lets run some baseline measures..."
            res = run_test(trX,trY,res_file)
            res_file.write('\n' + base + '\n')
            res_file.write(str(res)) 
            raw_input('Press enter to continue...')
 
        dec = 'dt'+data_types[i]
        if dec not in results.keys():
            print '\nNow we vary the cutoff for the decision tree and see how it affects accuracy...'
            thresh = [5,10,20,40,80,160]
            decTree = DT.DT()
            res = run_comps(decTree, thresh, trX[0:4800, :], trY[0:4800], trX[4801:6000, :], 
                        trY[4801:6000],"Figure 2: DT cutoff versus accuracy (MNIST)","DT cutoff","../figure2.png")
            results[dec] = res
            res_file.write('\n' + dec + '\n') 
            res_file.write(str(res))
            raw_input('Press enter to continue...')
     
        neigh = 'knn'+data_types[i]
        if neigh not in results.keys():
            print '\nNow we vary the k for the KNN classifier and see how it affects accuracy...'
            allK = [1,8,16,32,64,128]
            knnModel = KNN.KNN()
            res = run_comps(knnModel, allK, trX[0:2000, :], trY[0:2000], trX[2001:2501, :], 
                         trY[2001:2501],"Figure 3: KNN count versus accuracy (MNIST)","KNN count","../figure3.png")
            results[neigh] = res
示例#6
0
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 16 15:19:55 2017

@author: Thomas
"""

import DT
import pool
import row
import server

h = DT.DT('dc.in')
h.disp()
def pertest(name, item):
    print name, item
    print len(pfunc(item))


class nsc:
    this = 'that'
    num = 1


ns = nsc()

import DT
t = timer.Timer()
print t, 'start'
x = DT.DT(open('tests/test1.dtml').read())
print t, 'cooked'
output = x(ns)
print t, 'rendered'

#node=x.node
#for i in node.children:
#    pertest('i',i)
#    if hasattr(i, 'children'):
#        for j in i.children:
#            pertest('j',j)
#            if hasattr(j,'children'):
#                for k in j.children:
#                    pertest('k', k)

print 'serializing'
def TrainingOnDT():
    global filenameForTrainingResult,filenameForPredictedResult,\
        DTClass_weight,DTMax_features,\
        DTCriterion,unknowRow,hamRow,spamRow,PercisionRow,recallRow

    print DTClass_weight.get()
    DTconfusion_matrix, percision, recall, combinedResultOnActualAndPred = DT.DT(
        DTCriterion.get(), DTMax_features.get(), DTClass_weight.get())
    print DTconfusion_matrix, percision, recall
    strOnUnknowRow = "unknown:            " +  "            ".join( str(x)
                                                                    for x in \
            DTconfusion_matrix[0])
    strOnHamRow= "Ham:           " + "         ".join(str(x) for x in \
                                                DTconfusion_matrix[1])
    strOnSpamRow = "Spam:           " + "          ".join(str(x) for x in \
                                                DTconfusion_matrix[2])
    strOnPercisionRow = "precision On spam is : " + str(percision)
    strOnRecallRow = "Recall on spam is : " + str(recall)
    unknowRow.set(strOnUnknowRow)
    hamRow.set(strOnHamRow)
    spamRow.set(strOnSpamRow)
    PercisionRow.set(strOnPercisionRow)
    recallRow.set(strOnRecallRow)

    # ####write Condussion result into file
    print filenameForTrainingResult

    if (filenameForTrainingResult == ''):
        pass
    else:
        fileOnTrainingResult = open(filenameForTrainingResult, 'w')
        try:
            fileOnTrainingResult.write("The result of confusion matrix")
            fileOnTrainingResult.write('\r\n')
            fileOnTrainingResult.write("             predicted           ")
            fileOnTrainingResult.write('\r\n')
            fileOnTrainingResult.write(
                "                     unknown    ham    spam")
            fileOnTrainingResult.write('\r\n')
            fileOnTrainingResult.write(strOnUnknowRow)
            fileOnTrainingResult.write('\r\n')
            fileOnTrainingResult.write(strOnHamRow)
            fileOnTrainingResult.write('\r\n')
            fileOnTrainingResult.write(strOnSpamRow)
            fileOnTrainingResult.write('\r\n')
            fileOnTrainingResult.write(strOnPercisionRow)
            fileOnTrainingResult.write('\r\n')
            fileOnTrainingResult.write(strOnRecallRow)
        finally:
            fileOnTrainingResult.close()

    # ###Write ID --Actual result --Predicted resutl into file
    print filenameForPredictedResult
    if (filenameForPredictedResult == ''):
        pass
    else:
        fileOnIndividualResult = open(filenameForPredictedResult, 'w')
        fileOnIndividualResult.write("     ID           "
                                     "\tActual\tPredicted\r\n")
        try:
            for i in combinedResultOnActualAndPred:
                fileOnIndividualResult.write(i)
            # fileOnIndividualResult.write('\r\n')

        finally:
            fileOnIndividualResult.close()

    print strOnUnknowRow