def ntrial(numTrial, numTrain, numTest): for i in range(numTrial): #data [X, Y, T] = dt.sample_poly3(numTrain+numTest); [xTrain, yTrain, tTrain, xTest, yTest, tTest] = util.sepTrainTest4Reg(numTrain,numTest,\ X,T,Y); errTrainPoly, errTestPoly = pR.run (xTrain, yTrain, tTrain, xTest, yTest, tTest, 4); #errTrainPoly, errTestPoly = ppR.run(xTrain, yTrain, tTrain, xTest, yTest, tTest, 4); errTrainBay, errTestBay = bcf.run(xTrain, yTrain, tTrain, xTest, yTest, tTest, 4); errPoly[0].append(round(errTrainPoly,6)); errPoly[1].append(round(errTestPoly,6)); errBay[0].append(round(errTrainBay,6)); errBay[1].append(round(errTestBay,6)); return errPoly, errBay;
t = pl.plot(xTest, yTest, 'go', label="test data") o = pl.plot(xTest, oTest, 'ro', label="predicted") #pl.legend((t,o), ("target", "predicted")); pl.title('Polynomial Regression test set with degree ' + str(K - 1)) if __name__ == '__main__': K = 4 kList = [3] errorTrain = [0] * len(kList) errorTest = [0] * len(kList) #data [X, Y, T] = dt.sample_poly3(60) [xTrain, yTrain, tTrain, xTest, yTest, tTest] = util.sepTrainTest4Reg(40,20,\ X,T,Y) oTrain, mean = bayesianCurveFitting(xTrain, np.array(yTrain)) #graph pl.figure(1) t = pl.plot(xTrain, yTrain, 'go') o = pl.plot(xTrain, oTrain, 'ro-') #pl.plot(xTrain, oTrain2, 'bo-'); #pl.legend((t, o), ("target", "predicted")); pl.ylim([-8, 5]) pl.title('Bayesian Regression train set with K=' + str(K)) Basis = basisX(xTest) N = Basis.shape[1] oTest = eval1(np.array(yTest), Basis.T, mean, N)
# evaluating train oTrain = NPolynomReg(K, xTrain, yTrain, beta, w) print "Euclidean error distance for train: " print np.linalg.norm(np.array(yTrain) - oTrain) # evaluating test oTest = NPolynomReg(K, xTest, yTest, beta, w) print "Euclidean error distance for test: " print np.linalg.norm(np.array(oTest) - yTest) # graph pl.figure(3) t = pl.plot(xTrain, yTrain, "go", label="train data") pl.plot(xTrain, oTrain2, "bo-", label="curve fit") # pl.legend((t, o), ("target", "predicted")); # pl.ylim([-8,5]); pl.title("Polynomial Regression train set with degree " + str(K - 1)) pl.figure(4) t = pl.plot(xTest, yTest, "go", label="test data") o = pl.plot(xTest, oTest, "ro", label="predicted") # pl.legend((t,o), ("target", "predicted")); pl.title("Polynomial Regression test set with degree " + str(K - 1)) if __name__ == "__main__": [X, Y, T] = dt.sample_poly3(100) [xTrain, yTrain, tTrain, xTest, yTest, tTest] = util.sepTrainTest4Reg(50, 50, X, T, Y) errTrainPoly, errTestPoly = ppR.run(xTrain, yTrain, tTrain, xTest, yTest, tTest, 4) pl.show()
o = pl.plot(xTest, oTest, 'ro', label="predicted"); #pl.legend((t,o), ("target", "predicted")); pl.title('Polynomial Regression test set with degree ' + str(K-1)); if __name__ == '__main__': K = 4; kList = [3]; errorTrain = [0]*len(kList); errorTest = [0]*len(kList); #data [X, Y, T] = dt.sample_poly3(60); [xTrain, yTrain, tTrain, xTest, yTest, tTest] = util.sepTrainTest4Reg(40,20,\ X,T,Y); oTrain, mean = bayesianCurveFitting(xTrain, np.array(yTrain)); #graph pl.figure(1); t = pl.plot(xTrain, yTrain, 'go'); o = pl.plot(xTrain, oTrain, 'ro-'); #pl.plot(xTrain, oTrain2, 'bo-'); #pl.legend((t, o), ("target", "predicted")); pl.ylim([-8,5]); pl.title('Bayesian Regression train set with K=' + str(K)); Basis = basisX(xTest); N = Basis.shape[1]; oTest = eval1(np.array(yTest), Basis.T, mean, N);