def runNaiveBayesMultiTest(datasize=100): trainsampleinput = samples.readInput('digitdata/trainingimages', DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) trainlabels = samples.readLabels('digitdata/traininglabels', 'multi') datasize = (datasize * len(trainsampleinput)) / 100 labelpos = getLabelPosition(trainlabels[:datasize], 'multi') trainfeatures = samples.featureExtraction(trainsampleinput[:datasize], 4, 4) # getLabelDetails(trainlabels, 'multi', datasize) trainprobs = np.loadtxt("naiveprobmulti" + str(datasize) + ".txt") trainprobstrans = np.transpose(trainprobs) testsampleinput = samples.readInput('digitdata/testimages', DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) testlabels = samples.readLabels('digitdata/testlabels', 'multi') testfeatures = samples.featureExtraction(testsampleinput, 4, 4) count = 0 errorcount = 0 for sample in testfeatures: # profile.runctx('print(naiveBayesTrain(labelpos, trainfeatures, sample, trainprobstrans)); print()',globals(),{'labelpos': labelpos, 'trainfeatures': trainfeatures, 'sample': sample, 'trainprobstrans':trainprobstrans[2] },) prediction = naiveBayesTrain(labelpos, trainfeatures, sample, trainprobstrans[2]) # print "predicted %s actual %s" % (prediction, testlabels[count]) if prediction != testlabels[count]: errorcount += 1 count += 1 prederror = float(errorcount) / len(testlabels) print "Prediction accuracy for Naive Bayes MultiClass %s." % ( (1 - prederror) * 100)
def runNaiveBayesBinaryTest(datasize): trainsampleinput = samples.readInput('facedata/facedatatrain', FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT) datasize = (datasize * len(trainsampleinput)) / 100 trainlabels = samples.readLabels('facedata/facedatatrainlabels', 'binary') labelpos = getLabelPosition(trainlabels[:datasize], 'binary') trainfeatures = samples.featureExtraction(trainsampleinput[:datasize], 6, 7) getLabelDetails(trainlabels, 'binary', datasize) trainprobs = np.loadtxt("naiveprobbinary" + str(datasize) + ".txt") trainprobstrans = np.transpose(trainprobs) testsampleinput = samples.readInput('facedata/facedatatest', FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT) testlabels = samples.readLabels('facedata/facedatatestlabels', 'binary') testfeatures = samples.featureExtraction(testsampleinput, 6, 7) count = 0 errorcount = 0 for sample in testfeatures: # profile.runctx('print(naiveBayesTrain(labelpos, trainfeatures, sample, trainprobstrans)); print()',globals(),{'labelpos': labelpos, 'trainfeatures': trainfeatures, 'sample': sample, 'trainprobstrans':trainprobstrans[2] },) prediction = naiveBayesTrain(labelpos, trainfeatures, sample, trainprobstrans[2]) # print "predicted %s actual %s" % (prediction, testlabels[count]) if prediction != testlabels[count]: errorcount += 1 count += 1 prederror = float(errorcount) / len(testlabels) print "Prediction accuracy for Naive Bayes Binary Class data %s." % ( (1 - prederror) * 100)
def runKnnTesterMulti(datasize=100): print 'Running KNN MultiClass for datasize %s' % (datasize) testsampleinput = samples.readInput('digitdata/testimages', DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) testactuallabels = samples.readLabels('digitdata/testlabels', 'multi') testfeatures = knnfeatureExtract(testsampleinput[:datasize]) #testfeatures = samples.featureExtraction(testsampleinput, 4, 4) trainsampleinput = samples.readInput('digitdata/trainingimages', DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) datasize = (datasize*len(trainsampleinput))/100 trainactuallabels = samples.readLabels('digitdata/traininglabels', 'multi') #trainfeatures = samples.featureExtraction(trainsampleinput, 4, 4) trainfeatures = knnfeatureExtract(trainsampleinput[:datasize]) testknn(testfeatures, testactuallabels, trainfeatures, trainactuallabels[:datasize], 5)
def runKnnTesterBinary(datasize=100): print 'Running KNN BinaryClass for datasize %s' % (datasize) testsampleinput = samples.readInput('facedata/facedatatest', FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT) testactuallabels = samples.readLabels('facedata/facedatatestlabels', 'binary','knn') testfeatures = knnfeatureExtract(testsampleinput) #testfeatures = samples.featureExtraction(testsampleinput, 6, 7) trainsampleinput = samples.readInput('facedata/facedatatrain', FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT) datasize = (datasize*len(trainsampleinput))/100 trainactuallabels = samples.readLabels('facedata/facedatatrainlabels', 'binary', 'knn') trainfeatures = knnfeatureExtract(trainsampleinput[:datasize]) #trainfeatures = samples.featureExtraction(trainsampleinput, 6, 7) print('Running KNN binaryclass \n') testknn(testfeatures, testactuallabels, trainfeatures, trainactuallabels[:datasize], 5)
def runPercepBinaryTrain(maxiter, datasize): sampleinput = samples.readInput('facedata/facedatatrain', FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT) datasize = (datasize * len(sampleinput)) / 100 actuallabels = samples.readLabels('facedata/facedatatrainlabels', 'binary') features = samples.featureExtraction(sampleinput[:datasize], 6, 7) featlength = 0 for label in features[:datasize]: featlength = len(label) break weights = getInitialWeights('binary', featlength) trainPerc(weights, features, actuallabels[:datasize], maxiter, 'binary', datasize)
def runPercepMultiClassTrain(maxiter, datasize): sampleinput = samples.readInput('digitdata/trainingimages', DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) datasize = (datasize * len(sampleinput)) / 100 actuallabels = samples.readLabels('digitdata/traininglabels', 'multi') features = samples.featureExtraction(sampleinput[:datasize], 4, 4) featlength = 0 for label in features[:datasize]: featlength = len(label) break weights = getInitialWeights('multi', featlength) trainPerc(weights, features, actuallabels[:datasize], maxiter, 'multi', datasize)
def runPercepMultiValidator(datasize=100): sampleinput = samples.readInput('digitdata/testimages', DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) actuallabels = samples.readLabels('digitdata/testlabels', 'multi') features = samples.featureExtraction(sampleinput, 4, 4) sampleinputtain = samples.readInput('digitdata/trainingimages', DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) datasize = (datasize * len(sampleinputtain)) / 100 weights = np.loadtxt('percepmultiweights' + str(datasize) + '.txt') count = 0 errorcount = 0 for label in features: guess = guessPercep(label, weights) #print "predicted %s actual %s" % (guess, actuallabels[count]) if guess != int(actuallabels[count]): errorcount += 1 count += 1 prederror = float(errorcount) / len(actuallabels) #prederror = errorcount/len(actuallabels) print "Prediction accuracy for perceptron multi class %s." % ( (1 - prederror) * 100)
def runPercepBinaryValidator(datasize=100): sampleinput = samples.readInput('facedata/facedatatest', FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT) actuallabels = samples.readLabels('facedata/facedatatestlabels', 'binary') features = samples.featureExtraction(sampleinput, 6, 7) sampleinputtrain = samples.readInput('facedata/facedatatrain', FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT) datasize = (datasize * len(sampleinputtrain)) / 100 weights = np.loadtxt('percepbinaryweights' + str(datasize) + '.txt') count = 0 errorcount = 0 for label in features: guess = guessPercep(label, weights) #print "predicted %s actual %s" % (guess, actuallabels[count]) if guess != int(actuallabels[count]): errorcount += 1 count += 1 prederror = float(errorcount) / len(actuallabels) #prederror = errorcount/len(actuallabels) print "Prediction accuracy for perceptron Binry Class %s." % ( (1 - prederror) * 100)