From 47e2044dc3584a40ba12003586f6c392a8230c89 Mon Sep 17 00:00:00 2001 From: vasnake Date: Tue, 1 Apr 2014 20:16:06 +0400 Subject: [PATCH] added: ex1 unit tests from https://class.coursera.org/ml-005/forum/thread?thread_id=334#post-5327 --- ex1/test_computeCost.m | 8 ++++++++ ex1/test_computeCostMulti.m | 8 ++++++++ ex1/test_ex1.m | 15 +++++++++++++++ ex1/test_featureNormalize.m | 24 ++++++++++++++++++++++++ ex1/test_gradientDescent.m | 12 ++++++++++++ ex1/test_gradientDescentMulti.m | 12 ++++++++++++ ex1/test_normalEqn.m | 10 ++++++++++ ex1/test_sanity.m | 4 ++++ ex1/test_warmup.m | 5 +++++ 9 files changed, 98 insertions(+) create mode 100644 ex1/test_computeCost.m create mode 100644 ex1/test_computeCostMulti.m create mode 100644 ex1/test_ex1.m create mode 100644 ex1/test_featureNormalize.m create mode 100644 ex1/test_gradientDescent.m create mode 100644 ex1/test_gradientDescentMulti.m create mode 100644 ex1/test_normalEqn.m create mode 100644 ex1/test_sanity.m create mode 100644 ex1/test_warmup.m diff --git a/ex1/test_computeCost.m b/ex1/test_computeCost.m new file mode 100644 index 0000000..a965f99 --- /dev/null +++ b/ex1/test_computeCost.m @@ -0,0 +1,8 @@ +function test_computeCost () + epsilon = 1e-4; + + x = computeCost([1 2 3; 1 3 4; 1 4 5; 1 5 6], [7;6;5;4], [0.1;0.2;0.3]); + a = 7.0175; + assert(x, a, epsilon); + +endfunction diff --git a/ex1/test_computeCostMulti.m b/ex1/test_computeCostMulti.m new file mode 100644 index 0000000..26313eb --- /dev/null +++ b/ex1/test_computeCostMulti.m @@ -0,0 +1,8 @@ +function test_computeCostMulti () + epsilon = 1e-4; + + x = computeCostMulti( [ 2 1 3; 7 1 9; 1 8 1; 3 7 4 ], [ 2; 5; 5; 6 ], [ 0.3816; 0.7655; 0.7952 ] ); + a = 6.7273; + assert(x, a, epsilon); + +endfunction diff --git a/ex1/test_ex1.m b/ex1/test_ex1.m new file mode 100644 index 0000000..709843a --- /dev/null +++ b/ex1/test_ex1.m @@ -0,0 +1,15 @@ +%!test test_sanity() + +%!test test_warmup() + +%!test test_computeCost() + +%!test test_gradientDescent + +%!test test_featureNormalize + +%!test test_computeCostMulti + +%!test test_gradientDescentMulti + +%!test test_normalEqn diff --git a/ex1/test_featureNormalize.m b/ex1/test_featureNormalize.m new file mode 100644 index 0000000..8f3ff6a --- /dev/null +++ b/ex1/test_featureNormalize.m @@ -0,0 +1,24 @@ +function test_featureNormalize () + epsilon = 1e-4; + + x = featureNormalize([1 2 3]'); + a = [-1; 0; 1]; + assert(x, a, epsilon); + + x = featureNormalize([1 2 3;6 4 2]'); + a = [-1 1; 0 0; 1 -1]; + assert(x, a, epsilon); + + x = featureNormalize( [ 8 1 6; 3 5 7; 4 9 2 ] ); + a = [1.1339 -1.0000 0.3780; -0.7559 0 0.7559; -0.3780 1.0000 -1.1339]; + assert(x, a, epsilon); + + x = featureNormalize([1 2 3 1;6 4 2 0;11 3 3 9;4 9 8 8]'); + a = [ + -0.78335 1.16190 1.09141 -1.46571; + 0.26112 0.38730 -0.84887 0.78923; + 1.30558 -0.38730 -0.84887 0.33824; + -0.78335 -1.16190 0.60634 0.33824]; + assert(x, a, epsilon); + +endfunction diff --git a/ex1/test_gradientDescent.m b/ex1/test_gradientDescent.m new file mode 100644 index 0000000..9c8e337 --- /dev/null +++ b/ex1/test_gradientDescent.m @@ -0,0 +1,12 @@ +function test_gradientDescent () + epsilon = 1e-4; + + x = gradientDescent([1 5; 1 2; 1 4; 1 5],[1 6 4 2]',[0 0]',0.01,1000); + a = [5.2148; -0.5733]; + assert(x, a, epsilon); + + x = gradientDescent([3 5; 1 2; 9 4; 1 5],[1 6 4 2]',[0 0]',0.01,1000); + a = [0.2588; 0.3999]; + assert(x, a, epsilon); + +endfunction diff --git a/ex1/test_gradientDescentMulti.m b/ex1/test_gradientDescentMulti.m new file mode 100644 index 0000000..2c796c3 --- /dev/null +++ b/ex1/test_gradientDescentMulti.m @@ -0,0 +1,12 @@ +function test_gradientDescentMulti () + epsilon = 1e-4; + + x = gradientDescentMulti([3 5 6; 1 2 3; 9 4 2],[1 6 4]',[0 0 0]',0.01,1000); + a = [ + 1.2123; + -2.9458; + 2.3219]; + + assert(x, a, epsilon); + +endfunction diff --git a/ex1/test_normalEqn.m b/ex1/test_normalEqn.m new file mode 100644 index 0000000..9cb22dc --- /dev/null +++ b/ex1/test_normalEqn.m @@ -0,0 +1,10 @@ +function test_normalEqn () + epsilon = 1e-4; + + x = normalEqn([1 0; 0 2],[1 1]'); + a = [ + 1.00000; + 0.50000]; + assert(x, a, epsilon); + +endfunction diff --git a/ex1/test_sanity.m b/ex1/test_sanity.m new file mode 100644 index 0000000..6b10a83 --- /dev/null +++ b/ex1/test_sanity.m @@ -0,0 +1,4 @@ +function test_sanity () + % make sure tests are running correctly + assert(1,1); +endfunction diff --git a/ex1/test_warmup.m b/ex1/test_warmup.m new file mode 100644 index 0000000..99d5ad0 --- /dev/null +++ b/ex1/test_warmup.m @@ -0,0 +1,5 @@ +function test_warmup () + X = warmUpExercise(); + A = eye(5); + assert(X, A) +endfunction