def newtonRaphson(inputFiles): pol = PolynomialFeatures(2) errors = [] for File in inputFiles: data = tools.readData(File) X = data[:, :-1] Y = data[:, -1] kf = KFold(len(Y), n_folds = 10) trainError = 0 testError = 0 for train, test in kf: Z = pol.fit_transform(X[train]) row, col = Z.shape theta = np.empty(col, dtype='float') meanDiff = 1.0 i = 1 #print "Theta iteration %s: \n%s" % ('0', str(theta)) while abs(meanDiff) > 1.0e-15 : theta_new = recalculateTheta(theta, Z, Y[train]) diff = np.subtract(theta_new, theta) meanDiff = np.mean(diff) #print "Theta iteration %s: \n%s" % (str(i), str(theta_new)) #print "Diff: %s" % str(meanDiff) theta = theta_new i += 1 Z_test = pol.fit_transform(X[test]) Y_hat_test = np.dot(Z_test, theta) Y_hat = np.dot(Z, theta) trainError += tools.findError(Y_hat, Y[train]) testError += tools.findError(Y_hat_test, Y[test]) trainError = trainError/len(kf) testError = testError/len(kf) iterative_error = [trainError, testError] errors. append(iterative_error) return np.asarray(errors)
def newtonRaphson(inputFiles): pol = PolynomialFeatures(2) errors = [] for File in inputFiles: data = tools.readData(File) X = data[:, :-1] Y = data[:, -1] kf = KFold(len(Y), n_folds=10) trainError = 0 testError = 0 for train, test in kf: Z = pol.fit_transform(X[train]) row, col = Z.shape theta = np.empty(col, dtype='float') meanDiff = 1.0 i = 1 #print "Theta iteration %s: \n%s" % ('0', str(theta)) while abs(meanDiff) > 1.0e-15: theta_new = recalculateTheta(theta, Z, Y[train]) diff = np.subtract(theta_new, theta) meanDiff = np.mean(diff) #print "Theta iteration %s: \n%s" % (str(i), str(theta_new)) #print "Diff: %s" % str(meanDiff) theta = theta_new i += 1 Z_test = pol.fit_transform(X[test]) Y_hat_test = np.dot(Z_test, theta) Y_hat = np.dot(Z, theta) trainError += tools.findError(Y_hat, Y[train]) testError += tools.findError(Y_hat_test, Y[test]) trainError = trainError / len(kf) testError = testError / len(kf) iterative_error = [trainError, testError] errors.append(iterative_error) return np.asarray(errors)
def dualProblem(inputFiles): errors = [] start_time = time.time() for File in inputFiles: data = tools.readData(File) X = data[:, :-1] Y = data[:-1] kf = KFold(len(Y), n_folds = 10) trainError = 0 testError = 0 for train, test in kf: G = gaussian_function(X[train], 0.05) print "Done with G!" alpha = computeAlpha(G, Y[train], 0.05) theta = computeTheta(alpha, X[train]) Y_hat = np.dot(X[train], theta) Y_hat_test = np.dot(X[test], theta) trainError += tools.findError(Y_hat, Y[train]) testError += tools.findError(Y_hat_test, Y[test]) trainError = trainError/len(kf) testError = testError/len(kf) error = [trainError, testError] errors.append(error) time_taken = start_time - time.time() print "Time Taken for all data sets: %s" % str(time_taken) return np.asarray(errors)
def dualProblem(inputFiles): errors = [] start_time = time.time() for File in inputFiles: data = tools.readData(File) X = data[:, :-1] Y = data[:-1] kf = KFold(len(Y), n_folds=10) trainError = 0 testError = 0 for train, test in kf: G = gaussian_function(X[train], 0.05) print "Done with G!" alpha = computeAlpha(G, Y[train], 0.05) theta = computeTheta(alpha, X[train]) Y_hat = np.dot(X[train], theta) Y_hat_test = np.dot(X[test], theta) trainError += tools.findError(Y_hat, Y[train]) testError += tools.findError(Y_hat_test, Y[test]) trainError = trainError / len(kf) testError = testError / len(kf) error = [trainError, testError] errors.append(error) time_taken = start_time - time.time() print "Time Taken for all data sets: %s" % str(time_taken) return np.asarray(errors)
def linearRegressionKFold(inputFiles, i=1): print "\nSingle Variable, Degree: %s" % i print "###########################" for File in inputFiles: print "===========================" print "Data Set %s" % File data = tools.readData(File) X = data[:, 0] Y = data[:, 1] kf = KFold(len(data), n_folds=10, shuffle=True) TrainError = 0 TestError = 0 for train, test in kf: Z = tools.createZ(X[train], i) theta = regress(Z, Y[train]) Y_hat = YHat(theta, X[train]) Y_hat_test = YHat(theta, X[test]) TrainError = TrainError + tools.findError(theta, Y[train]) TestError = TestError + tools.findError(theta, Y[test]) TestError = TestError / len(kf) TrainError = TrainError / len(kf) print "---------------------------" print "Test Error: %s" % TestError print "Train Error: %s" % TrainError py_linearRegression(X, Y) return TestError
def py_linearRegression(X, Y): regr = linear_model.LinearRegression(fit_intercept=False) kf = KFold(len(X), n_folds=10, shuffle=True) py_trainError=0 py_testError=0 for train, test in kf: regr.fit(tools.transposeHelper(X[train]), Y[train]) py_trainError += tools.findError(regr.predict( tools.transposeHelper(X[train])), Y[train]) py_testError += tools.findError( regr.predict( tools.transposeHelper(X[test])), Y[test]) py_testError /= len(kf) py_trainError /= len(kf) print "---------------------------------" print "Python Functions:\n" print "Test Error: %s" % py_testError print "Train Error: %s" % py_trainError
def polyRegression(inputFiles): pol = PolynomialFeatures(2) errors = [] for Files in inputFiles: data = tools.readData(Files) data = data[np.argsort(data[:, 0])] X = data[:, :-1] Y = data[:, -1] kf = KFold(len(Y), n_folds = 10) trainError = 0 testError = 0 for train, test in kf: Z = pol.fit_transform(X[train]) theta = regress(Z, Y[train]) Y_hat = np.dot(Z, theta) Z_test = pol.fit_transform(X[test]) Y_hat_test = np.dot(Z_test, theta) trainError += tools.findError(Y_hat, Y[train]) testError += tools.findError(Y_hat_test, Y[test]) testError = testError/len(kf) trainError = trainError/len(kf) explicit_error = [trainError, testError] errors.append(explicit_error) return np.asarray(errors)
def polyRegression(inputFiles): pol = PolynomialFeatures(2) errors = [] for Files in inputFiles: data = tools.readData(Files) data = data[np.argsort(data[:, 0])] X = data[:, :-1] Y = data[:, -1] kf = KFold(len(Y), n_folds=10) trainError = 0 testError = 0 for train, test in kf: Z = pol.fit_transform(X[train]) theta = regress(Z, Y[train]) Y_hat = np.dot(Z, theta) Z_test = pol.fit_transform(X[test]) Y_hat_test = np.dot(Z_test, theta) trainError += tools.findError(Y_hat, Y[train]) testError += tools.findError(Y_hat_test, Y[test]) testError = testError / len(kf) trainError = trainError / len(kf) explicit_error = [trainError, testError] errors.append(explicit_error) return np.asarray(errors)