I have written the following code to implement a PCA
algorithm. It works on standalone basis, however when I put it into a function that is called the following output is returned pca_funct[{{2,3,4},{4,5,6}},2]
as opposed to the result of function evaluation.
Any suggestions ?
pca_funct[X_, k_] :=
Module[{Dim, m, n, mn, ones, Cv, phi0, lambda, phi},
Dim = Dimensions[X]; m = Dim[[1]];
n = Dim[[2]]; (*Compute dimensions of input matrix X*)
mn = List /@ Mean[X];(*Compute column mean matrix for X and convert it into matrix form*)
ones = ConstantArray[1, {m, 1}];
X = X - ones.Transpose[mn]; (*Center columns of X around their means to get new matrix
where columns have mean zero*)
Cv = Transpose[X].X/m;(*Step 1 in algorithm. Compute covariance matrix*)
phi0 = Eigenvectors[Cv]; (*Step 2 in algorithm. Compute Eigensystem*)
lambda = Eigenvalues[Cv];
phi = phi0[1 ;; k, 1 ;; n];
phi]
X = {{2, 3, 4}, {4, 5, 6}}; k = 2;
phi = pca_funct[X, k];
phi // MatrixForm
pca_funct[{{2,3,4},{4,5,6}},2]
X
inside the Module. ButX
is passed parameter. $\endgroup$