def test_cv_score(self): def label_weight(listInst, fLabel): dblWeight = 0.0 for inst in listInst: if inst.fLabel == fLabel: dblWeight += inst.dblWeight return dblWeight cValues = 4 fxnGen = build_consistent_generator(cValues=cValues) cInst = random.randint(30,60) listLeft = fxnGen(cInst) listRight = [dtree.Instance([cAttr+cValues+1 for cAttr in inst.listAttrs], inst.fLabel) for inst in fxnGen(cInst)] fMajL = dtree.majority_label(listLeft) fMajR = dtree.majority_label(listRight) iterableFolds = dtree.yield_cv_folds(listLeft + listRight, 2) dblScore = dtree.cv_score(iterableFolds) dblL = label_weight(listRight, fMajL) dblR = label_weight(listLeft, fMajR) self.assertAlmostEqual((dblL + dblR)/(2.0*cInst), dblScore)
def test_cv_score(self): def label_weight(listInst, fLabel): dblWeight = 0.0 for inst in listInst: if inst.fLabel == fLabel: dblWeight += inst.dblWeight return dblWeight cValues = 4 fxnGen = build_consistent_generator(cValues=cValues) cInst = random.randint(30, 60) listLeft = fxnGen(cInst) listRight = [ dtree.Instance([cAttr + cValues + 1 for cAttr in inst.listAttrs], inst.fLabel) for inst in fxnGen(cInst) ] fMajL = dtree.majority_label(listLeft) fMajR = dtree.majority_label(listRight) iterableFolds = dtree.yield_cv_folds(listLeft + listRight, 2) dblScore = dtree.cv_score(iterableFolds) dblL = label_weight(listRight, fMajL) dblR = label_weight(listLeft, fMajR) self.assertAlmostEqual((dblL + dblR) / (2.0 * cInst), dblScore)
def yield_cv_folds(listInst, cFold): for cvf in dtree.yield_cv_folds(listInst, cFold): cvf.cMaxLevel = iDepth yield cvf