0% found this document useful (0 votes)
29 views23 pages

PCA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views23 pages

PCA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 23

# Installing Pillow Libraries

!pip install scikit-learn matplotlib Pillow


Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: scikit-learn in
/home/nasir/.local/lib/python3.10/site-packages (1.3.0)
Requirement already satisfied: matplotlib in
/home/nasir/.local/lib/python3.10/site-packages (3.7.2)
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages (9.0.1)
Requirement already satisfied: joblib>=1.1.1 in
/home/nasir/.local/lib/python3.10/site-packages (from scikit-learn) (1.3.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in
/home/nasir/.local/lib/python3.10/site-packages (from scikit-learn) (3.2.0)
Requirement already satisfied: scipy>=1.5.0 in
/home/nasir/.local/lib/python3.10/site-packages (from scikit-learn) (1.11.2)
Requirement already satisfied: numpy>=1.17.3 in
/home/nasir/.local/lib/python3.10/site-packages (from scikit-learn) (1.25.2)
Requirement already satisfied: kiwisolver>=1.0.1 in
/home/nasir/.local/lib/python3.10/site-packages (from matplotlib) (1.4.4)
Requirement already satisfied: fonttools>=4.22.0 in
/home/nasir/.local/lib/python3.10/site-packages (from matplotlib) (4.42.1)
Requirement already satisfied: packaging>=20.0 in
/home/nasir/.local/lib/python3.10/site-packages (from matplotlib) (23.1)
Requirement already satisfied: pyparsing<3.1,>=2.3.1 in /usr/lib/python3/dist-
packages (from matplotlib) (2.4.7)
Requirement already satisfied: cycler>=0.10 in
/home/nasir/.local/lib/python3.10/site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: python-dateutil>=2.7 in
/home/nasir/.local/lib/python3.10/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: contourpy>=1.0.1 in
/home/nasir/.local/lib/python3.10/site-packages (from matplotlib) (1.1.0)
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from
python-dateutil>=2.7->matplotlib) (1.16.0)
import numpy as np
import os
from random import randrange
from matplotlib import pyplot as plt
from PIL import Image
cfw_dict = {
'Amitabhbachan': 0,
'AamirKhan': 1,
'DwayneJohnson': 2,
'AishwaryaRai': 3,
'BarackObama': 4,
'NarendraModi': 5,
'ManmohanSingh': 6,
'VladimirPutin': 7
}

opt = {
'image_size': 32,
'is_grayscale': False,
'val_split': 0.75
}
def load_image(path):
im = Image.open(path).convert('L' if opt['is_grayscale'] else 'RGB')
im = im.resize((opt['image_size'],opt['image_size']))
im = np.array(im)
im = im/256
return im
def display_images(imgs,classes,row=1,col=2,w=32,h=32):

fig=plt.figure(figsize=(8, 8))
for i in range(1, col*row +1):
img = imgs[i-1]
fig.add_subplot(row, col, i)

if opt['is_grayscale']:
plt.imshow(img , cmap='gray')
else:
plt.imshow(img)

plt.title("Class:{}".format(classes[i-1]))
plt.axis('off')
plt.show()

def load_data(dir_path):

image_list = []
y_list = []
label_dict = cfw_dict
for filename in sorted(os.listdir(dir_path)):
if filename.endswith(".png"):
im = load_image(os.path.join(dir_path,filename))
y = filename.split('_')[0]
y = label_dict[y]
image_list.append(im)
y_list.append(y)
else:
continue
image_list = np.array(image_list)
y_list = np.array(y_list)
print("Dataset shape:",image_list.shape)
print("Label shape:",y_list.shape)

return image_list,y_list
dirpath = '/home/nasir/python-ws/assingments/assingment2/SMAI-Dataset/IIIT-CFW/
IIIT-CFW'
X,y = load_data(dirpath)
Dataset shape: (672, 32, 32, 3)
Label shape: (672,)
X[0].dtype
dtype('float64')
N,H,W = X.shape[0:3]
C = 1 if opt['is_grayscale'] else X.shape[3]

ind = np.random.randint(0,y.shape[0],6)
display_images(X[ind,...],y[ind], row=2,col=3)

X[0]
array([[[0.00390625, 0. , 0. ],
[0. , 0.00390625, 0.00390625],
[0. , 0.01171875, 0.015625 ],
...,
[0.0234375 , 0.046875 , 0.01171875],
[0.02734375, 0.0390625 , 0.0078125 ],
[0.0234375 , 0.03515625, 0.0078125 ]],

[[0. , 0.01171875, 0.015625 ],


[0.00390625, 0.00390625, 0.0078125 ],
[0.0234375 , 0.0078125 , 0.01171875],
...,
[0.05859375, 0.08984375, 0.02734375],
[0.05859375, 0.08984375, 0.02734375],
[0.0546875 , 0.0859375 , 0.0234375 ]],

[[0.0078125 , 0.015625 , 0.0234375 ],


[0.015625 , 0. , 0.00390625],
[0.09375 , 0.0234375 , 0.015625 ],
...,
[0.09375 , 0.13671875, 0.046875 ],
[0.0859375 , 0.1328125 , 0.04296875],
[0.08203125, 0.1328125 , 0.04296875]],

...,

[[0.01171875, 0.0078125 , 0.00390625],


[0.19921875, 0.10546875, 0.06640625],
[0.69140625, 0.45703125, 0.34375 ],
...,
[0.875 , 0.59765625, 0.28125 ],
[0.84765625, 0.5703125 , 0.3125 ],
[0.828125 , 0.55859375, 0.34375 ]],

[[0.3515625 , 0.203125 , 0.1171875 ],


[0.6171875 , 0.41796875, 0.26171875],
[0.78125 , 0.50390625, 0.29296875],
...,
[0.890625 , 0.60546875, 0.33203125],
[0.859375 , 0.5859375 , 0.30859375],
[0.8359375 , 0.5703125 , 0.2890625 ]],

[[0.7421875 , 0.46875 , 0.26953125],


[0.78515625, 0.49609375, 0.2578125 ],
[0.8046875 , 0.5 , 0.22265625],
...,
[0.92578125, 0.6171875 , 0.37109375],
[0.90234375, 0.60546875, 0.33203125],
[0.859375 , 0.578125 , 0.27734375]]])

plt.imshow(X[0])
<matplotlib.image.AxesImage at 0x7f9ba3bb0250>

X=X.reshape(672, 3072)
y=X[0]
y1=y.reshape(32, 32, 3)
plt.imshow(y1)
<matplotlib.image.AxesImage at 0x7f9ba1833520>

len(X)
672
data=X
print(X)
[[0.00390625 0. 0. ... 0.859375 0.578125 0.27734375]
[0.00390625 0. 0.08203125 ... 0.28515625 0.24609375 0.27734375]
[0.9765625 0.98046875 0.9609375 ... 0.64453125 0.8203125 0.45703125]
...
[0.95703125 0.7421875 0.54296875 ... 0.9921875 0.99609375 0.9765625 ]
[0.96484375 0.98046875 0.28125 ... 0.50390625 0.51171875 0.19140625]
[0.796875 0.8203125 0.8046875 ... 0.2109375 0.44140625 0.52734375]]
import pandas as pd
y=pd.DataFrame(X)
y
0 1 2 3 4 5 6 \
0 0.003906 0.000000 0.000000 0.000000 0.003906 0.003906 0.000000
1 0.003906 0.000000 0.082031 0.003906 0.000000 0.070312 0.007812
2 0.976562 0.980469 0.960938 0.980469 0.984375 0.945312 0.976562
3 0.656250 0.656250 0.687500 0.652344 0.656250 0.683594 0.644531
4 0.000000 0.546875 0.562500 0.000000 0.542969 0.542969 0.074219
.. ... ... ... ... ... ... ...
667 0.570312 0.472656 0.417969 0.292969 0.167969 0.093750 0.351562
668 0.992188 0.992188 0.996094 0.996094 0.988281 0.996094 0.996094
669 0.957031 0.742188 0.542969 0.949219 0.746094 0.550781 0.949219
670 0.964844 0.980469 0.281250 0.972656 0.988281 0.285156 0.968750
671 0.796875 0.820312 0.804688 0.789062 0.812500 0.796875 0.851562

7 8 9 ... 3062 3063 3064 \


0 0.011719 0.015625 0.003906 ... 0.386719 0.925781 0.617188
1 0.000000 0.054688 0.011719 ... 0.171875 0.105469 0.093750
2 0.984375 0.925781 0.996094 ... 0.578125 0.789062 0.925781
3 0.660156 0.675781 0.648438 ... 0.437500 0.324219 0.308594
4 0.503906 0.500000 0.273438 ... 0.292969 0.906250 0.410156
.. ... ... ... ... ... ... ...
667 0.195312 0.093750 0.492188 ... 0.664062 0.917969 0.730469
668 0.988281 0.996094 0.996094 ... 0.812500 0.609375 0.601562
669 0.750000 0.562500 0.949219 ... 0.968750 0.996094 0.984375
670 0.988281 0.277344 0.968750 ... 0.324219 0.960938 0.964844
671 0.875000 0.859375 0.855469 ... 0.570312 0.242188 0.507812

3065 3066 3067 3068 3069 3070 3071


0 0.371094 0.902344 0.605469 0.332031 0.859375 0.578125 0.277344
1 0.113281 0.246094 0.218750 0.246094 0.285156 0.246094 0.277344
2 0.546875 0.753906 0.906250 0.531250 0.644531 0.820312 0.457031
3 0.445312 0.324219 0.308594 0.445312 0.324219 0.308594 0.445312
4 0.363281 0.921875 0.449219 0.421875 0.910156 0.453125 0.441406
.. ... ... ... ... ... ... ...
667 0.644531 0.886719 0.703125 0.621094 0.804688 0.621094 0.542969
668 0.546875 0.957031 0.957031 0.917969 0.992188 0.992188 0.972656
669 0.960938 0.996094 0.992188 0.972656 0.992188 0.996094 0.976562
670 0.320312 0.992188 0.996094 0.429688 0.503906 0.511719 0.191406
671 0.601562 0.101562 0.343750 0.433594 0.210938 0.441406 0.527344

[672 rows x 3072 columns]


class Pca():
def __init__(self,X, p_comps=0):
self.img = None
self.X=X
self.p_comps = p_comps
sel_eigen_vectors = self.getvectors()
self.finalimg = self.reducedimage(sel_eigen_vectors)

def getvectors(self):
#calculating the mean
X_mean=np.mean(self.X,axis=0)
#mean centering the data
X_meaned=X-X_mean
print(X_meaned.shape)
# Compute the covariance matrix
cov_matrix = np.dot(X_meaned.T,X_meaned)
# Get the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
# Sort the eigenvectors based on eigenvalues
sorted_indices = np.argsort(eigenvalues)[::-1]
sorted_eigenvectors = eigenvectors[:, sorted_indices]
# Choose the desired number of principal components
num_components = self.p_comps
selected_eigenvectors = sorted_eigenvectors[:, :num_components]
return selected_eigenvectors

def reducedimage(self,sel_eigen):
# Project the image onto the selected eigenvectors
reduced_image = np.dot(sel_eigen.T,X.T)
return reduced_image
pca1=Pca(X,1000)
(672, 3072)
pca1_df=pd.DataFrame(pca1.finalimg)
pca1_df
0 1 2 \
0 -17.347318+0.000000j -21.032250+0.000000j -30.016021+0.000000j
1 12.921849+0.000000j 21.694817+0.000000j 7.841285+0.000000j
2 -2.303765+0.000000j -2.031538+0.000000j -3.672945+0.000000j
3 -7.927858+0.000000j -3.187595+0.000000j -6.401104+0.000000j
4 4.367666+0.000000j 3.044460+0.000000j 5.532498+0.000000j
.. ... ... ...
995 0.013505-0.012052j 0.013505-0.012052j 0.013505-0.012052j
996 0.001471+0.003997j 0.001471+0.003997j 0.001471+0.003997j
997 0.001471-0.003997j 0.001471-0.003997j 0.001471-0.003997j
998 0.011971-0.007727j 0.011971-0.007727j 0.011971-0.007727j
999 0.011971+0.007727j 0.011971+0.007727j 0.011971+0.007727j

3 4 5 \
0 -28.662534+0.000000j -22.801657+0.000000j -34.877382+0.000000j
1 12.228595+0.000000j 12.725849+0.000000j 13.395109+0.000000j
2 -1.210359+0.000000j -9.264425+0.000000j -11.560975+0.000000j
3 -3.125037+0.000000j -2.366442+0.000000j -3.183381+0.000000j
4 7.434119+0.000000j 5.799617+0.000000j 6.497211+0.000000j
.. ... ... ...
995 0.013505-0.012052j 0.013505-0.012052j 0.013505-0.012052j
996 0.001471+0.003997j 0.001471+0.003997j 0.001471+0.003997j
997 0.001471-0.003997j 0.001471-0.003997j 0.001471-0.003997j
998 0.011971-0.007727j 0.011971-0.007727j 0.011971-0.007727j
999 0.011971+0.007727j 0.011971+0.007727j 0.011971+0.007727j

6 7 8 \
0 -37.541784+0.000000j -28.97449+0.00000j -33.228114+0.000000j
1 1.213471+0.000000j 4.896505+0.000000j 10.467162+0.000000j
2 -6.044526+0.000000j -8.382431+0.000000j -1.671362+0.000000j
3 -0.786509+0.000000j 3.510783+0.000000j 0.382807+0.000000j
4 6.364132+0.000000j 5.524970+0.000000j 1.530844+0.000000j
.. ... ... ...
995 0.013505-0.012052j 0.013505-0.012052j 0.013505-0.012052j
996 0.001471+0.003997j 0.001471+0.003997j 0.001471+0.003997j
997 0.001471-0.003997j 0.001471-0.003997j 0.001471-0.003997j
998 0.011971-0.007727j 0.011971-0.007727j 0.011971-0.007727j
999 0.011971+0.007727j 0.011971+0.007727j 0.011971+0.007727j

9 ... 662 663 \


0 -32.096042+0.000000j ... -42.789324+0.000000j -39.328444+0.000000j
1 -0.919435+0.000000j ... 5.358701+0.000000j 10.273680+0.000000j
2 -8.246350+0.000000j ... 1.537155+0.000000j -2.108079+0.000000j
3 -1.955174+0.000000j ... -2.614687+0.000000j -2.765994+0.000000j
4 2.895818+0.000000j ... -1.546653+0.000000j 2.717463+0.000000j
.. ... ... ... ...
995 0.013505-0.012052j ... 0.013505-0.012052j 0.013505-0.012052j
996 0.001471+0.003997j ... 0.001471+0.003997j 0.001471+0.003997j
997 0.001471-0.003997j ... 0.001471-0.003997j 0.001471-0.003997j
998 0.011971-0.007727j ... 0.011971-0.007727j 0.011971-0.007727j
999 0.011971+0.007727j ... 0.011971+0.007727j 0.011971+0.007727j

664 665 666 \


0 -33.062602+0.000000j -24.942517+0.000000j -38.251908+0.000000j
1 7.665273+0.000000j 16.767108+0.000000j 7.366036+0.000000j
2 2.022610+0.000000j 1.019133+0.000000j 9.611906+0.000000j
3 -2.796347+0.000000j -6.088712+0.000000j 1.666615+0.000000j
4 7.916669+0.000000j 7.211482+0.000000j 8.277256+0.000000j
.. ... ... ...
995 0.013505-0.012052j 0.013505-0.012052j 0.013505-0.012052j
996 0.001471+0.003997j 0.001471+0.003997j 0.001471+0.003997j
997 0.001471-0.003997j 0.001471-0.003997j 0.001471-0.003997j
998 0.011971-0.007727j 0.011971-0.007727j 0.011971-0.007727j
999 0.011971+0.007727j 0.011971+0.007727j 0.011971+0.007727j

667 668 669 \


0 -29.535049+0.000000j -42.258114+0.000000j -43.466899+0.000000j
1 10.974146+0.000000j 7.742181+0.000000j 9.861553+0.000000j
2 -0.704849+0.000000j 0.226484+0.000000j -1.787527+0.000000j
3 -6.756405+0.000000j -2.842829+0.000000j -4.252755+0.000000j
4 -0.546578+0.000000j 5.572103+0.000000j 2.940502+0.000000j
.. ... ... ...
995 0.013505-0.012052j 0.013505-0.012052j 0.013505-0.012052j
996 0.001471+0.003997j 0.001471+0.003997j 0.001471+0.003997j
997 0.001471-0.003997j 0.001471-0.003997j 0.001471-0.003997j
998 0.011971-0.007727j 0.011971-0.007727j 0.011971-0.007727j
999 0.011971+0.007727j 0.011971+0.007727j 0.011971+0.007727j

670 671
0 -31.785884+0.000000j -39.974815+0.000000j
1 5.976527+0.000000j 6.554488+0.000000j
2 -1.000672+0.000000j -0.959378+0.000000j
3 -6.426645+0.000000j -4.059933+0.000000j
4 10.063658+0.000000j 2.951960+0.000000j
.. ... ...
995 0.013505-0.012052j 0.013505-0.012052j
996 0.001471+0.003997j 0.001471+0.003997j
997 0.001471-0.003997j 0.001471-0.003997j
998 0.011971-0.007727j 0.011971-0.007727j
999 0.011971+0.007727j 0.011971+0.007727j

[1000 rows x 672 columns]


for i in range(X.shape[0]):
image_vector = pca1.finalimg[:, i]
image_vector=np.clip(image_vector, 0, 255).astype(np.uint8)
image_matrix = image_vector.reshape((100,10)) # Reshape to the original image
dimensions
plt.imshow(image_matrix) # You might need to adjust the color map
plt.show() # Display the image
/tmp/ipykernel_13247/898492757.py:3: ComplexWarning: Casting complex values to real
discards the imaginary part
image_vector=np.clip(image_vector, 0, 255).astype(np.uint8)
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

# Step 1: Data Preprocessing


# Load your dataset as a numpy array, assuming you have loaded it into a variable
`data`.
# Standardize the data.
data_mean = np.mean(data, axis=0)
data_std = np.std(data, axis=0)
standardized_data = (data - data_mean) / data_std

# Step 2: Covariance Matrix Calculation


cov_matrix = np.cov(standardized_data, rowvar=False)

# Step 3: Eigenvalue and Eigenvector Computation


eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)

# Step 4: Sort and Select Principal Components


sorted_indices = np.argsort(eigenvalues)[::-1]
eigenvalues = eigenvalues[sorted_indices]
eigenvectors = eigenvectors[:, sorted_indices]

# Determine the number of principal components to explain >90% variance


explained_variance_ratio = np.cumsum(eigenvalues) / np.sum(eigenvalues)
print(np.sum(eigenvectors))
num_components_90_percent = np.argmax(explained_variance_ratio > 0.90) + 1

# Step 5: Projection onto Principal Components


pca_projection = np.dot(standardized_data,
eigenvectors[:, :num_components_90_percent])
print(pca_projection.shape)

# Step 6: Cumulative Explained Variance Calculation


cumulative_explained_variance =
explained_variance_ratio[:num_components_90_percent]

# Step 7: Visualization
# Visualize the PCA-transformed images
fig, axes = plt.subplots(1, num_components_90_percent, figsize=(15, 2))
for i in range(num_components_90_percent):
# Inverse transform from PCA space to original space
pca_image = np.dot(pca_projection[:, i], eigenvectors[:, i].T) * data_std +
data_mean
pca_image = np.clip(pca_image, 0, 255).astype(np.uint8)
pca_image = pca_image.reshape(32, 32, 3)

# Display the PCA-transformed image


axes[i].imshow(pca_image)
axes[i].set_title(f"PC {i+1}")

plt.show()

# Print cumulative explained variance


print("Cumulative Explained Variance:")
print(cumulative_explained_variance)
43.74320223283074
(672, 124)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[46], line 41
38 fig, axes = plt.subplots(1, num_components_90_percent, figsize=(15, 2))
39 for i in range(num_components_90_percent):
40 # Inverse transform from PCA space to original space
---> 41 pca_image = np.dot(pca_projection[:, i], eigenvectors[:, i].T) *
data_std + data_mean
42 pca_image = np.clip(pca_image, 0, 255).astype(np.uint8)
43 pca_image = pca_image.reshape(32, 32, 3)

ValueError: shapes (672,) and (3072,) not aligned: 672 (dim 0) != 3072 (dim 0)

import numpy as np
from PIL import Image
from PIL import Image
import numpy as np

# Example: Create an array containing NumPy arrays representing RGB images


# Assume the input array is already in float64 format
image_array_float64 = [
np.random.rand(100, 100, 3), # Example RGB image with float64 data type
np.random.rand(100, 100, 3), # Another example RGB image with float64 data
type
# Add more images here...
]

# Convert each element in the array to Pillow Image objects and store them in a new
array
gray_image_array = []

for rgb_image in X:
# Convert RGB image to Pillow Image
pil_rgb_image = Image.fromarray((rgb_image * 255).astype(np.uint8)) # Convert
back to uint8 for Pillow

# Convert to grayscale and explicitly set the data type to float32 or float64
pil_gray_image = pil_rgb_image.convert('L')
gray_image = np.array(pil_gray_image, dtype=np.float64) # Or dtype=np.float32
if you prefer

# Normalize pixel values to the range [0, 1] for float data types
gray_image /= 255.0

# Append the grayscale NumPy array to the new list


gray_image_array.append(gray_image)

# Convert the list of grayscale NumPy arrays to a NumPy array


gray_image_array = np.stack(gray_image_array)

# 'gray_image_array' is now a NumPy array containing grayscale images with float32


or float64 data type
print(gray_image_array)
[[[0. 0. 0.00784314 ... 0.03137255 0.02745098 0.02352941]
[0.00784314 0. 0.00784314 ... 0.07058824 0.07058824 0.06666667]
[0.01176471 0.00392157 0.03921569 ... 0.10980392 0.10588235 0.10196078]
...
[0.00392157 0.1254902 0.51372549 ... 0.64313725 0.62352941 0.61176471]
[0.23529412 0.45882353 0.56078431 ... 0.65882353 0.63529412 0.61568627]
[0.5254902 0.55294118 0.55686275 ... 0.67843137 0.6627451 0.62745098]]

[[0.00784314 0.00784314 0.00784314 ... 0.16862745 0.12156863 0.20784314]


[0.00784314 0.00784314 0.00784314 ... 0.16470588 0.11764706 0.13333333]
[0.00784314 0.00784314 0.00392157 ... 0.14117647 0.14117647 0.14901961]
...
[0.01568627 0.02352941 0.17647059 ... 0.11764706 0.14509804 0.14901961]
[0.01960784 0.42352941 0.83921569 ... 0.08627451 0.1372549 0.14901961]
[0.10196078 0.72156863 0.87058824 ... 0.09411765 0.22745098 0.25882353]]

[[0.97647059 0.98039216 0.97647059 ... 0.74509804 0.65098039 0.71372549]


[0.94117647 0.94901961 0.96862745 ... 0.20784314 0.63529412 0.8 ]
[0.74117647 0.65098039 0.3254902 ... 0.83921569 0.99607843 0.96470588]
...
[0.87843137 0.89803922 0.82745098 ... 0.8627451 0.85490196 0.78823529]
[0.88235294 0.88627451 0.83529412 ... 0.85882353 0.84705882 0.85098039]
[0.88235294 0.87843137 0.83137255 ... 0.83921569 0.81568627 0.7254902 ]]

...

[[0.78431373 0.78431373 0.78823529 ... 0.87843137 0.85098039 0.83137255]


[0.81176471 0.80784314 0.81176471 ... 0.90196078 0.87843137 0.85882353]
[0.82352941 0.82745098 0.82745098 ... 0.91372549 0.89411765 0.88235294]
...
[0.97254902 0.97254902 0.97647059 ... 0.99215686 0.99215686 0.99215686]
[0.97254902 0.97254902 0.97647059 ... 0.98431373 0.98823529 0.99215686]
[0.97254902 0.97254902 0.97254902 ... 0.98431373 0.99215686 0.99215686]]

[[0.89411765 0.90196078 0.90196078 ... 0.91372549 0.91372549 0.89803922]


[0.90588235 0.90588235 0.90588235 ... 0.90588235 0.90588235 0.89803922]
[0.90588235 0.89411765 0.90588235 ... 0.90588235 0.90980392 0.90980392]
...
[0.9254902 0.47058824 0.05882353 ... 0.91372549 0.90588235 0.89019608]
[0.86666667 0.34117647 0.07058824 ... 0.89411765 0.90588235 0.89803922]
[0.83921569 0.21960784 0.05490196 ... 0.89019608 0.92941176 0.47058824]]

[[0.81176471 0.80392157 0.86666667 ... 0.95686275 0.95686275 0.96078431]


[0.85490196 0.8745098 0.88235294 ... 0.95686275 0.95686275 0.96078431]
[0.89803922 0.89411765 0.89019608 ... 0.94901961 0.95294118 0.95686275]
...
[0.70196078 0.82745098 0.87843137 ... 0.62745098 0.55686275 0.55686275]
[0.54901961 0.51764706 0.52941176 ... 0.43529412 0.40784314 0.40784314]
[0.58039216 0.43529412 0.37647059 ... 0.43529412 0.27843137 0.38039216]]]
gray_exm_imp_vis=gray_image_array[0,:]
gray_exm_imp_vis=gray_exm_imp_vis.reshape(32,32)
plt.imshow(gray_exm_imp_vis)
<matplotlib.image.AxesImage at 0x7f9b9cc6a1d0>

#code by phind
import numpy as np
import matplotlib.pyplot as plt

# Assume we have a variable `images` where each element is a 2D array representing


an image
# e.g., images = [image1, image2, ..., imageN] where image1 is an 8x8 numpy array
# Step 1: Standardize the dataset
images_standardized = (images_flattened - np.mean(images_flattened, axis=0)) /
np.std(images_flattened, axis=0)

# Step 2: Calculate the covariance matrix


cov_matrix = np.cov(images_standardized, rowvar=False)

# Step 3: Calculate eigenvalues and eigenvectors of the covariance matrix


eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)

# Step 4: Sort eigenvalues and corresponding eigenvectors, in decreasing order


sorted_indices = np.argsort(eigenvalues)[::-1]
eigenvalues_sorted = eigenvalues[sorted_indices]
eigenvectors_sorted = eigenvectors[:, sorted_indices]

# Step 5: Select the first k eigenvectors, which will be the new k dimensions
k = 2
eigenvectors_subset = eigenvectors_sorted[:, :k]

# Step 6: Transform the data


images_transformed = np.dot(images_standardized, eigenvectors_subset)

plt.scatter(images_transformed[:, 0], images_transformed[:, 1], cmap='viridis')


plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Transformed Images')
plt.colorbar()
plt.show()
# Step 1: Multiply the transformed images with the transpose of the eigenvectors
images_inverse = np.dot(images_transformed, eigenvectors_subset.T)

# Step 2: Unstandardize the data by multiplying with the standard deviation and
adding the mean
images_reconstructed = images_inverse * np.std(images_flattened, axis=0) +
np.mean(images_flattened, axis=0)
# Plot the first few images
fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(10, 5))
for i, ax in enumerate(axes.flatten()):
# Reshape the image to its original shapeThis task requires you to implement
the EM algorithm for GMM and perform
clustering operations on a given dataset(s). The list of subtasks is given below.
• Find the parameters of GMM associated with the customer-dataset, us-
ing the EM method. Vary the number of components, and observe the
results. Implement GMM in a class which has the routines to fit data (e.g.
gmm.fit(data, number of clusters)), a routine to obtain the parameters, a
routine to calculate the likelihoods for a given set of samples and a routine
to obtain the membership values of data samples.This task requires you to implement
the EM algorithm for GMM and perform
clustering operations on a given dataset(s). The list of subtasks is given below.
• Find the parameters of GMM associated with the customer-dataset, us-
ing the EM method. Vary the number of components, and observe the
results. Implement GMM in a class which has the routines to fit data (e.g.
gmm.fit(data, number of clusters)), a routine to obtain the parameters, a
routine to calculate the likelihoods for a given set of samples and a routine
to obtain the membership values of data samples.This task requires you to implement
the EM algorithm for GMM and perform
clustering operations on a given dataset(s). The list of subtasks is given below.
• Find the parameters of GMM associated with the customer-dataset, us-
ing the EM method. Vary the number of components, and observe the
results. Implement GMM in a class which has the routines to fit data (e.g.
gmm.fit(data, number of clusters)), a routine to obtain the parameters, a
routine to calculate the likelihoods for a given set of samples and a routine
to obtain the membership values of data samples.
image = images_reconstructed[i].reshape(32,32)

# Plot the image


ax.imshow(image, cmap='gray')
ax.axis('off')

plt.tight_layout()
plt.show()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[55], line 7
3 import matplotlib.pyplot as plt
5 # Assume we have a variable `images` where each element is a 2D array
representing an image
6 # e.g., images = [image1, image2, ..., imageN] where image1 is an 8x8 numpy
array
----> 7 images_flattened = images_flattened = np.array([img.flatten().convert('L')
for img in X])
8 # Step 1: Standardize the dataset
9 images_standardized = (images_flattened - np.mean(images_flattened,
axis=0)) / np.std(images_flattened, axis=0)

Cell In[55], line 7, in <listcomp>(.0)


3 import matplotlib.pyplot as plt
5 # Assume we have a variable `images` where each element is a 2D array
representing an image
6 # e.g., images = [image1, image2, ..., imageN] where image1 is an 8x8 numpy
array
----> 7 images_flattened = images_flattened = np.array([img.flatten().convert('L')
for img in X])
8 # Step 1: Standardize the dataset
9 images_standardized = (images_flattened - np.mean(images_flattened,
axis=0)) / np.std(images_flattened, axis=0)

AttributeError: 'numpy.ndarray' object has no attribute 'convert'

You might also like