For dataset that used to train a classification model, we can use LDA to enhance the discriminant possibility.
Step 1 First calculate the within-class scatter and between-classes scatter
within-class scatter
SW=classesc∑j∈c∑Pc(Xj−μc)(Xj−μc)T
between-classes scatter
SB=classesc∑(μc−μ)(μc−μ)T
Step 2 Fine the projection direction w, maximum the ratio of between classes scatter and within-class scatter
SBw=λSWw
this equals to
SW−1SBw=λw
eigenvector w : this is the optimal projection direction (discriminant direction)
eigenvalue λ : reflect the discriminant ability in this direction (value greater, classification ability better)
Step 3 Select first k eigenvectors if there are k classes, most K-1 discriminant direction can be selected (because rank(SB))cannotexceedK−1
Select the first k eigenvector, along the eigen values ordered descent, comprise projection matrix W=[w1,w2,...,wk]
Step 4 Project data into reduced space
Kprojected=XW
Xprojected is the dimensionality reduced data, this could be used in classification.
PCA
The PCA algorithm should be applied on a set of training dataset, which is to reduce the dimensionality, by discard the lower variance features, but keep the principal components inside the data. It generally take the training dataset usually is a set of numerical vector, and apply the PCA on it, to get the Principal Components, and calculation the projection of the original vertors on it. The principal components should be kept for future applied on test dataset before predicting.
A simulatation of PCA on a concrete 3*2 dataset (3 samples, 2 features) Given data matrix X (3 samples * 2 features):
X=125246
Step 1 Center the Data (Mean Normalization) Compute the mean of each feature and subtract it: