user2229953 user2229953 - 2 months ago 16
Python Question

Principal Component Analysis (PCA) - accessing shape

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

When you do

[np.asarray(Image.open(os.path.join(dirname, fn))) for fn in os.listdir(dirname)]

It would return an array, which is stored in local variable X

You are trying to extract variables from a list, and hence the error.

The individual elements within X have the shape attributes. Hence, you need to do something like this (modify it according to the datastructure shape is defined as )

dim0 = X[0].shape and so on