How to Whiten my data before plugging it into PCA












0















I am applying PCA to some ECG features, that I extracted. I found some code and learned it from the Matlab website, to apply PCA. However, I would like to whiten my data before applying PCA to it. My data is a matrix (125 x 9), with 9 features, and 125 tests. My matrix name is ratings.
I have two blocks of code: PCA code and whitening code. My PCA code doesn't include whitening, but I hope by answering this question, it will.



Whitening Code



I subtracted the mean of each column from the matrix, as follows:



mean_ratings=mean(ratings); %get mean of each column
ratings=ratings-repmat(mean_ratings, 375,1); %subtract mean of each
%column from each element of the column


I am a little but stuck on what to do after this. I calculated my enter image description here, by following the sample code:



sigma = ratings * ratings' / size(ratings, 2); %calculate the value of sigma
[U,S,V] = svd(sigma); %get S value and eigen vectors

xPCAwhite = diag(1./sqrt(diag(S) + epsilon)) * U' * ratings; %calculate
%whitened data


PCA Code



I perform my PCA, without considering whitening, by using the code below:



C = corr(ratings,ratings);
w=1./var(ratings);
[wcoeff,score,latent,tsquared,explained] = pca(ratings,...
'VariableWeights',w);
coefforth = inv(diag(std(ratings)))*wcoeff;
cscores = zscore(ratings)*coefforth;
figure()
plot(score(:,1),score(:,2),'+')
xlabel('1st Principal Component')
ylabel('2nd Principal Component')


Now my question is that, do I just plug in the variable 'xPCAwhite' instead of ratings for my PCA code?










share|improve this question



























    0















    I am applying PCA to some ECG features, that I extracted. I found some code and learned it from the Matlab website, to apply PCA. However, I would like to whiten my data before applying PCA to it. My data is a matrix (125 x 9), with 9 features, and 125 tests. My matrix name is ratings.
    I have two blocks of code: PCA code and whitening code. My PCA code doesn't include whitening, but I hope by answering this question, it will.



    Whitening Code



    I subtracted the mean of each column from the matrix, as follows:



    mean_ratings=mean(ratings); %get mean of each column
    ratings=ratings-repmat(mean_ratings, 375,1); %subtract mean of each
    %column from each element of the column


    I am a little but stuck on what to do after this. I calculated my enter image description here, by following the sample code:



    sigma = ratings * ratings' / size(ratings, 2); %calculate the value of sigma
    [U,S,V] = svd(sigma); %get S value and eigen vectors

    xPCAwhite = diag(1./sqrt(diag(S) + epsilon)) * U' * ratings; %calculate
    %whitened data


    PCA Code



    I perform my PCA, without considering whitening, by using the code below:



    C = corr(ratings,ratings);
    w=1./var(ratings);
    [wcoeff,score,latent,tsquared,explained] = pca(ratings,...
    'VariableWeights',w);
    coefforth = inv(diag(std(ratings)))*wcoeff;
    cscores = zscore(ratings)*coefforth;
    figure()
    plot(score(:,1),score(:,2),'+')
    xlabel('1st Principal Component')
    ylabel('2nd Principal Component')


    Now my question is that, do I just plug in the variable 'xPCAwhite' instead of ratings for my PCA code?










    share|improve this question

























      0












      0








      0


      1






      I am applying PCA to some ECG features, that I extracted. I found some code and learned it from the Matlab website, to apply PCA. However, I would like to whiten my data before applying PCA to it. My data is a matrix (125 x 9), with 9 features, and 125 tests. My matrix name is ratings.
      I have two blocks of code: PCA code and whitening code. My PCA code doesn't include whitening, but I hope by answering this question, it will.



      Whitening Code



      I subtracted the mean of each column from the matrix, as follows:



      mean_ratings=mean(ratings); %get mean of each column
      ratings=ratings-repmat(mean_ratings, 375,1); %subtract mean of each
      %column from each element of the column


      I am a little but stuck on what to do after this. I calculated my enter image description here, by following the sample code:



      sigma = ratings * ratings' / size(ratings, 2); %calculate the value of sigma
      [U,S,V] = svd(sigma); %get S value and eigen vectors

      xPCAwhite = diag(1./sqrt(diag(S) + epsilon)) * U' * ratings; %calculate
      %whitened data


      PCA Code



      I perform my PCA, without considering whitening, by using the code below:



      C = corr(ratings,ratings);
      w=1./var(ratings);
      [wcoeff,score,latent,tsquared,explained] = pca(ratings,...
      'VariableWeights',w);
      coefforth = inv(diag(std(ratings)))*wcoeff;
      cscores = zscore(ratings)*coefforth;
      figure()
      plot(score(:,1),score(:,2),'+')
      xlabel('1st Principal Component')
      ylabel('2nd Principal Component')


      Now my question is that, do I just plug in the variable 'xPCAwhite' instead of ratings for my PCA code?










      share|improve this question














      I am applying PCA to some ECG features, that I extracted. I found some code and learned it from the Matlab website, to apply PCA. However, I would like to whiten my data before applying PCA to it. My data is a matrix (125 x 9), with 9 features, and 125 tests. My matrix name is ratings.
      I have two blocks of code: PCA code and whitening code. My PCA code doesn't include whitening, but I hope by answering this question, it will.



      Whitening Code



      I subtracted the mean of each column from the matrix, as follows:



      mean_ratings=mean(ratings); %get mean of each column
      ratings=ratings-repmat(mean_ratings, 375,1); %subtract mean of each
      %column from each element of the column


      I am a little but stuck on what to do after this. I calculated my enter image description here, by following the sample code:



      sigma = ratings * ratings' / size(ratings, 2); %calculate the value of sigma
      [U,S,V] = svd(sigma); %get S value and eigen vectors

      xPCAwhite = diag(1./sqrt(diag(S) + epsilon)) * U' * ratings; %calculate
      %whitened data


      PCA Code



      I perform my PCA, without considering whitening, by using the code below:



      C = corr(ratings,ratings);
      w=1./var(ratings);
      [wcoeff,score,latent,tsquared,explained] = pca(ratings,...
      'VariableWeights',w);
      coefforth = inv(diag(std(ratings)))*wcoeff;
      cscores = zscore(ratings)*coefforth;
      figure()
      plot(score(:,1),score(:,2),'+')
      xlabel('1st Principal Component')
      ylabel('2nd Principal Component')


      Now my question is that, do I just plug in the variable 'xPCAwhite' instead of ratings for my PCA code?







      matlab pca






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 3:23









      MuhammadMuhammad

      170418




      170418
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474400%2fhow-to-whiten-my-data-before-plugging-it-into-pca%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474400%2fhow-to-whiten-my-data-before-plugging-it-into-pca%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python