From f4b1cd1bc30547516a346ad3291204a4d1d91c4d Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 21 Apr 2019 21:33:11 +0100 Subject: [PATCH 1/8] [issue-81] Improved the name of the variables. --- .../PMPrincipalComponentAnalyserTest.class.st | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st index b648f76b3..15a8018ca 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st @@ -19,8 +19,8 @@ Class { { #category : #'pca tutorial' } PMPrincipalComponentAnalyserTest >> testJacobiBasedTransformWithMeanCentredMeasurements [ "As acceptance tests we use the worked example of the PCA tutorial of Lindsay Smith" - | m pca transformedData expected | - m := PMMatrix rows: #( + | meanCentredData pca transformedData expected | + meanCentredData := PMMatrix rows: #( #(0.69 0.49) #(-1.31 -1.21) #(0.39 0.99) @@ -34,8 +34,8 @@ PMPrincipalComponentAnalyserTest >> testJacobiBasedTransformWithMeanCentredMeasu ). pca := PMPrincipalComponentAnalyserJacobiTransformation new componentsNumber: 2. - pca fit: m. - transformedData := pca transform: m. + pca fit: meanCentredData. + transformedData := pca transform: meanCentredData. expected := PMMatrix rows: #( #(-0.827970186 -0.175115307) @@ -67,8 +67,8 @@ PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnS PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurements [ "As acceptance tests we use the worked example of the PCA tutorial of Lindsay Smith" - | m pca transformedData expected | - m := PMMatrix rows: #( + | meanCentredData pca transformedData expected | + meanCentredData := PMMatrix rows: #( #(0.69 0.49) #(-1.31 -1.21) #(0.39 0.99) @@ -82,8 +82,8 @@ PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurem ). pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. - pca fit: m. - transformedData := pca transform: m. + pca fit: meanCentredData. + transformedData := pca transform: meanCentredData. expected := PMMatrix rows: #( #(-0.827970186 -0.175115307) From 1b8284d75ae47a4e914d9641af6480d7c0e87c05 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 21 Apr 2019 21:37:13 +0100 Subject: [PATCH 2/8] [issue-81] Improved the name of the categories. --- .../PMPrincipalComponentAnalyserTest.class.st | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st index 15a8018ca..66773a6df 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st @@ -16,7 +16,7 @@ Class { #category : #'Math-Tests-PrincipalComponentAnalysis' } -{ #category : #'pca tutorial' } +{ #category : #'lindsay-smith-pca-tutorial' } PMPrincipalComponentAnalyserTest >> testJacobiBasedTransformWithMeanCentredMeasurements [ "As acceptance tests we use the worked example of the PCA tutorial of Lindsay Smith" | meanCentredData pca transformedData expected | @@ -51,7 +51,7 @@ PMPrincipalComponentAnalyserTest >> testJacobiBasedTransformWithMeanCentredMeasu self assert: (transformedData abs) closeTo: expected abs. ] -{ #category : #tests } +{ #category : #'scikit-learn-example' } PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnSame [ | m pca1 pca2 | m := PMMatrix rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). @@ -63,7 +63,7 @@ PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnS self assert: pca1 transformMatrix abs closeTo: pca2 transformMatrix abs ] -{ #category : #'pca tutorial' } +{ #category : #'lindsay-smith-pca-tutorial' } PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurements [ "As acceptance tests we use the worked example of the PCA tutorial of Lindsay Smith" @@ -99,7 +99,7 @@ PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurem self assert: (transformedData abs) closeTo: expected abs. ] -{ #category : #tests } +{ #category : #'scikit-learn-example' } PMPrincipalComponentAnalyserTest >> testTransformMatrixWithJacobiTransformation [ | m pca expected | m := PMMatrix @@ -114,7 +114,7 @@ PMPrincipalComponentAnalyserTest >> testTransformMatrixWithJacobiTransformation self assert: pca transformMatrix abs closeTo: expected ] -{ #category : #tests } +{ #category : #'scikit-learn-example' } PMPrincipalComponentAnalyserTest >> testTransformMatrixWithSVD [ | m pca expected | m := PMMatrix From b269f5ac91d227854aca4291891f9bfc3d34993c Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 21 Apr 2019 21:38:57 +0100 Subject: [PATCH 3/8] [issue-81] Removed obselete comments. --- .../PMPrincipalComponentAnalyserTest.class.st | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st index 66773a6df..1fd6ba263 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st @@ -18,7 +18,6 @@ Class { { #category : #'lindsay-smith-pca-tutorial' } PMPrincipalComponentAnalyserTest >> testJacobiBasedTransformWithMeanCentredMeasurements [ - "As acceptance tests we use the worked example of the PCA tutorial of Lindsay Smith" | meanCentredData pca transformedData expected | meanCentredData := PMMatrix rows: #( #(0.69 0.49) @@ -65,8 +64,6 @@ PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnS { #category : #'lindsay-smith-pca-tutorial' } PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurements [ - "As acceptance tests we use the worked example of the PCA tutorial of Lindsay Smith" - | meanCentredData pca transformedData expected | meanCentredData := PMMatrix rows: #( #(0.69 0.49) From 9dada092ce12daf2235e73587fc2f32f9d7d1eac Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 21 Apr 2019 22:03:37 +0100 Subject: [PATCH 4/8] [issue-81] Emphasised the polymorphism of the two implementations of PCA in their respective tests. --- .../PMPCAJacobiTransformationTest.class.st | 54 ++++++++++++ ...CASingularValueDecompositionTest.class.st} | 83 ++++--------------- 2 files changed, 71 insertions(+), 66 deletions(-) create mode 100644 src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st rename src/Math-Tests-PrincipalComponentAnalysis/{PMPrincipalComponentAnalyserTest.class.st => PMPCASingularValueDecompositionTest.class.st} (54%) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st new file mode 100644 index 000000000..7f02b9343 --- /dev/null +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st @@ -0,0 +1,54 @@ +Class { + #name : #PMPCAJacobiTransformationTest, + #superclass : #TestCase, + #category : #'Math-Tests-PrincipalComponentAnalysis' +} + +{ #category : #'scikit-learn-example' } +PMPCAJacobiTransformationTest >> testTransformMatrix [ + | m pca expected | + m := PMMatrix + rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). + pca := PMPrincipalComponentAnalyserJacobiTransformation new + componentsNumber: 2. + + pca fit: m. + + expected := (PMMatrix + rows: #(#(-0.83849224 -0.54491354) #(0.54491354 -0.83849224))) abs. + self assert: pca transformMatrix abs closeTo: expected +] + +{ #category : #'lindsay-smith-pca-tutorial' } +PMPCAJacobiTransformationTest >> testTransformWithMeanCentredMeasurements [ + | meanCentredData pca transformedData expected | + meanCentredData := PMMatrix rows: #( + #(0.69 0.49) + #(-1.31 -1.21) + #(0.39 0.99) + #(0.09 0.29) + #(1.29 1.09) + #(0.49 0.79) + #(0.19 -0.31) + #(-0.81 -0.81) + #(-0.31 -0.31) + #(-0.71 -1.01) + ). + pca := PMPrincipalComponentAnalyserJacobiTransformation new componentsNumber: 2. + + pca fit: meanCentredData. + transformedData := pca transform: meanCentredData. + + expected := PMMatrix rows: #( + #(-0.827970186 -0.175115307) + #(1.77758033 0.142857227) + #(-0.992197494 0.384374989) + #(-0.274210416 0.130417207) + #(-1.67580142 -0.209498461) + #(-0.912949103 0.175282444) + #(0.0991094375 -0.349824698) + #(1.14457216 0.0464172582) + #(0.438046137 0.0177646297) + #(1.22382056 -0.162675287)). + self assert: (transformedData abs) closeTo: expected abs. +] diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st similarity index 54% rename from src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st rename to src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st index 1fd6ba263..7ed36beeb 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st @@ -5,7 +5,7 @@ The tests in this class compare the output from PolyMath with: are the mean-centred data in section 3.1, step 2. " Class { - #name : #PMPrincipalComponentAnalyserTest, + #name : #PMPCASingularValueDecompositionTest, #superclass : #TestCase, #instVars : [ 'average', @@ -16,42 +16,8 @@ Class { #category : #'Math-Tests-PrincipalComponentAnalysis' } -{ #category : #'lindsay-smith-pca-tutorial' } -PMPrincipalComponentAnalyserTest >> testJacobiBasedTransformWithMeanCentredMeasurements [ - | meanCentredData pca transformedData expected | - meanCentredData := PMMatrix rows: #( - #(0.69 0.49) - #(-1.31 -1.21) - #(0.39 0.99) - #(0.09 0.29) - #(1.29 1.09) - #(0.49 0.79) - #(0.19 -0.31) - #(-0.81 -0.81) - #(-0.31 -0.31) - #(-0.71 -1.01) - ). - pca := PMPrincipalComponentAnalyserJacobiTransformation new componentsNumber: 2. - - pca fit: meanCentredData. - transformedData := pca transform: meanCentredData. - - expected := PMMatrix rows: #( - #(-0.827970186 -0.175115307) - #(1.77758033 0.142857227) - #(-0.992197494 0.384374989) - #(-0.274210416 0.130417207) - #(-1.67580142 -0.209498461) - #(-0.912949103 0.175282444) - #(0.0991094375 -0.349824698) - #(1.14457216 0.0464172582) - #(0.438046137 0.0177646297) - #(1.22382056 -0.162675287)). - self assert: (transformedData abs) closeTo: expected abs. -] - { #category : #'scikit-learn-example' } -PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnSame [ +PMPCASingularValueDecompositionTest >> testPCAwithPCAandJacobiTransformationReturnSame [ | m pca1 pca2 | m := PMMatrix rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). pca1 := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. @@ -62,8 +28,22 @@ PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnS self assert: pca1 transformMatrix abs closeTo: pca2 transformMatrix abs ] +{ #category : #'scikit-learn-example' } +PMPCASingularValueDecompositionTest >> testTransformMatrix [ + | m pca expected | + m := PMMatrix + rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). + pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. + + pca fit: m. + + expected := (PMMatrix + rows: #(#(-0.83849224 -0.54491354) #(0.54491354 -0.83849224))) abs. + self assert: pca transformMatrix abs closeTo: expected +] + { #category : #'lindsay-smith-pca-tutorial' } -PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurements [ +PMPCASingularValueDecompositionTest >> testTransformWithMeanCentredMeasurements [ | meanCentredData pca transformedData expected | meanCentredData := PMMatrix rows: #( #(0.69 0.49) @@ -95,32 +75,3 @@ PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurem #(1.22382056 -0.162675287)). self assert: (transformedData abs) closeTo: expected abs. ] - -{ #category : #'scikit-learn-example' } -PMPrincipalComponentAnalyserTest >> testTransformMatrixWithJacobiTransformation [ - | m pca expected | - m := PMMatrix - rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). - pca := PMPrincipalComponentAnalyserJacobiTransformation new - componentsNumber: 2. - - pca fit: m. - - expected := (PMMatrix - rows: #(#(-0.83849224 -0.54491354) #(0.54491354 -0.83849224))) abs. - self assert: pca transformMatrix abs closeTo: expected -] - -{ #category : #'scikit-learn-example' } -PMPrincipalComponentAnalyserTest >> testTransformMatrixWithSVD [ - | m pca expected | - m := PMMatrix - rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). - pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. - - pca fit: m. - - expected := (PMMatrix - rows: #(#(-0.83849224 -0.54491354) #(0.54491354 -0.83849224))) abs. - self assert: pca transformMatrix abs closeTo: expected -] From 6b13ed26a10105fdd1b3b1bbf8a7e182f8962e73 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 21 Apr 2019 22:10:55 +0100 Subject: [PATCH 5/8] [issue-81] Removed duplicate code to set up messages. --- .../PMPCAJacobiTransformationTest.class.st | 16 +++++++++++----- .../PMPCASingularValueDecompositionTest.class.st | 14 +++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st index 7f02b9343..a9cec0961 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st @@ -1,16 +1,23 @@ Class { #name : #PMPCAJacobiTransformationTest, #superclass : #TestCase, + #instVars : [ + 'pca' + ], #category : #'Math-Tests-PrincipalComponentAnalysis' } +{ #category : #running } +PMPCAJacobiTransformationTest >> setUp [ + pca := PMPrincipalComponentAnalyserJacobiTransformation new componentsNumber: 2. + +] + { #category : #'scikit-learn-example' } PMPCAJacobiTransformationTest >> testTransformMatrix [ - | m pca expected | + | m expected | m := PMMatrix rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). - pca := PMPrincipalComponentAnalyserJacobiTransformation new - componentsNumber: 2. pca fit: m. @@ -21,7 +28,7 @@ PMPCAJacobiTransformationTest >> testTransformMatrix [ { #category : #'lindsay-smith-pca-tutorial' } PMPCAJacobiTransformationTest >> testTransformWithMeanCentredMeasurements [ - | meanCentredData pca transformedData expected | + | meanCentredData transformedData expected | meanCentredData := PMMatrix rows: #( #(0.69 0.49) #(-1.31 -1.21) @@ -34,7 +41,6 @@ PMPCAJacobiTransformationTest >> testTransformWithMeanCentredMeasurements [ #(-0.31 -0.31) #(-0.71 -1.01) ). - pca := PMPrincipalComponentAnalyserJacobiTransformation new componentsNumber: 2. pca fit: meanCentredData. transformedData := pca transform: meanCentredData. diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st index 7ed36beeb..51b8466e7 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st @@ -11,11 +11,17 @@ Class { 'average', 'covarianceMatrix', 'accumulator', - 'server' + 'server', + 'pca' ], #category : #'Math-Tests-PrincipalComponentAnalysis' } +{ #category : #running } +PMPCASingularValueDecompositionTest >> setUp [ + pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. +] + { #category : #'scikit-learn-example' } PMPCASingularValueDecompositionTest >> testPCAwithPCAandJacobiTransformationReturnSame [ | m pca1 pca2 | @@ -30,10 +36,9 @@ PMPCASingularValueDecompositionTest >> testPCAwithPCAandJacobiTransformationRetu { #category : #'scikit-learn-example' } PMPCASingularValueDecompositionTest >> testTransformMatrix [ - | m pca expected | + | m expected | m := PMMatrix rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). - pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. pca fit: m. @@ -44,7 +49,7 @@ PMPCASingularValueDecompositionTest >> testTransformMatrix [ { #category : #'lindsay-smith-pca-tutorial' } PMPCASingularValueDecompositionTest >> testTransformWithMeanCentredMeasurements [ - | meanCentredData pca transformedData expected | + | meanCentredData transformedData expected | meanCentredData := PMMatrix rows: #( #(0.69 0.49) #(-1.31 -1.21) @@ -57,7 +62,6 @@ PMPCASingularValueDecompositionTest >> testTransformWithMeanCentredMeasurements #(-0.31 -0.31) #(-0.71 -1.01) ). - pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. pca fit: meanCentredData. transformedData := pca transform: meanCentredData. From c0ce02ac64355a4bd4518918da42b90465a38fd7 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 22 Apr 2019 09:19:44 +0100 Subject: [PATCH 6/8] [issue-81] Removed unused instance variables. --- .../PMPCASingularValueDecompositionTest.class.st | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st index 51b8466e7..8c05436cb 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st @@ -8,10 +8,6 @@ Class { #name : #PMPCASingularValueDecompositionTest, #superclass : #TestCase, #instVars : [ - 'average', - 'covarianceMatrix', - 'accumulator', - 'server', 'pca' ], #category : #'Math-Tests-PrincipalComponentAnalysis' From eb3bce8c7a5d6d6ff09e716b42369a3357d0d8dc Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Tue, 23 Apr 2019 10:45:57 +0100 Subject: [PATCH 7/8] [issue-81] Use an existing message that has the same method. --- .../PMPCAJacobiTransformationTest.class.st | 3 +-- .../PMPCASingularValueDecompositionTest.class.st | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st index a9cec0961..989217131 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st @@ -42,8 +42,7 @@ PMPCAJacobiTransformationTest >> testTransformWithMeanCentredMeasurements [ #(-0.71 -1.01) ). - pca fit: meanCentredData. - transformedData := pca transform: meanCentredData. + transformedData := pca fitAndTransform: meanCentredData. expected := PMMatrix rows: #( #(-0.827970186 -0.175115307) diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st index 8c05436cb..050094899 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st @@ -59,8 +59,7 @@ PMPCASingularValueDecompositionTest >> testTransformWithMeanCentredMeasurements #(-0.71 -1.01) ). - pca fit: meanCentredData. - transformedData := pca transform: meanCentredData. + transformedData := pca fitAndTransform: meanCentredData. expected := PMMatrix rows: #( #(-0.827970186 -0.175115307) From 2d67e5bf250edefc8dc7576cf5e57830ead2e621 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Tue, 23 Apr 2019 20:52:21 +0100 Subject: [PATCH 8/8] [issue-81] Moved the duplicate acceptance tests to a more sensible place. --- .../PMPCAJacobiTransformationTest.class.st | 57 ++--------------- ...PCASingularValueDecompositionTest.class.st | 57 +---------------- .../PMPrincipalComponentAnalyserTest.class.st | 64 +++++++++++++++++++ 3 files changed, 73 insertions(+), 105 deletions(-) create mode 100644 src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st index 989217131..33ed4e4df 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCAJacobiTransformationTest.class.st @@ -1,59 +1,14 @@ +" +This test checks that the Jacobi transform based PCA meets the acceptance requirements defined in PMPrincipalComponentAnalysisTest +" Class { #name : #PMPCAJacobiTransformationTest, - #superclass : #TestCase, - #instVars : [ - 'pca' - ], + #superclass : #PMPrincipalComponentAnalyserTest, #category : #'Math-Tests-PrincipalComponentAnalysis' } { #category : #running } PMPCAJacobiTransformationTest >> setUp [ - pca := PMPrincipalComponentAnalyserJacobiTransformation new componentsNumber: 2. - -] - -{ #category : #'scikit-learn-example' } -PMPCAJacobiTransformationTest >> testTransformMatrix [ - | m expected | - m := PMMatrix - rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). - - pca fit: m. - - expected := (PMMatrix - rows: #(#(-0.83849224 -0.54491354) #(0.54491354 -0.83849224))) abs. - self assert: pca transformMatrix abs closeTo: expected -] - -{ #category : #'lindsay-smith-pca-tutorial' } -PMPCAJacobiTransformationTest >> testTransformWithMeanCentredMeasurements [ - | meanCentredData transformedData expected | - meanCentredData := PMMatrix rows: #( - #(0.69 0.49) - #(-1.31 -1.21) - #(0.39 0.99) - #(0.09 0.29) - #(1.29 1.09) - #(0.49 0.79) - #(0.19 -0.31) - #(-0.81 -0.81) - #(-0.31 -0.31) - #(-0.71 -1.01) - ). - - transformedData := pca fitAndTransform: meanCentredData. - - expected := PMMatrix rows: #( - #(-0.827970186 -0.175115307) - #(1.77758033 0.142857227) - #(-0.992197494 0.384374989) - #(-0.274210416 0.130417207) - #(-1.67580142 -0.209498461) - #(-0.912949103 0.175282444) - #(0.0991094375 -0.349824698) - #(1.14457216 0.0464172582) - #(0.438046137 0.0177646297) - #(1.22382056 -0.162675287)). - self assert: (transformedData abs) closeTo: expected abs. + pca := PMPrincipalComponentAnalyserJacobiTransformation new + componentsNumber: 2 ] diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st index 050094899..23ae687eb 100644 --- a/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPCASingularValueDecompositionTest.class.st @@ -1,21 +1,15 @@ " -The tests in this class compare the output from PolyMath with: -1) an example from Scikit-Learn's documentation; and -2) the PCA Tutorial by Lindsay Smith (see http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf). The input data used -are the mean-centred data in section 3.1, step 2. +This test checks that the SVD transform based PCA meets the acceptance requirements defined in PMPrincipalComponentAnalysisTest " Class { #name : #PMPCASingularValueDecompositionTest, - #superclass : #TestCase, - #instVars : [ - 'pca' - ], + #superclass : #PMPrincipalComponentAnalyserTest, #category : #'Math-Tests-PrincipalComponentAnalysis' } { #category : #running } PMPCASingularValueDecompositionTest >> setUp [ - pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2. + pca := PMPrincipalComponentAnalyserSVD new componentsNumber: 2 ] { #category : #'scikit-learn-example' } @@ -29,48 +23,3 @@ PMPCASingularValueDecompositionTest >> testPCAwithPCAandJacobiTransformationRetu self assert: pca1 transformMatrix abs closeTo: pca2 transformMatrix abs ] - -{ #category : #'scikit-learn-example' } -PMPCASingularValueDecompositionTest >> testTransformMatrix [ - | m expected | - m := PMMatrix - rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). - - pca fit: m. - - expected := (PMMatrix - rows: #(#(-0.83849224 -0.54491354) #(0.54491354 -0.83849224))) abs. - self assert: pca transformMatrix abs closeTo: expected -] - -{ #category : #'lindsay-smith-pca-tutorial' } -PMPCASingularValueDecompositionTest >> testTransformWithMeanCentredMeasurements [ - | meanCentredData transformedData expected | - meanCentredData := PMMatrix rows: #( - #(0.69 0.49) - #(-1.31 -1.21) - #(0.39 0.99) - #(0.09 0.29) - #(1.29 1.09) - #(0.49 0.79) - #(0.19 -0.31) - #(-0.81 -0.81) - #(-0.31 -0.31) - #(-0.71 -1.01) - ). - - transformedData := pca fitAndTransform: meanCentredData. - - expected := PMMatrix rows: #( - #(-0.827970186 -0.175115307) - #(1.77758033 0.142857227) - #(-0.992197494 0.384374989) - #(-0.274210416 0.130417207) - #(-1.67580142 -0.209498461) - #(-0.912949103 0.175282444) - #(0.0991094375 -0.349824698) - #(1.14457216 0.0464172582) - #(0.438046137 0.0177646297) - #(1.22382056 -0.162675287)). - self assert: (transformedData abs) closeTo: expected abs. -] diff --git a/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st new file mode 100644 index 000000000..48cea390c --- /dev/null +++ b/src/Math-Tests-PrincipalComponentAnalysis/PMPrincipalComponentAnalyserTest.class.st @@ -0,0 +1,64 @@ +" +The tests in this class compare the output from PolyMath with: +1) an example from Scikit-Learn's documentation; and +2) the PCA Tutorial by Lindsay Smith (see http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf). The input data used +are the mean-centred data in section 3.1, step 2. +" +Class { + #name : #PMPrincipalComponentAnalyserTest, + #superclass : #TestCase, + #instVars : [ + 'pca' + ], + #category : #'Math-Tests-PrincipalComponentAnalysis' +} + +{ #category : #testing } +PMPrincipalComponentAnalyserTest class >> isAbstract [ + ^ self name = #PMPrincipalComponentAnalyserTest +] + +{ #category : #'scikit-learn-example' } +PMPrincipalComponentAnalyserTest >> testTransformMatrix [ + | m expected | + m := PMMatrix + rows: #(#(-1 -1) #(-2 -1) #(-3 -2) #(1 1) #(2 1) #(3 2)). + + pca fit: m. + + expected := (PMMatrix + rows: #(#(-0.83849224 -0.54491354) #(0.54491354 -0.83849224))) abs. + self assert: pca transformMatrix abs closeTo: expected +] + +{ #category : #'lindsay-smith-pca-tutorial' } +PMPrincipalComponentAnalyserTest >> testTransformWithMeanCentredMeasurements [ + | meanCentredData transformedData expected | + meanCentredData := PMMatrix rows: #( + #(0.69 0.49) + #(-1.31 -1.21) + #(0.39 0.99) + #(0.09 0.29) + #(1.29 1.09) + #(0.49 0.79) + #(0.19 -0.31) + #(-0.81 -0.81) + #(-0.31 -0.31) + #(-0.71 -1.01) + ). + + transformedData := pca fitAndTransform: meanCentredData. + + expected := PMMatrix rows: #( + #(-0.827970186 -0.175115307) + #(1.77758033 0.142857227) + #(-0.992197494 0.384374989) + #(-0.274210416 0.130417207) + #(-1.67580142 -0.209498461) + #(-0.912949103 0.175282444) + #(0.0991094375 -0.349824698) + #(1.14457216 0.0464172582) + #(0.438046137 0.0177646297) + #(1.22382056 -0.162675287)). + self assert: (transformedData abs) closeTo: expected abs. +]