Пример #1
0
def testBayes(training, test, labels, targetPath):

    if targetPath.endswith('/') == False:
        targetPath += '/'
    if not os.path.exists(os.path.dirname(targetPath)):
        os.makedirs(os.path.dirname(targetPath))

    labelCount = len(labels)
    neurons = training.shape[1] - labelCount

    minTraining = np.min(training[:, 0:neurons])
    minTest = np.min(test[:, 0:neurons])
    absoluteMin = 0

    if minTraining < minTest:
        absoluteMin = minTraining
    else:
        absoluteMin = minTest

    training[:, 0:neurons] = training[:, 0:neurons] + np.abs(absoluteMin)
    test[:, 0:neurons] = test[:, 0:neurons] + np.abs(absoluteMin)

    PLog = tb.trainBayes(training, labelCount)

    overallCorrect = 0
    overallWrong = 0
    meanAveragePrecision = 0

    featuresByLabel = utility.splitTestFeaturesByLabel(test, len(labels))

    confusionMatrix = np.zeros((labelCount, labelCount))

    for labelIndex, activations in enumerate(featuresByLabel):
        counter = 0
        while counter < activations.shape[0]:
            currentActivation = activations[counter, 0:neurons]
            searchedLabel = np.argmax(activations[counter, neurons:])

            predictions = currentActivation * PLog
            predictions = np.sum(predictions, axis=1)

            if np.argmax(predictions) == searchedLabel:
                overallCorrect += 1
            else:
                overallWrong += 1

            confusionMatrix[labelIndex, np.argmax(predictions)] += 1
            averagePrecision = 0
            relevant = 0

            predictedLabelsSorted = np.argsort(predictions)[::-1]

            for idx, value in enumerate(predictedLabelsSorted):
                indicator = 0
                if (value == searchedLabel):
                    indicator = 1
                    relevant += 1

                precision = float(relevant) / (idx + 1)
                averagePrecision += (precision * indicator)

            if relevant != 0:
                averagePrecision = float(averagePrecision) / relevant
            meanAveragePrecision += averagePrecision

            counter += 1

    meanAveragePrecision = float(meanAveragePrecision) / test.shape[0]

    logger.info(' Accuracy: ' +
                str(float(overallCorrect) / (overallWrong + overallCorrect)))
    logger.info(' Mean average precision: ' + str(meanAveragePrecision))

    utility.plotConfusionMatrix(confusionMatrix, labels,
                                targetPath + "confusion.pdf")

    results = [0, meanAveragePrecision, overallCorrect, overallWrong]
    logger.info('Writing file ' + targetPath + "overview.csv")
    np.savetxt(targetPath + "overview.csv", results, delimiter=',')
Пример #2
0
def runTest(clusters, testFeatures, labels, perLabel):

   labelCount = len(labels)
   neurons = activations.shape[1] - labelCount

   confusionMatrix = np.zeros((labelCount,labelCount))

   # split activations by label
   activationsByLabel = utility.splitTestFeaturesByLabel(testFeatures, labelCount)

   overallCorrect = 0
   overallWrong = 0
   meanAveragePrecision = 0

   # evaluate
   for labelIndex, values in enumerate(activationsByLabel):
      confusion = np.zeros((1,len(labels)))

      activationCounter = 0
      while activationCounter < values.shape[0]:
         currentActivation = values[activationCounter]
         clusterRanking = kMeans_core.predictSimple(clusters, currentActivation, neurons)
         bestCluster = clusters[clusterRanking[0]]

         confusion[0,np.argmax(bestCluster[neurons:])] += 1

         if np.argmax(bestCluster[neurons:]) == np.argwhere(currentActivation[neurons:] == 1):
            overallCorrect += 1
         else:
            overallWrong += 1

         averagePrecision = 0
         relevant = 0
         sortedPredictions = None
         if perLabel == False:
            sortedPredictions = np.argsort(bestCluster[neurons:])[::-1].tolist()
         else:
            sortedPredictions = np.argmax(clusters[clusterRanking][:,neurons:], axis=1)
            # logger.debug(sortedPredictions)
         for idx, value in enumerate(sortedPredictions):
            indicator = 0
            if(value == np.argwhere(currentActivation[neurons:] == 1)):
               if perLabel == False and bestCluster[neurons + value] == 0: # ingore if 0 in histogram
                  continue
               indicator = 1
               relevant += 1

            precision = float(relevant) / (idx + 1)
            averagePrecision += (precision * indicator)


         if relevant != 0:
            averagePrecision = float(averagePrecision) / relevant
         meanAveragePrecision += averagePrecision

         activationCounter += 1

      confusionMatrix[labelIndex,:] = confusion

   meanAveragePrecision = float(meanAveragePrecision) / activations.shape[0]
   
   logger.info(' Accuracy: ' + str(float(overallCorrect)/(overallWrong + overallCorrect)))
   logger.info(' Mean average precision: '+str(meanAveragePrecision))

   return [confusionMatrix, meanAveragePrecision, overallCorrect, overallWrong]
Пример #3
0
def testBayes(training, test, labels, targetPath):

   if targetPath.endswith('/') == False:
      targetPath += '/'
   if not os.path.exists(os.path.dirname(targetPath)):
      os.makedirs(os.path.dirname(targetPath))

   labelCount = len(labels)
   neurons = training.shape[1] - labelCount

   minTraining = np.min(training[:,0:neurons])
   minTest = np.min(test[:,0:neurons])
   absoluteMin = 0

   if minTraining < minTest:
      absoluteMin = minTraining
   else:
      absoluteMin = minTest

   training[:,0:neurons] = training[:,0:neurons] + np.abs(absoluteMin)
   test[:,0:neurons] = test[:,0:neurons] + np.abs(absoluteMin)

   PLog = tb.trainBayes(training, labelCount)

   overallCorrect = 0
   overallWrong = 0
   meanAveragePrecision = 0

   featuresByLabel = utility.splitTestFeaturesByLabel(test, len(labels))

   confusionMatrix = np.zeros((labelCount,labelCount))

   for labelIndex, activations in enumerate(featuresByLabel):
      counter = 0
      while counter < activations.shape[0]:
         currentActivation = activations[counter,0:neurons]
         searchedLabel = np.argmax(activations[counter,neurons:])

         predictions = currentActivation * PLog
         predictions = np.sum(predictions, axis=1)

         if np.argmax(predictions) == searchedLabel:
            overallCorrect += 1
         else:
            overallWrong += 1

         confusionMatrix[labelIndex,np.argmax(predictions)] += 1
         averagePrecision = 0
         relevant = 0

         predictedLabelsSorted = np.argsort(predictions)[::-1]

         for idx, value in enumerate(predictedLabelsSorted):
            indicator = 0
            if(value == searchedLabel):
               indicator = 1
               relevant += 1

            precision = float(relevant) / (idx + 1)
            averagePrecision += (precision * indicator)

         if relevant != 0:
            averagePrecision = float(averagePrecision) / relevant
         meanAveragePrecision += averagePrecision

         counter += 1

   meanAveragePrecision = float(meanAveragePrecision) / test.shape[0]

   logger.info(' Accuracy: ' + str(float(overallCorrect)/(overallWrong + overallCorrect)))
   logger.info(' Mean average precision: '+str(meanAveragePrecision))

   utility.plotConfusionMatrix(confusionMatrix, labels, targetPath + "confusion.pdf")

   results = [0, meanAveragePrecision, overallCorrect, overallWrong]
   logger.info('Writing file ' + targetPath + "overview.csv")
   np.savetxt( targetPath + "overview.csv", results, delimiter=',')