Using MATLAB to realize PCA demo display

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.