How to use PCA (Principal component analysis) with SVM for classification in Mathlab?
14 views (last 30 days)
Show older comments
The input data that I have is a matrix X (99*8) , where the rows of X correspond to observations and the 8 columns to correspond (predictors or variables). I need to apply the PCA on this matrix to choose a set of predictors (as a feature selection technique) .In Matlab, I know that I can use this function [coeff,score,latent]= pca(X) for applying the PCA on input matrix, but I don't know how to use the output of this function to create a new matrix that I need to use for training Support Vector Machine classifier. Please Help me!
1 Comment
SHWETA KHARA
on 9 May 2018
Hey!!! Did you get any solution for this problem? I encountered the same problem
Answers (1)
Prasanna
on 4 Jun 2025
Hi Huda,
When you run “[coeff, score, latent] = pca(X)”, MATLAB performs PCA and returns:
- coeff: the principal component coefficients (eigenvectors),
- score: the projection of your data X into the new PCA space (i.e., transformed features),
- latent: the eigenvalues (variance explained by each component).
The score matrix is what you use as the input to your classifier. It contains the transformed features (principal components), and typically you choose a subset of the first k columns of score that explain most of the variance. For example, if you want components that explain 95% of the variance:
[coeff, score, latent, ~, explained] = pca(X);
cumExplained = cumsum(explained);
k = find(cumExplained >= 95, 1); % Find the number of components to retain
X_reduced = score(:, 1:k); % Reduced feature set for SVM input
Now, X_reduced is your new feature matrix to be used for training the SVM. If you have a label vector Y, you can train the SVM as follows:
SVMModel = fitcsvm(X_reduced, Y);
Essentially, after applying PCA, you should use the score matrix as your transformed feature set. Select the number of principal components based on how much variance you want to retain and then use the resulting reduced matrix to train your SVM classifier. For more information, refer to the following documentations:
- pca: https://www.mathworks.com/help/stats/pca.html
- SVM: https://www.mathworks.com/help/stats/fitcsvm.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!