def test_regularized_linear_regression(Xtrain, ytrain, lambd): result = [] w = lr.regularized_linear_regression(Xtrain, ytrain, lambd) result.append('[TEST RegularizedLinearRegression]' + str(len(w)) + ",") result.append('[TEST RegularizedLinearRegression]' + weights_to_string(w)) return result
def test_test_error(Xtrain, ytrain, Xval, yval, lambds): result = [] bestlambd = lr.tune_lambda(Xtrain, ytrain, Xval, yval, lambds) wbest = lr.regularized_linear_regression(Xtrain, ytrain, bestlambd) mse = lr.test_error(wbest, Xtest, ytest) result.append(('[TEST TestError]%.3f,') % mse) return result
Xtrain, ytrain, Xval, yval, Xtest, ytest = data_processing_linear_regression( filename, True, False, 0) w = linear_regression_invertible(Xtrain, ytrain) print("dimensionality of the model parameter is ", w.shape, ".", sep="") print("model parameter is ", np.array_str(w)) mae = mean_absolute_error(w, Xtrain, ytrain) print("MAE on train is %.5f" % mae) mae = mean_absolute_error(w, Xval, yval) print("MAE on val is %.5f" % mae) mae = mean_absolute_error(w, Xtest, ytest) print("MAE on test is %.5f" % mae) print("\n======== Question 1.4 ========") Xtrain, ytrain, Xval, yval, Xtest, ytest = data_processing_linear_regression( filename, True, False, 0) w = regularized_linear_regression(Xtrain, ytrain, 0.1) print("dimensionality of the model parameter is ", w.shape, ".", sep="") print("model parameter is ", np.array_str(w)) mae = mean_absolute_error(w, Xtrain, ytrain) print("MAE on train is %.5f" % mae) mae = mean_absolute_error(w, Xval, yval) print("MAE on val is %.5f" % mae) mae = mean_absolute_error(w, Xtest, ytest) print("MAE on test is %.5f" % mae) print("\n======== Question 1.5========") Xtrain, ytrain, Xval, yval, Xtest, ytest = data_processing_linear_regression( filename, False, False, 0) bestlambd = tune_lambda(Xtrain, ytrain, Xval, yval) print("Best Lambda = " + str(bestlambd)) w = regularized_linear_regression(Xtrain, ytrain, bestlambd)