arrays - Python - Principal component analysis (PCA) error -
i trying make principal component analysis (pca) using python. here code:
import os pil import image import numpy np import glob matplotlib.mlab import pca #step1: put database images 3d array filenames = glob.glob('c:\\users\\karim\\downloads\\att_faces\\new folder/*.pgm') filenames.sort() img = [image.open(fn).convert('l') fn in filenames] images = np.dstack([np.array(im) im in img]) # step2: create 2d flattened version of 3d input array d1,d2,d3 = images.shape b = np.zeros([d1,d2*d3]) in range(len(images)): b[i] = images[i].flatten() #step 3: pca results = pca(b) results.wt
but getting error runtimeerror: assume data in organized numrows>numcols
i tried replacing b = np.zeros([d1,d2*d3])
b = np.zeros([d2*d3, d1])
got valueerror: not broadcast input array shape (2760) shape (112)
can me?
if change b = np.zeros([d2*d3, d1])
should change loop afterwards otherwise try put d1
dimention array d2*d3
one.
you should rid of second error doing
you can transpose b
# step2: create 2d flattened version of 3d input array d1,d2,d3 = images.shape b = np.empty([d1,d2*d3]) #if know filling whole array it's faster using np.zeros or np.ones i, im in enumerate(images): b[i,:] = im.flatten() #step 3: pca results = pca(b.t)
i've substituted loop think better version: in implementation first find dimension of images
, create list of integers loop on , re-access images
. enumerate
returns iterator couple (index, value). advantages returns elements need, , don't have access images
directly in loop.
probably don't need create images
, don't know pil
, there can't you. in case, can dimensions like
d1,d2,d3 = len(img), img[0].shape
edit
you if want can convert content of files numpy when reading them.
for records, numpy.asarray
.
Comments
Post a Comment