How can I apply PCA to KNN? I want to know the do not want to how to use library

I will denote a $n\times p$ data matrix by $X$, where $n&lt;p$. That is, each row of $X$ is one sample data with $p$ feature variables.



By using singular vector decomposition method, I can decompose $X$ into $A$, $B$, and $C$ such that $X=ABC$, where $A$ is a $n\times n$ matrix satisfying $A^TA=I_{[n\times n]}$, $B$ is a $n\times p$ matrix whose diagonal elements are all non-negative and real but non-diagonal elements are all zeros, and $C$ is a $p\times p$ matrix such that $P^TP = I_{[p\times p]}$.
Without the loss of generality, I assume that $B$ is obtained satisfying that the diagonal elements of $B$ is descending order, i.e., $b_{i,i} &gt; b_{j,j}$ for $i&lt;j$.



The score matrix, $S$, is obtained by $S=XC$, whose dimension is $n\times p$.

In order to reduce the dimension of variables, we do as follows:
Denoting the $i$-th column of $C$ by $c_i$, which is a loading vector with respect to $i$-th principal component.
Hence, deleting $c_{k+1}$ to $c_{p}$, I can obtain $C_k = [c_1~c_2~\cdots~c_k]$.
The dimension-reduced score matrix, $S$, is obtained by $S_k=XC_k$.



In this situation, how can I run KNN?
Actually, I already made my own KNN code, whose input is trainX, trainY, and testX, and whose output is evaluatedY.
I want to compare (i) basic KNN and (ii) dimension-reduction-applied KNN.

I obtained error rate of (i) basic KNN using 5-fold cross validation method.

yhat = myKNN(trainX, trainY, testX);
errRate = myErr(y, yhat);


To obtain error rate of (ii) dimension-reduction-applied KNN, is the following correct?

trainS= myDimensionReduction(trainX, k)
testS= myDimensionReduction(testX, k)
yhat = myKNN(trainS, trainY, testS);
errRate = myErr(y, yhat);


where myDimensionReduction is a function that converts a data matrix $(n\times p)$ into a reduced data matrix $(n\times k)$.
Everything looks good. However, what I doubt about is the myDimensionReduction function. When you apply the PCA on the test data, you have to multiply $X_{test}$ by the matrix $C$ that has been generated from the training data, not by the test data. This is to stress that the test data should not be used in any step of the training process. If this is what the myDimensionReduction function does, that is alright. Otherwise, if this function creates the matrix $C$, then it is not right.

I know you didn't ask for the library, but I think it makes sense in here: The whole philosophy of scikit-learn is to create objects that correspond to algorithms, use the training data with the fit method and apply the transform method to the test data.