2

I am trying to perform PCA in python 3 on a set of tiff images that are located in a single folder. I have already referenced this answer and this one but am still unsure on what the error is. All of the tiff images are the same size.

from sklearn.decomposition import PCA
import glob
import numpy as np
from scipy import misc

images = [misc.imread(path) for path in glob.glob("path/*.tif")]
images = np.asarray(images)
print(images.size)
pca = PCA(150)
pca.fit(images)

I first upload them all into a np.array and try to perform PCA, but I keep getting the error below.

File "Users/.../anaconda3/lib/python3.7/site-packages/numpy/core/numeric.py", line 538, in asarray return array(a, dtype, copy=False, order=order)

ValueError: setting an array element with a sequence.

Why do I keep getting this error and how should I perform PCA on these tiff images?

john
  • 619
  • 9
  • 24
  • 1
    This images have same size? – Grzegorz Bokota Jul 18 '19 at 18:36
  • as for me it can works with 1-dimensional array - array with single values - but you have 2-dimensional array or even n-dimensional if you get that every image has 3-dimensions (x, y, RGB). And these dimensions create `sequence` in array. OR maybe you have to flatten every image so it would use only one dimension ? – furas Jul 18 '19 at 19:00
  • 1
    if I use `imread(path).flatten()` and `PCA()` without 150 then it starts working for me. I don't need even `np.asarray()` – furas Jul 18 '19 at 20:08
  • @GrzegorzBokota yes they all have the same size – john Aug 24 '19 at 20:46

0 Answers0