def main_with_validation(fileName, intercept=False, evalFunc = Util.brierScore): trainX, trainY, testX, testY = Util.readData(fileName, intercept=intercept) clf = GLS() regs = np.logspace(-3, 3, 7) xis = np.linspace(-1., 1.5, 26) bestScore, bestXi, bestReg = 1e10, None, None for xi in xis: clf.setXi(xi) print("Current xi = ", xi) score, reg = Util.crossValidate(clf, trainX, trainY, \ evalFunc, 5, "Regular", regs) if score < bestScore: bestScore, bestXi, bestReg = score, xi, reg print("bestScore, bestXi, bestReg = ", bestScore, bestXi, bestReg) clf.setXi(bestXi) clf.setRegular(bestReg) clf.fit(trainX, trainY) testScore = evalFunc(clf.predict(testX), testY) with open("log/GLS_final_log.txt", 'a') as f: log = ','.join([dt.now().strftime("%Y/%m/%d %H:%M"), str(fileName), \ str(bestReg), str(bestXi), str(intercept), \ evalFunc.__name__, str(bestScore), str(testScore)]) f.write(log + '\n')
def main_label(fileName, xi, preproc=False, evalFunc = Util.f1): data = np.loadtxt(fname=fileName, delimiter=",") scoreList = [] clf = GLS() clf.setXi(xi) k = 10 for _ in range(k): trainX, trainY, testX, testY = Util.readData(data, False, preproc) clf.fit(trainX, trainY) s = evalFunc(Util.probToLabel(clf.predict(testX)), testY) print("score = ", s) scoreList.append(s) print("mean score = ", sum(scoreList)/k)
def main_prob(fileName, xi, preproc=False, evalFunc = Util.brierScore): data = np.loadtxt(fname=fileName, delimiter=",") scoreList = [] clf = GLS() clf.setXi(xi) k = 10 for _ in range(k): trainX, trainY, testX, testY = Util.readData(data, False, preproc) clf.fit(trainX, trainY) s = evalFunc(clf.predict(testX), testY) print("score = ", s) scoreList.append(s) score = sum(scoreList)/k print("mean score = ", score) with open("log/GLS_test_log.txt", 'a') as f: log = ','.join([dt.now().strftime("%Y/%m/%d %H:%M"), str(fileName), \ str(preproc), evalFunc.__name__, str(score)]) f.write(log + '\n')