def kFoldCrossValidation(data, learner, returnBest=False, folds=10):
    indices = Orange.data.sample.SubsetIndicesCV(data, folds)
    totalSuccessTrain = 0
    totalSuccessTest = 0
    best = [0,None]
    totalClassifications = {}
    for label in data.domain.class_var.values:
        for label2 in data.domain.class_var.values:
            totalClassifications[(str(label),str(label2))] = 0
    for fold in range(folds):
        train = data.select_ref(indices, fold, negate = 1)
        test  = data.select_ref(indices, fold)
        print "Fold %d" % fold
        resultTrain,resultTest,classifier=runTest(train,test,learner,totalClassifications)
        if resultTest == 'Error':
            print 'Error testing fold, skipping'
            folds=folds-1
        else:
            (correct,wrong,classifications)=resultTrain
            successTrain=correct/(correct+wrong)
            (correct,wrong,classifications)=resultTest
            successTest=correct/(correct+wrong)
            if best[0]<successTest:
                best[1]=classifier
                best[0]=successTest
            totalSuccessTest+=successTest
            totalSuccessTrain+=successTrain
            print 'Success train %f, test %f\n'%(successTrain,successTest)
    writeConfusionToFile(classifications,test,'Confusion of %s.csv'%learner.name)
    averageSuccessTest = totalSuccessTest/folds
    averageSuccessTrain = totalSuccessTrain/folds
    print 'Average success train %f, test %f'%(averageSuccessTrain,averageSuccessTest)
    print '----------------------------------------------------------'
    if returnBest:
        return (best[1],averageSuccess)
    return (averageSuccessTrain,averageSuccessTest)
dataset/ETHPy150Open andreykurenkov/emailinsight/pyScripts/orangeClassify.py/kFoldCrossValidation/original