Azzera filtri
Azzera filtri

Convert test set to training set principal component axes for use in neural networks

7 visualizzazioni (ultimi 30 giorni)
Hi,
I have a training and test set of data. I want to reduce the training set using PCA with 'VariableWeights':-
w = 1./var(mydata);
[coeff,score,latent,tsquared,explained,mu] = pca(X,'VariableWeights',w)
and then use a subset of the transformed principal components that captures 90% of the variation to train a NN. I would then like to apply this trained model to a test set. Question though is how can the test set be transformed to the same principal components that were chosen for training the NN so that it can be fed into the NN.
Thx

Risposte (1)

Sai Pavan
Sai Pavan il 19 Apr 2024
Hi John,
I understand that you want to reduce the training set using PCA with variable weights and use a subset of the transformed principal components that captures 90% of the variance to train a NN.
Please refer to the below workflow to perform this task:
  • Perform PCA on the training data with variable weights, choosing components that capture 90% of the variance.
  • Transform the test set using the PCA model obtained from the training set by subtracting the mean of the original training data from the test data and then project the test data onto the PCA space defined by the coefficients obtained from the training data.
  • Train the neural network using the reduced training data and then test the model on the reduced test data.
Please refer to the below code snippet that illustrates the workflow mentioned above:
% Perform PCA on the Training Set
w = 1./var(X);
[coeff, score, ~, ~, explained, mu] = pca(X, 'VariableWeights', w);
numComponents = find(cumsum(explained) >= 90, 1);
reducedData = score(:, 1:numComponents);
% Prepare the Test Set
testDataCentered = bsxfun(@minus, testData, mu);
testDataPCA = testDataCentered * coeff;
testDataReduced = testDataPCA(:, 1:numComponents);
% Train the Neural Network
net = feedforwardnet(10); % Example with 10 neurons in one hidden layer
net = train(net, reducedData', trainLabels');
% Test the Neural Network
predictedLabels = net(testDataReduced');
Hope it helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by