From 16a994e54ec81018c46e09004c9135af104bd569 Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Tue, 8 Nov 2011 17:01:32 +0100 Subject: Adjusted for population --- transform.rb | 58 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index a00f87d..7642cf0 100644 --- a/transform.rb +++ b/transform.rb @@ -22,34 +22,34 @@ def test_mlr } end -# def test_pca -# -# d = GSL::Matrix.alloc([1,1.1,2,1.9,3,3.3], 3, 2) -# td = GSL::Matrix.alloc([-1.3421074161875, -0.127000127000191, 1.46910754318769],3,1) -# ev = GSL::Matrix.alloc([0.707106781186548, 0.707106781186548], 2, 1) -# rd = GSL::Matrix.alloc([1.05098674493306, 1.043223563717, 1.91019734898661, 2.0, 3.03881590608033, 3.256776436283], 3, 2) -# -# # Lossy -# 2.times do # repeat to ensure idempotency -# pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.05) -# assert_equal pca.data_matrix, d -# assert_equal pca.data_transformed_matrix, td -# assert_equal pca.eigenvector_matrix, ev -# assert_equal pca.restore, rd -# end -# -# td = GSL::Matrix.alloc([-1.3421074161875, 0.0721061461855949, -0.127000127000191, -0.127000127000191, 1.46910754318769, 0.0548939808145955],3,2) -# ev = GSL::Matrix.alloc([0.707106781186548, -0.707106781186548, 0.707106781186548, 0.707106781186548], 2, 2) -# -# # Lossless -# 2.times do -# pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.0) -# assert_equal pca.data_matrix, d -# assert_equal pca.data_transformed_matrix, td -# assert_equal pca.eigenvector_matrix, ev -# assert_equal pca.restore, d -# end -# -# end +def test_pca + + d = GSL::Matrix.alloc([1,1.1,2,1.9,3,3.3], 3, 2) + td = GSL::Matrix.alloc([-1.64373917483226, -0.155542754209564, 1.79928192904182],3,1) + ev = GSL::Matrix.alloc([0.707106781186548, 0.707106781186548], 2, 1) + rd = GSL::Matrix.alloc([1.05098674493306, 1.043223563717, 1.91019734898661, 2.0, 3.03881590608033, 3.256776436283], 3, 2) + + # Lossy + 2.times do # repeat to ensure idempotency + pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.05) + assert_equal pca.data_matrix, d + assert_equal pca.data_transformed_matrix, td + assert_equal pca.eigenvector_matrix, ev + assert_equal pca.restore, rd + end + + td = GSL::Matrix.alloc([-1.64373917483226, 0.0883116327366195, -0.155542754209564, -0.155542754209564, 1.79928192904182, 0.0672311214729441],3,2) + ev = GSL::Matrix.alloc([0.707106781186548, -0.707106781186548, 0.707106781186548, 0.707106781186548], 2, 2) + + # Lossless + 2.times do + pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.0) + assert_equal pca.data_matrix, d + assert_equal pca.data_transformed_matrix, td + assert_equal pca.eigenvector_matrix, ev + assert_equal pca.restore, d + end + +end end -- cgit v1.2.3 From 8cfb409e1e3431441f1094c53a1950f680320b6a Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Wed, 9 Nov 2011 11:37:23 +0100 Subject: Removed MLR test temp --- transform.rb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index 7642cf0..dee511c 100644 --- a/transform.rb +++ b/transform.rb @@ -5,22 +5,22 @@ require 'test/unit' class TransformTest < Test::Unit::TestCase -def test_mlr - 2.times { - n_prop = [ [1,1], [2,2], [3,3] ] # erste WH - acts = [ 3,2,3 ] # should yield a constant y=2.8 - sims = [ 4,2,4 ] # move constant closer to 3.0 - q_prop = [0.5,0.5] # extrapolation - params={:n_prop => n_prop, :q_prop => q_prop, :sims => sims, :acts => acts} - - prediction = OpenTox::Algorithm::Neighbors.mlr(params) - assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why - - q_prop = [1.5,1.5] # interpolation - prediction = OpenTox::Algorithm::Neighbors.mlr(params) - assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why - } -end +#def test_mlr +# 2.times { +# n_prop = [ [1,1], [2,2], [3,3] ] # erste WH +# acts = [ 3,2,3 ] # should yield a constant y=2.8 +# sims = [ 4,2,4 ] # move constant closer to 3.0 +# q_prop = [0.5,0.5] # extrapolation +# params={:n_prop => n_prop, :q_prop => q_prop, :sims => sims, :acts => acts} +# +# prediction = OpenTox::Algorithm::Neighbors.mlr(params) +# assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why +# +# q_prop = [1.5,1.5] # interpolation +# prediction = OpenTox::Algorithm::Neighbors.mlr(params) +# assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why +# } +#end def test_pca @@ -34,6 +34,7 @@ def test_pca pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.05) assert_equal pca.data_matrix, d assert_equal pca.data_transformed_matrix, td + assert_equal pca.transform(d), td assert_equal pca.eigenvector_matrix, ev assert_equal pca.restore, rd end @@ -46,6 +47,7 @@ def test_pca pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.0) assert_equal pca.data_matrix, d assert_equal pca.data_transformed_matrix, td + assert_equal pca.transform(d), td assert_equal pca.eigenvector_matrix, ev assert_equal pca.restore, d end -- cgit v1.2.3 From 4f5f15ae60a0d98c419e67f86a7dc161721ded14 Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Wed, 9 Nov 2011 13:50:18 +0100 Subject: Added tests for LogAutoScaler --- transform.rb | 142 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 97 insertions(+), 45 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index dee511c..049909c 100644 --- a/transform.rb +++ b/transform.rb @@ -5,53 +5,105 @@ require 'test/unit' class TransformTest < Test::Unit::TestCase -#def test_mlr -# 2.times { -# n_prop = [ [1,1], [2,2], [3,3] ] # erste WH -# acts = [ 3,2,3 ] # should yield a constant y=2.8 -# sims = [ 4,2,4 ] # move constant closer to 3.0 -# q_prop = [0.5,0.5] # extrapolation -# params={:n_prop => n_prop, :q_prop => q_prop, :sims => sims, :acts => acts} -# -# prediction = OpenTox::Algorithm::Neighbors.mlr(params) -# assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why -# -# q_prop = [1.5,1.5] # interpolation -# prediction = OpenTox::Algorithm::Neighbors.mlr(params) -# assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why -# } -#end - -def test_pca - - d = GSL::Matrix.alloc([1,1.1,2,1.9,3,3.3], 3, 2) - td = GSL::Matrix.alloc([-1.64373917483226, -0.155542754209564, 1.79928192904182],3,1) - ev = GSL::Matrix.alloc([0.707106781186548, 0.707106781186548], 2, 1) - rd = GSL::Matrix.alloc([1.05098674493306, 1.043223563717, 1.91019734898661, 2.0, 3.03881590608033, 3.256776436283], 3, 2) - - # Lossy - 2.times do # repeat to ensure idempotency - pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.05) - assert_equal pca.data_matrix, d - assert_equal pca.data_transformed_matrix, td - assert_equal pca.transform(d), td - assert_equal pca.eigenvector_matrix, ev - assert_equal pca.restore, rd + #def test_mlr + # 2.times { + # n_prop = [ [1,1], [2,2], [3,3] ] # erste WH + # acts = [ 3,2,3 ] # should yield a constant y=2.8 + # sims = [ 4,2,4 ] # move constant closer to 3.0 + # q_prop = [0.5,0.5] # extrapolation + # params={:n_prop => n_prop, :q_prop => q_prop, :sims => sims, :acts => acts} + # + # prediction = OpenTox::Algorithm::Neighbors.mlr(params) + # assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why + # + # q_prop = [1.5,1.5] # interpolation + # prediction = OpenTox::Algorithm::Neighbors.mlr(params) + # assert_in_delta prediction, 2.8, 1.0E-10 # small deviations, don't know why + # } + #end + + def test_pca + + d = GSL::Matrix.alloc([1,1.1,2,1.9,3,3.3], 3, 2) + td = GSL::Matrix.alloc([-1.64373917483226, -0.155542754209564, 1.79928192904182],3,1) + ev = GSL::Matrix.alloc([0.707106781186548, 0.707106781186548], 2, 1) + rd = GSL::Matrix.alloc([1.05098674493306, 1.043223563717, 1.91019734898661, 2.0, 3.03881590608033, 3.256776436283], 3, 2) + + # Lossy + 2.times do # repeat to ensure idempotency + pca = OpenTox::Transform::PCA.new(d, 0.05) + assert_equal pca.data_matrix, d + assert_equal pca.data_transformed_matrix, td + assert_equal pca.transform(d), td + assert_equal pca.eigenvector_matrix, ev + assert_equal pca.restore, rd + end + + td = GSL::Matrix.alloc([-1.64373917483226, 0.0883116327366195, -0.155542754209564, -0.155542754209564, 1.79928192904182, 0.0672311214729441],3,2) + ev = GSL::Matrix.alloc([0.707106781186548, -0.707106781186548, 0.707106781186548, 0.707106781186548], 2, 2) + + # Lossless + 2.times do + pca = OpenTox::Transform::PCA.new(d, 0.0) + assert_equal pca.data_matrix, d + assert_equal pca.data_transformed_matrix, td + assert_equal pca.transform(d), td + assert_equal pca.eigenvector_matrix, ev + assert_equal pca.restore, d + end + end + + def test_logas + + d1 = [ 1,2,3 ] + d2 = [ -1,0,1 ] + d3 = [ -2,3,8 ] + d4 = [ -20,30,80 ] - td = GSL::Matrix.alloc([-1.64373917483226, 0.0883116327366195, -0.155542754209564, -0.155542754209564, 1.79928192904182, 0.0672311214729441],3,2) - ev = GSL::Matrix.alloc([0.707106781186548, -0.707106781186548, 0.707106781186548, 0.707106781186548], 2, 2) - - # Lossless - 2.times do - pca = OpenTox::Algorithm::Transform::PCA.new(d, 0.0) - assert_equal pca.data_matrix, d - assert_equal pca.data_transformed_matrix, td - assert_equal pca.transform(d), td - assert_equal pca.eigenvector_matrix, ev - assert_equal pca.restore, d - end -end + 2.times { + + logas = OpenTox::Transform::LogAutoScale.new(d1) + d1la = logas.vs + d1la.each_with_index { |v,i| + assert_in_delta v, [ -1.31668596949013, 0.211405021140643, 1.10528094834949 ][i], 1.0E-10 + } + assert_equal logas.transform(d1), d1la + logas.restore(d1la).each_with_index { |v,i| + assert_in_delta v, d1[i], 1.0E-10 + } + + logas = OpenTox::Transform::LogAutoScale.new(d2) + d2la = logas.vs + assert_equal d2la, d1la + assert_equal logas.transform(d2), d2la + logas.restore(d2la).each_with_index { |v,i| + assert_in_delta v, d2[i], 1.0E-10 + } + + logas = OpenTox::Transform::LogAutoScale.new(d3) + d3la = logas.vs + d3la.each_with_index { |v,i| + assert_in_delta v, [ -1.37180016053906, 0.388203523926062, 0.983596636612997 ][i], 1.0E-10 + } + assert_equal logas.transform(d3), d3la + logas.restore(d3la).each_with_index { |v,i| + assert_in_delta v, d3[i], 1.0E-10 + } + + logas = OpenTox::Transform::LogAutoScale.new(d4) + d4la = logas.vs + d4la.each_with_index { |v,i| + assert_in_delta v, [ -1.40084731572532, 0.532435269814955, 0.868412045910369 ][i], 1.0E-10 + } + assert_equal logas.transform(d4), d4la + logas.restore(d4la).each_with_index { |v,i| + assert_in_delta v, d4[i], 1.0E-10 + } + + } + + end end -- cgit v1.2.3 From 3845d0407b591599378c92a419734cc252f8954b Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Wed, 9 Nov 2011 16:54:28 +0100 Subject: Transform test now uses GSL --- transform.rb | 58 ++++++++++++++++++++++------------------------------------ 1 file changed, 22 insertions(+), 36 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index 049909c..15d1f09 100644 --- a/transform.rb +++ b/transform.rb @@ -56,52 +56,38 @@ class TransformTest < Test::Unit::TestCase def test_logas - d1 = [ 1,2,3 ] - d2 = [ -1,0,1 ] - d3 = [ -2,3,8 ] - d4 = [ -20,30,80 ] + d1 = [ 1,2,3 ].to_gv + d2 = [ -1,0,1 ].to_gv + d3 = [ -2,3,8 ].to_gv + d4 = [ -20,30,80 ].to_gv + d1la = [ -1.31668596949013, 0.211405021140643, 1.10528094834949 ].to_gv + d2la = d1la + d3la = [ -1.37180016053906, 0.388203523926062, 0.983596636612997 ].to_gv + d4la = [ -1.40084731572532, 0.532435269814955, 0.868412045910369 ].to_gv 2.times { logas = OpenTox::Transform::LogAutoScale.new(d1) - d1la = logas.vs - d1la.each_with_index { |v,i| - assert_in_delta v, [ -1.31668596949013, 0.211405021140643, 1.10528094834949 ][i], 1.0E-10 - } - assert_equal logas.transform(d1), d1la - logas.restore(d1la).each_with_index { |v,i| - assert_in_delta v, d1[i], 1.0E-10 - } + assert_equal logas.vs, d1la + assert_equal logas.transform(d1), logas.vs + assert_equal logas.restore(logas.vs), d1 logas = OpenTox::Transform::LogAutoScale.new(d2) - d2la = logas.vs - assert_equal d2la, d1la + assert_equal logas.vs, d2la assert_equal logas.transform(d2), d2la - logas.restore(d2la).each_with_index { |v,i| - assert_in_delta v, d2[i], 1.0E-10 - } - + assert_equal logas.restore(logas.vs), d2 + logas = OpenTox::Transform::LogAutoScale.new(d3) - d3la = logas.vs - d3la.each_with_index { |v,i| - assert_in_delta v, [ -1.37180016053906, 0.388203523926062, 0.983596636612997 ][i], 1.0E-10 - } - assert_equal logas.transform(d3), d3la - logas.restore(d3la).each_with_index { |v,i| - assert_in_delta v, d3[i], 1.0E-10 - } - + assert_equal logas.vs, d3la + assert_equal logas.transform(d3), logas.vs + assert_equal logas.restore(logas.vs), d3 + logas = OpenTox::Transform::LogAutoScale.new(d4) - d4la = logas.vs - d4la.each_with_index { |v,i| - assert_in_delta v, [ -1.40084731572532, 0.532435269814955, 0.868412045910369 ][i], 1.0E-10 - } - assert_equal logas.transform(d4), d4la - logas.restore(d4la).each_with_index { |v,i| - assert_in_delta v, d4[i], 1.0E-10 - } - + assert_equal logas.vs, d4la + assert_equal logas.transform(d4), logas.vs + assert_equal logas.restore(logas.vs), d4 + } end -- cgit v1.2.3 From b25be2d6cbcf880fe2afe83143a7d47227dcf0e0 Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Thu, 10 Nov 2011 09:20:23 +0100 Subject: PCA: fixed scaling --- transform.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index 15d1f09..3c3b2da 100644 --- a/transform.rb +++ b/transform.rb @@ -24,10 +24,11 @@ class TransformTest < Test::Unit::TestCase def test_pca - d = GSL::Matrix.alloc([1,1.1,2,1.9,3,3.3], 3, 2) - td = GSL::Matrix.alloc([-1.64373917483226, -0.155542754209564, 1.79928192904182],3,1) + d = GSL::Matrix.alloc([1.0, -5, 1.1, 2.0, -5, 1.9, 3.0, -5, 3.3], 3, 3) # 2nd col is const -5, gets removed + rd = GSL::Matrix.alloc([1.0, 1.1, 1.9, 2.0, 3.1, 3.2], 3, 2) + + td = GSL::Matrix.alloc([-1.4142135623731, -0.14142135623731, 1.5556349186104],3,1) ev = GSL::Matrix.alloc([0.707106781186548, 0.707106781186548], 2, 1) - rd = GSL::Matrix.alloc([1.05098674493306, 1.043223563717, 1.91019734898661, 2.0, 3.03881590608033, 3.256776436283], 3, 2) # Lossy 2.times do # repeat to ensure idempotency @@ -39,7 +40,8 @@ class TransformTest < Test::Unit::TestCase assert_equal pca.restore, rd end - td = GSL::Matrix.alloc([-1.64373917483226, 0.0883116327366195, -0.155542754209564, -0.155542754209564, 1.79928192904182, 0.0672311214729441],3,2) + rd = GSL::Matrix.alloc([1.0, 1.1, 2.0, 1.9, 3.0, 3.3], 3, 2) # 2nd col of d is const -5, gets removed on rd + td = GSL::Matrix.alloc([-1.4142135623731, -7.84962372879505e-17, -0.14142135623731, -0.14142135623731, 1.5556349186104, 0.141421356237309],3,2) ev = GSL::Matrix.alloc([0.707106781186548, -0.707106781186548, 0.707106781186548, 0.707106781186548], 2, 2) # Lossless @@ -49,7 +51,7 @@ class TransformTest < Test::Unit::TestCase assert_equal pca.data_transformed_matrix, td assert_equal pca.transform(d), td assert_equal pca.eigenvector_matrix, ev - assert_equal pca.restore, d + assert_equal pca.restore, rd end end -- cgit v1.2.3 From 6a123c29067bd741ac6bac87a2154ac888b6f677 Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Thu, 10 Nov 2011 14:44:54 +0100 Subject: Testing maxcols constraint on PCA --- transform.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index 3c3b2da..5b2bd48 100644 --- a/transform.rb +++ b/transform.rb @@ -26,7 +26,6 @@ class TransformTest < Test::Unit::TestCase d = GSL::Matrix.alloc([1.0, -5, 1.1, 2.0, -5, 1.9, 3.0, -5, 3.3], 3, 3) # 2nd col is const -5, gets removed rd = GSL::Matrix.alloc([1.0, 1.1, 1.9, 2.0, 3.1, 3.2], 3, 2) - td = GSL::Matrix.alloc([-1.4142135623731, -0.14142135623731, 1.5556349186104],3,1) ev = GSL::Matrix.alloc([0.707106781186548, 0.707106781186548], 2, 1) @@ -53,6 +52,20 @@ class TransformTest < Test::Unit::TestCase assert_equal pca.eigenvector_matrix, ev assert_equal pca.restore, rd end + + rd = GSL::Matrix.alloc([1.0, 1.1, 1.9, 2.0, 3.1, 3.2], 3, 2) + td = GSL::Matrix.alloc([-1.4142135623731, -0.14142135623731, 1.5556349186104],3,1) + ev = GSL::Matrix.alloc([0.707106781186548, 0.707106781186548], 2, 1) + # Lossy, but using maxcols constraint + 2.times do + pca = OpenTox::Transform::PCA.new(d, 0.0, 1) # 1 column + assert_equal pca.data_matrix, d + assert_equal pca.data_transformed_matrix, td + assert_equal pca.transform(d), td + assert_equal pca.eigenvector_matrix, ev + assert_equal pca.restore, rd + end + end @@ -62,11 +75,13 @@ class TransformTest < Test::Unit::TestCase d2 = [ -1,0,1 ].to_gv d3 = [ -2,3,8 ].to_gv d4 = [ -20,30,80 ].to_gv + d5 = [ 0.707, 0.7071].to_gv d1la = [ -1.31668596949013, 0.211405021140643, 1.10528094834949 ].to_gv d2la = d1la d3la = [ -1.37180016053906, 0.388203523926062, 0.983596636612997 ].to_gv d4la = [ -1.40084731572532, 0.532435269814955, 0.868412045910369 ].to_gv + d5la = [ -1.0, 1.0 ].to_gv 2.times { @@ -89,6 +104,11 @@ class TransformTest < Test::Unit::TestCase assert_equal logas.vs, d4la assert_equal logas.transform(d4), logas.vs assert_equal logas.restore(logas.vs), d4 + + logas = OpenTox::Transform::LogAutoScale.new(d5) + assert_equal logas.vs, d5la + assert_equal logas.transform(d5), logas.vs + assert_equal logas.restore(logas.vs), d5 } -- cgit v1.2.3 From ad68034eb14e49adb41910502f1cb6cbac0b0afd Mon Sep 17 00:00:00 2001 From: davor Date: Mon, 19 Dec 2011 14:43:24 +0100 Subject: Added SVD test fixed logas transform --- transform.rb | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index 5b2bd48..73b803c 100644 --- a/transform.rb +++ b/transform.rb @@ -2,6 +2,11 @@ require 'rubygems' require 'opentox-ruby' require 'test/unit' +class Float + def round_to(x) + (self * 10**x).round.to_f / 10**x + end +end class TransformTest < Test::Unit::TestCase @@ -68,6 +73,42 @@ class TransformTest < Test::Unit::TestCase end + + def test_svd + + m = GSL::Matrix[ + [5,5,0,5], + [5,0,3,4], + [3,4,0,3], + [0,0,5,3], + [5,4,4,5], + [5,4,5,5] + ] + svd = OpenTox::Algorithm::Transform::SVD.new m + + foo = svd.transform_feature GSL::Matrix[[5,5,3,0,5,5]] + sim = [] + svd.vk.each_row { |x| + sim << OpenTox::Algorithm::Similarity.cosine_num(x,foo.row(0)) + } + assert_equal sim[0].round_to(3), 1.000 + assert_equal sim[1].round_to(3), 0.874 + assert_equal sim[2].round_to(3), 0.064 + assert_equal sim[3].round_to(3), 0.895 + + bar = svd.transform_instance GSL::Matrix[[5,4,5,5]] + sim = [] + svd.uk.each_row { |x| + sim << OpenTox::Algorithm::Similarity.cosine_num(x,bar.row(0)) + } + + assert_equal sim[0].round_to(3), 0.346 + assert_equal sim[1].round_to(3), 0.966 + assert_equal sim[2].round_to(3), 0.282 + assert_equal sim[3].round_to(3), 0.599 + assert_equal sim[4].round_to(3), 0.975 + assert_equal sim[5].round_to(3), 1.000 + end def test_logas @@ -87,27 +128,22 @@ class TransformTest < Test::Unit::TestCase logas = OpenTox::Transform::LogAutoScale.new(d1) assert_equal logas.vs, d1la - assert_equal logas.transform(d1), logas.vs assert_equal logas.restore(logas.vs), d1 logas = OpenTox::Transform::LogAutoScale.new(d2) assert_equal logas.vs, d2la - assert_equal logas.transform(d2), d2la assert_equal logas.restore(logas.vs), d2 logas = OpenTox::Transform::LogAutoScale.new(d3) assert_equal logas.vs, d3la - assert_equal logas.transform(d3), logas.vs assert_equal logas.restore(logas.vs), d3 logas = OpenTox::Transform::LogAutoScale.new(d4) assert_equal logas.vs, d4la - assert_equal logas.transform(d4), logas.vs assert_equal logas.restore(logas.vs), d4 logas = OpenTox::Transform::LogAutoScale.new(d5) assert_equal logas.vs, d5la - assert_equal logas.transform(d5), logas.vs assert_equal logas.restore(logas.vs), d5 } -- cgit v1.2.3 From c9ea121a7f6cce3643b38ffe551ca29d9a4b8e18 Mon Sep 17 00:00:00 2001 From: davor Date: Mon, 19 Dec 2011 16:53:59 +0100 Subject: Added Scaling --- transform.rb | 81 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 17 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index 73b803c..cfedd2b 100644 --- a/transform.rb +++ b/transform.rb @@ -76,38 +76,85 @@ class TransformTest < Test::Unit::TestCase def test_svd - m = GSL::Matrix[ - [5,5,0,5], - [5,0,3,4], - [3,4,0,3], - [0,0,5,3], - [5,4,4,5], - [5,4,5,5] + m = GSL::Matrix[ + [5,5,0,5], + [5,0,3,4], + [3,4,0,3], + [0,0,5,3], + [5,4,4,5], + [5,4,5,5] ] + foo = GSL::Matrix[[5,5,3,0,5,5]] + bar = GSL::Matrix[[5,4,5,5]] + + #puts + #puts m.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + #puts + #puts foo.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + #puts + #puts bar.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + + # AutoScale to improve on representation + nr_cases, nr_features = m.size1, m.size2 + (0..nr_features-1).each { |i| + autoscaler = OpenTox::Transform::AutoScale.new(m.col(i)) + m.col(i)[0..nr_cases-1] = autoscaler.vs + bar.col(i)[0..0] = autoscaler.transform bar.col(i) + } + autoscaler = OpenTox::Transform::AutoScale.new(foo.transpose.col(0)) + foo = GSL::Matrix[autoscaler.vs] + + #puts + #puts m.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + #puts + #puts foo.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + #puts + #puts bar.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + + # run SVD svd = OpenTox::Algorithm::Transform::SVD.new m - foo = svd.transform_feature GSL::Matrix[[5,5,3,0,5,5]] + foo = svd.transform_feature foo sim = [] svd.vk.each_row { |x| sim << OpenTox::Algorithm::Similarity.cosine_num(x,foo.row(0)) } + + # # # NO AUTOSCALE + #assert_equal sim[0].round_to(3), 1.000 + #assert_equal sim[1].round_to(3), 0.874 + #assert_equal sim[2].round_to(3), 0.064 + #assert_equal sim[3].round_to(3), 0.895 + + # # # AUTOSCALE assert_equal sim[0].round_to(3), 1.000 - assert_equal sim[1].round_to(3), 0.874 - assert_equal sim[2].round_to(3), 0.064 - assert_equal sim[3].round_to(3), 0.895 + assert_equal sim[1].round_to(3), 0.705 + assert_equal sim[2].round_to(3), 0.023 + assert_equal sim[3].round_to(3), 0.934 + - bar = svd.transform_instance GSL::Matrix[[5,4,5,5]] + bar = svd.transform_instance bar sim = [] svd.uk.each_row { |x| sim << OpenTox::Algorithm::Similarity.cosine_num(x,bar.row(0)) } - assert_equal sim[0].round_to(3), 0.346 - assert_equal sim[1].round_to(3), 0.966 - assert_equal sim[2].round_to(3), 0.282 - assert_equal sim[3].round_to(3), 0.599 - assert_equal sim[4].round_to(3), 0.975 + # # # NO AUTOSCALE + #assert_equal sim[0].round_to(3), 0.346 + #assert_equal sim[1].round_to(3), 0.966 + #assert_equal sim[2].round_to(3), 0.282 + #assert_equal sim[3].round_to(3), 0.599 + #assert_equal sim[4].round_to(3), 0.975 + #assert_equal sim[5].round_to(3), 1.000 + + # # # AUTOSCALE + assert_equal sim[0].round_to(3), -0.115 + assert_equal sim[1].round_to(3), 0.425 + assert_equal sim[2].round_to(3), -0.931 + assert_equal sim[3].round_to(3), -0.352 + assert_equal sim[4].round_to(3), 0.972 assert_equal sim[5].round_to(3), 1.000 + end def test_logas -- cgit v1.2.3 From 21954fd5831e14b274c071fde7251974b298a0b7 Mon Sep 17 00:00:00 2001 From: davor Date: Tue, 20 Dec 2011 15:05:29 +0100 Subject: More tests for SVD --- transform.rb | 58 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index cfedd2b..0ac7bf7 100644 --- a/transform.rb +++ b/transform.rb @@ -84,17 +84,14 @@ class TransformTest < Test::Unit::TestCase [5,4,4,5], [5,4,5,5] ] + + foo = GSL::Matrix[[5,5,3,0,5,5]] bar = GSL::Matrix[[5,4,5,5]] - #puts - #puts m.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") - #puts - #puts foo.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") - #puts - #puts bar.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") - # AutoScale to improve on representation + + # AutoScale (mean and center) to improve on representation nr_cases, nr_features = m.size1, m.size2 (0..nr_features-1).each { |i| autoscaler = OpenTox::Transform::AutoScale.new(m.col(i)) @@ -104,6 +101,7 @@ class TransformTest < Test::Unit::TestCase autoscaler = OpenTox::Transform::AutoScale.new(foo.transpose.col(0)) foo = GSL::Matrix[autoscaler.vs] + #puts #puts m.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") #puts @@ -111,29 +109,14 @@ class TransformTest < Test::Unit::TestCase #puts #puts bar.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") - # run SVD - svd = OpenTox::Algorithm::Transform::SVD.new m - - foo = svd.transform_feature foo - sim = [] - svd.vk.each_row { |x| - sim << OpenTox::Algorithm::Similarity.cosine_num(x,foo.row(0)) - } - # # # NO AUTOSCALE - #assert_equal sim[0].round_to(3), 1.000 - #assert_equal sim[1].round_to(3), 0.874 - #assert_equal sim[2].round_to(3), 0.064 - #assert_equal sim[3].round_to(3), 0.895 - # # # AUTOSCALE - assert_equal sim[0].round_to(3), 1.000 - assert_equal sim[1].round_to(3), 0.705 - assert_equal sim[2].round_to(3), 0.023 - assert_equal sim[3].round_to(3), 0.934 + # run SVD + svd = OpenTox::Algorithm::Transform::SVD.new m - bar = svd.transform_instance bar + # instance transform + bar = svd.transform bar # alias for svd.transform_instance bar sim = [] svd.uk.each_row { |x| sim << OpenTox::Algorithm::Similarity.cosine_num(x,bar.row(0)) @@ -154,7 +137,30 @@ class TransformTest < Test::Unit::TestCase assert_equal sim[3].round_to(3), -0.352 assert_equal sim[4].round_to(3), 0.972 assert_equal sim[5].round_to(3), 1.000 + + #puts + #puts svd.restore.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + + # feature transform, only for demonstration of concept + foo = svd.transform_feature foo + sim = [] + svd.vk.each_row { |x| + sim << OpenTox::Algorithm::Similarity.cosine_num(x,foo.row(0)) + } + + # # # NO AUTOSCALE + #assert_equal sim[0].round_to(3), 1.000 + #assert_equal sim[1].round_to(3), 0.874 + #assert_equal sim[2].round_to(3), 0.064 + #assert_equal sim[3].round_to(3), 0.895 + + # # # AUTOSCALE + assert_equal sim[0].round_to(3), 1.000 + assert_equal sim[1].round_to(3), 0.705 + assert_equal sim[2].round_to(3), 0.023 + assert_equal sim[3].round_to(3), 0.934 + end def test_logas -- cgit v1.2.3 From 196f5292da023ee6cbc9f1e833a8236e26b8b7c7 Mon Sep 17 00:00:00 2001 From: davor Date: Fri, 13 Jan 2012 14:17:12 +0100 Subject: Fixed SVD test --- transform.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'transform.rb') diff --git a/transform.rb b/transform.rb index 0ac7bf7..5754393 100644 --- a/transform.rb +++ b/transform.rb @@ -112,16 +112,21 @@ class TransformTest < Test::Unit::TestCase # run SVD - svd = OpenTox::Algorithm::Transform::SVD.new m + svd = OpenTox::Algorithm::Transform::SVD.new m, 0.2 + #puts + #puts svd.restore.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + #puts + #puts svd.data_transformed_matrix.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") + # instance transform bar = svd.transform bar # alias for svd.transform_instance bar sim = [] svd.uk.each_row { |x| sim << OpenTox::Algorithm::Similarity.cosine_num(x,bar.row(0)) } - + # # # NO AUTOSCALE #assert_equal sim[0].round_to(3), 0.346 #assert_equal sim[1].round_to(3), 0.966 @@ -138,10 +143,7 @@ class TransformTest < Test::Unit::TestCase assert_equal sim[4].round_to(3), 0.972 assert_equal sim[5].round_to(3), 1.000 - #puts - #puts svd.restore.to_a.collect { |r| r.collect{ |v| sprintf("%.2f", v) }.join(", ") }.join("\n") - - + # feature transform, only for demonstration of concept foo = svd.transform_feature foo sim = [] -- cgit v1.2.3