def computeRankMetrics(self, X, Y, indexList, bestLearners, standardiserY, labelIndex): #Some code to do ranking using the learner predictors i = 0 rankMetrics = numpy.zeros((len(indexList), self.boundsList[labelIndex].shape[0]-1)) for idxtr, idxts in indexList: logging.info("Iteration " + str(i)) trainX, testX = X[idxtr, :], X[idxts, :] trainY, testY = Y[idxtr], Y[idxts] bestLearners[i].learnModel(trainX, trainY) predY = bestLearners[i].predict(testX) gc.collect() #Now output 3 sets of ranked scores predY = standardiserY.unstandardiseArray(predY) testY = standardiserY.unstandardiseArray(testY) YScores = MetabolomicsUtils.scoreLabels(predY, self.boundsList[labelIndex]) YIndList = MetabolomicsUtils.createIndicatorLabel(testY, self.boundsList[labelIndex]) for j in range(self.boundsList[labelIndex].shape[0]-1): rankMetrics[i, j] = Evaluator.auc(YScores[:, j], YIndList[j]) i += 1 logging.debug(rankMetrics) return rankMetrics
def meanAUC(self, predY, testY, labelIndex, standardiserY): predY = standardiserY.unstandardiseArray(predY) testY = standardiserY.unstandardiseArray(testY) YScores = MetabolomicsUtils.scoreLabels(predY, self.boundsList[labelIndex]) YIndList = MetabolomicsUtils.createIndicatorLabel(testY, self.boundsList[labelIndex]) rankMetrics = numpy.zeros(self.boundsList[labelIndex].shape[0]-1) for j in range(rankMetrics.shape[0]): rankMetrics[j] = Evaluator.auc(YScores[:, j], YIndList[j]) return numpy.mean(rankMetrics)
def testScoreLabel(self):# numExamples = 10 Y = numpy.random.rand(numExamples) bounds = numpy.array([0, 0.2, 0.8, 1.0]) YScores = MetabolomicsUtils.scoreLabels(Y, bounds) inds1 = numpy.argsort(Y) inds2 = numpy.argsort(YScores[:, 0]) inds3 = numpy.argsort(YScores[:, -1]) inds4 = numpy.argsort(numpy.abs(Y - 0.5)) inds5 = numpy.argsort(YScores[:, 1]) self.assertTrue((inds1 == inds3).all()) self.assertTrue((inds1 == numpy.flipud(inds2)).all()) self.assertTrue((inds4 == numpy.flipud(inds5)).all()) #Test we don't get problems when Y has the same values Y = numpy.ones(numExamples) YScores = MetabolomicsUtils.scoreLabels(Y, bounds) self.assertTrue((YScores == numpy.ones((Y.shape[0], 3))).all())