Skip to content

[Issue 81] 2 PCA implementations that give same results but different from Python scikit-learn implementation #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"
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,
Expand All @@ -10,6 +16,41 @@ Class {
#category : #'Math-Tests-PrincipalComponentAnalysis'
}

{ #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: #(
#(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: m.
transformedData := pca transform: m.

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 : #tests }
PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnSame [
| m pca1 pca2 |
Expand All @@ -22,6 +63,42 @@ PMPrincipalComponentAnalyserTest >> testPCAwithPCAandJacobiTransformationReturnS
self assert: pca1 transformMatrix abs closeTo: pca2 transformMatrix abs
]

{ #category : #'pca tutorial' }
PMPrincipalComponentAnalyserTest >> testSVDBasedTransformWithMeanCentredMeasurements [
"As acceptance tests we use the worked example of the PCA tutorial of Lindsay Smith"

| m pca transformedData expected |
m := 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 := PMPrincipalComponentAnalyserSVD new componentsNumber: 2.

pca fit: m.
transformedData := pca transform: m.

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 : #tests }
PMPrincipalComponentAnalyserTest >> testTransformMatrixWithJacobiTransformation [
| m pca expected |
Expand Down