Principal Component Analysis (PCA) - accessing shape
Question: I am a beginner in python and I am trying to apply Principal Component
Analysis (PCA) to a set of images. I want to put the images in a matrix to be
able to perform PCA. I am still at the beginning but I am having errors.

    
    
    import numpy as np
    import Image
    import os
    
    #insert images to matrix
    dirname = 'C:\Users\Karim\Downloads\shp_marcel_train\Marcel-Train\A'
    X = [np.asarray(Image.open(os.path.join(dirname, fn))) for fn in os.listdir(dirname)]
    
    #get dimensions
    num_data,dim = X.shape
    

It gives `AttributeError: 'list' object has no attribute 'shape'`

anyone can help?

a link to a detailed tutorial would be appreciated


Answer: As you've defined it, `X` is a list:

    
    
    X = [... for fn in ...]
    

Python lists do not have a shape attribute. Inside this list are NumPy arrays
which _do_ have a shape attribute. So you could say

    
    
    num_data = len(X)
    shape_of_first_array = X[0].shape
    

(You didn't explain what `dim` is supposed to represent...)

* * *

Here is an [example of face recognition](http://scipy-
lectures.github.io/advanced/scikit-learn/index.html#id9) done with PCA and a
State Vector Machine. Here is [another related example](http://scikit-
learn.org/dev/auto_examples/applications/face_recognition.html).

