input_data = rand (1000,3); % randomly generated 1000 samples, each with x, Y, z three properties
Figure (1);% control the drawing window to 1
Hold off;% so that the current axis and graphics no longer have the nature of being refreshed, close on this basis and then draw
PLOT3 (Input_data (:, 1), Input_data (:, 2), Input_data (:, 3), ' Ro ');
Percent Function PCA, Input_data, Out_dim
% use this to switch methods
use_svd_method=1;% changed to 0 after using the Eig method, the default use of the SVD method
Out_dim = 2;
[Count In_dim] = size (input_data);
The % SVD method solves the PCA, and the calculation steps are here to see an additional two blogs about PCA
if (Use_svd_method)
Sub_input_data = (Input_data-repmat (mean (input_data), count,1))/sqrt (count-1);%repmat (a,m,n) is to expand the matrix to M row n column matrix A, here refers to the expansion of Mean (Input_data) to 1000 rows 1 columns, the result is input_data each column of the mean is repeated 1000 times, the generation of a 1000*3 matrix, SQRT is to seek a prescription.
[U,s,v] = SVD (sub_input_data);
% First Out_dim columns as PCA bases
PCAV = V (:, 1:out_dim);
Output_data = Input_data * PCAV;
Else
% EIG Eigenvalue decomposition method to solve PCA
Mean_input_data = mean (Input_data);
Sub_input_data = Input_data-repmat (Mean_input_data, count,1);
Mean_mat = Sub_input_data ' * sub_input_data./(COUNT-1);
Cov_mat = Mean_mat;
[V D] = Eig (Cov_mat);
% last Out_dim columns as PCA bases
PCAV = V (:, In_dim-out_dim + 1:in_dim);
Output_data = Input_data * PCAV;
End
Percent End Function PCA
Percent visualize Output Data
Figure (2);
Hold off;
Plot (Output_data (:, 1), Output_data (:, 2), ' Bo '),%bo represents the blue color of O (circle)
Original data display diagram:
Post-dimensionality display diagram:
Using MATLAB to realize PCA demo display