title: Extracting PCA components with sklearn
body: <p>I am using <a href="http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html" rel="nofollow noreferrer">sklearn's PCA</a> for dimensionality reduction on a large set of images. Once the PCA is fitted, I would like to see what the components look like. </p>
  
  <p>One can do so by looking at the <code>components_</code> attribute. Not realizing that was available, I did something else instead:</p>
  
  <pre><code>each_component = np.eye(total_components)
  component_im_array = pca.inverse_transform(each_component)
  
  for i in range(num_components):
     component_im = component_im_array[i, :].reshape(height, width)
     # do something with component_im
  </code></pre>
  
  <p>In other words, I create an image in the PCA space that has all features but 1 set to 0. By inversely transforming them, I should then get the image in the original space which, once transformed, can be expressed solely with that PCA component.</p>
  
  <p>The following image shows the results. On the left is the component calculated using my method. On the right is <code>pca.components_[i]</code> directly. Additionally, with my method, most images are very similar (but they <em>are</em> different) while by accessing the <code>components_</code> the images are very different as I would have expected</p>
  
  <p>Is there a conceptual problem in my method? Clearly the components from <code>pca.components_[i]</code> are correct (or at least more correct) than the ones I'm getting. Thanks!</p>
  
  <p><img src="https://i.stack.imgur.com/onAOc.png" alt="left: calculated component, right: real component"></p>