Question: In Python: # PCA Tree Example %matplotlib inline import matplotlib.pyplot as plt import imageio import numpy as np import warnings from PIL import Image from

 In Python: # PCA Tree Example %matplotlib inline import matplotlib.pyplot as

In Python:

# PCA Tree Example %matplotlib inline import matplotlib.pyplot as plt import imageio import numpy as np import warnings from PIL import Image from urllib.request import urlopen

warnings.filterwarnings("ignore", category=RuntimeWarning)

url = 'https://raw.githubusercontent.com/joefoxva1/CDS468/master/Lecture_6_7_8_9/D72.png' amg = imageio.imread(url, as_gray=True)

cv = np.cov(amg.transpose()) evl, evc = np.linalg.eig(cv) ndata = np.dot(amg, evc[:,:2])

plt.title('Original Image') plt.axis('off') plt.imshow(amg, cmap='gray') plt.show()

def ScrambleImage(mgdata): sdata = mgdata + 0 np.random.shuffle(sdata) dists = np.sqrt(((mgdata[0]-sdata).sum(1))) seedrow = (dists==0).nonzero()[0] return sdata, seedrow

def Project(data): """data in rows returns matrix: projected data in rows""" cv = np.cov(data.transpose()) evl, evc = np.linalg.eig(cv) ndata = np.dot(data, evc) return ndata

sdata, seedrow = ScrambleImage(amg) ndata = Project(sdata)

""" plt.title('Scrambled Image') plt.axis('off') plt.imshow(ndata, cmap='gray') plt.show() """

def Unscramble(sdata, seedrow, ndata): V,H = sdata.shape udata = np.zeros((V,H)) udata[0] = sdata[seedrow] + 0 unused = list(range(V)) unused.remove(seedrow) nndata = ndata + 0 k = seedrow for i in range( 1, V ): dist = np.sqrt(((nndata[k]-nndata[unused])**2).sum(1)) ag = dist.argsort() k = unused[ag[0]] udata[i] = sdata[k] unused.remove( k ) return udata

udata = Unscramble(sdata, seedrow, ndata[:,:7])

plt.title('First 7 Components Image') plt.axis('off') plt.imshow(udata, cmap='gray') plt.show()

2. Using the PCA Tree Example above, calculate and display the first 1st, 3rd, and 10th dimensions/components of the image. Discuss the differences

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!