Skip to content

Instantly share code, notes, and snippets.

@TakuTsuzuki
Last active August 29, 2015 14:22
Show Gist options
  • Save TakuTsuzuki/16b5b4583a23f58a8582 to your computer and use it in GitHub Desktop.
Save TakuTsuzuki/16b5b4583a23f58a8582 to your computer and use it in GitHub Desktop.
PCA for image analysis
import numpy as np
def entropy(X):
#caluculate Entropy of 1D-list
X = np.array(X,dtype='f')
p = X/np.sum(X)
p = np.array([x for x in p if x>0])
H = np.sum(-p*np.log2(p))
return H
def KL(X,Y):
#caluculate KL divergence of 1D-list
X = np.array(X,dtype='f')
Y = np.array(Y,dtype='f')
P = X/np.sum(X)
Q = Y/np.sum(Y)
p = np.array([x for x in P if x>0])
Q = np.array([x for x in Q if x>0])
KL = np.sum(P*np.log(P/Q))
return KL
from PIL import Image
from numpy import *
def PCA(X):
""""""
#Principal component analysis
#input:X, matrix of data
#output: egen matrix, average, covariance
""""""
# get X dimension
num_data,dim = X.shape
# centerling the data
mean_X = X.mean(axis=0)
X = X - mean_X
if dim>num_data:
# PCA - if inputs have very high dimention, Use special technics
M = dot(X,X.T) # calculate the cov
e, EV = liniag.eigh(M) # caluculate the eigenvalue and eigen vector
tmp = dot(X.T,EV).T
V = tmp[::-1]
S = sqrt(e)[::-1]
for i in range(V.shape[1]):
V[:,i] /= S
else:
# PCA - singular value decomposition
U,S,V = liniag.svd(X)
V = V[:num_data] # use first number of num_data
# V:eigenmatrix, S:cov,
return V ,S, mean_X
from PIL import Image
from numpy import *
from pylab import *
import pca
im = array(Image.open(imlist[0])) # to measure size of images, open one of the images
m,n = im.shape[0:2] # get size of images
imnbr = len(imlist) # get number of images
# reshape the stacked images to matrix
immatrix = array([array(Image.open(im)).flatten() for im in imlist],'f')
# PCA
V,S,immean = pca.pca(immatrix)
# vidualize the images
figure()
gray()
subplot(2,4,1)
imshow(immean.reshape(m,n))
for i in range(7):
subplot(2,4,i+2)
imshow(V[i].reshape(m,n))
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment