diff --git a/src/Math-TSNE/PMTSNE.class.st b/src/Math-TSNE/PMTSNE.class.st index 699ec849f..46053e4d8 100644 --- a/src/Math-TSNE/PMTSNE.class.st +++ b/src/Math-TSNE/PMTSNE.class.st @@ -9,8 +9,8 @@ Class { #name : #PMTSNE, #superclass : #Object, #instVars : [ - 'ndims', - 'initialdims', + 'outputDims', + 'initialDims', 'perplexity', 'x', 'y', @@ -122,17 +122,17 @@ PMTSNE >> epsilon: aFloat [ epsilon := aFloat ] -{ #category : #'as yet unclassified' } +{ #category : #accessing } PMTSNE >> initialDims [ - ^initialdims + ^ initialDims ] -{ #category : #'as yet unclassified' } +{ #category : #accessing } PMTSNE >> initialDims: aFloat [ - initialdims := aFloat + initialDims := aFloat ] -{ #category : #'as yet unclassified' } +{ #category : #accessing } PMTSNE >> initialDimsDefaultValue [ ^ 50 ] @@ -150,8 +150,8 @@ PMTSNE >> initialize [ { #category : #initialization } PMTSNE >> initializeUninitializedParameters [ perplexity ifNil: [ perplexity := self perplexityDefaultValue ]. - ndims ifNil: [ ndims := self ndimsDefaultValue ]. - initialdims ifNil: [ initialdims := self initialDimsDefaultValue ] + outputDims ifNil: [ outputDims := self outputDimsDefaultValue ]. + initialDims ifNil: [ initialDims := self initialDimsDefaultValue ] ] { #category : #initialization } @@ -161,7 +161,7 @@ PMTSNE >> initializeYWithRandomValues [ | a b rows columns d | rows := x dimension x. - columns := ndims. + columns := outputDims. d := PMNormalDistribution new:0 sigma: 1. a := (1 to: rows) collect: [ :row | @@ -172,12 +172,17 @@ PMTSNE >> initializeYWithRandomValues [ ] { #category : #accessing } -PMTSNE >> ndims [ - ^ ndims +PMTSNE >> outputDims [ + ^ outputDims ] -{ #category : #'as yet unclassified' } -PMTSNE >> ndimsDefaultValue [ +{ #category : #accessing } +PMTSNE >> outputDims: anInteger [ + outputDims := anInteger +] + +{ #category : #accessing } +PMTSNE >> outputDimsDefaultValue [ ^ 2 ] @@ -198,23 +203,23 @@ PMTSNE >> perplexityDefaultValue [ { #category : #running } PMTSNE >> runPcaOnX [ - "Runs PCA on X in order to reduce its dimensionality to initialdims dimensions. - - print ""Preprocessing the data using PCA..."" - (n, d) = X.shape; - X = X - Math.tile(Math.mean(X, 0), (n, 1)); - (l, M) = Math.linalg.eig(Math.dot(X.T, X)); - Y = Math.dot(X, M[:,0:no_dims]); - return Y; -" + "Runs PCA on X in order to reduce its dimensionality to initialDims dimensions." - "| analyzer i | - analyzer := PMPrincipalComponentAnalyser new: initialdims. - i := 1. - x dimension x - timesRepeat: [ analyzer accumulate: (x rowAt: i). - i := i + 1 ]. - ^ analyzer components" + self runPcaOnXUsing: PMPrincipalComponentAnalyserJacobiTransformation. +] + +{ #category : #running } +PMTSNE >> runPcaOnXUsing: aClass [ + "Runs aClass PCA on X in order to reduce its dimensionality to initialDims dimensions." + + | scaler pca | + aClass superclass = PMPrincipalComponentAnalyser + ifFalse: [ self + error: 'Argument must be subclass of PMPrincipalComponentAnalyser' ]. + scaler := PMStandardizationScaler new. + initialDims ifNil: [ initialDims := self initialDimsDefaultValue ]. + pca := aClass new componentsNumber: (initialDims min: x dimension y). + x := pca fitAndTransform: (scaler fitAndTransform: x) ] { #category : #accessing } @@ -230,6 +235,11 @@ PMTSNE >> step [ self computePairwiseAffinities ] +{ #category : #accessing } +PMTSNE >> x [ + ^ x +] + { #category : #'as yet unclassified' } PMTSNE >> x2p [ | p d beta logU n betaMin betaMax| diff --git a/src/Math-Tests-TSNE/PMTSNETest.class.st b/src/Math-Tests-TSNE/PMTSNETest.class.st index 34bd7c97e..3ecc96394 100644 --- a/src/Math-Tests-TSNE/PMTSNETest.class.st +++ b/src/Math-Tests-TSNE/PMTSNETest.class.st @@ -45,3 +45,14 @@ PMTSNETest >> testInitialDimsSetByDefaultWithFifty [ start. self assert: t initialDims equals: 50 ] + +{ #category : #tests } +PMTSNETest >> testRunPcaOnXUsing [ + "Tests the message runPcaOnXUsing" + + | t | + t := (PMTSNE new) + x: (PMMatrix rows: #(#(0 0) #(2 2) #(2 0))). + t runPcaOnXUsing: PMPrincipalComponentAnalyserJacobiTransformation. + self assert: (t x) closeTo: (PMMatrix rows: #(#(-0.5 -1.5) #(-0.5 1.5) #(1 0))). +]