def stackTest(fModels, K, testFile, DIR): """ Implementation of algorithm 21. Takes in k classifiers( fModel ), and fin are strings (filename), k is the level of stack """ fIn = map(lambda k:DIR+"test%d.megam"%k, range(K)) fOut = map(lambda k: DIR+"Y_test%d.megam"%k, range(K)) data = stackFeatures.initFeatures(citeFile, testFile, fIn[0]) err = [0]*K for k in range(0,K): if k!=0: stackFeatures.stackFeatures(data, fIn[k-1], fOut[k-1], fIn[k], k) # test print "********** test @ stack %d **********"%k err[k] = predict(fModels[k],fIn[k],fOut[k]) return fOut, err
def stackTrain(K, contentFile, DIR): """ implementation stacking algorithm. Takes in k which is the number of stacks """ fIn = map(lambda k: DIR+"train%d.megam"%k, range(K)) fOut = map(lambda k: DIR+"Y_train%d.megam"%k, range(K)) err = [0]*K classifiers = map(lambda k: DIR+"model%d.megam"%k, range(K)) # name of the classifiers for k in range(0,K): if k == 0: data = stackFeatures.initFeatures(citeFile, contentFile, fIn[k]) else: stackFeatures.stackFeatures(data, fIn[k-1],fOut[k-1],fIn[k],k) # train print "********** train stack %d **********"%k trainMegam(fIn[k], classifiers[k]) # test, get \hat Y[k], here don't do stackTest but reuse err[k] = predict(classifiers[k], fIn[k], fOut[k]) return (classifiers,err)