<jupyter_start><jupyter_text>Part A<jupyter_code>f = h5py.File('hw4_data1.mat','r')

faces = np.array(f['faces'][:]).T

print(faces.shape)
N, num_pixel = faces.shape
image_faces = faces.reshape(N, np.int(np.sqrt(num_pixel)), np.int(np.sqrt(num_pixel)))
print(image_faces.shape)
fig, axs = plt.subplots(3,3,figsize = (8,8))
num_examples = 9

for i in range(3):
    for j in range(3):
        axs[i,j].imshow(image_faces[i + j], cmap = 'gray')
        axs[i,j].axis('off')
    
# For plotting
import plotly.io as plt_io
import plotly.graph_objects as go
%matplotlib inline


def plot_2d(component1, component2):
    
    fig = go.Figure(data=go.Scatter(
        x = component1,
        y = component2,
        mode='markers',
        marker=dict(
            size=20,
            color=faces, #set color equal to a variable
            colorscale='Rainbow', # one of plotly colorscales
            showscale=True,
            line_width=1
        )
    ))
    fig.update_layout(margin=dict( l=100,r=100,b=100,t=100),width=2000,height=1200)                 
    fig.layout.template = 'plotly_dark'
    
    fig.show()


def plot_3d(component1,component2,component3):
    fig = go.Figure(data=[go.Scatter3d(
            x=component1,
            y=component2,
            z=component3,
            mode='markers',
            marker=dict(
                size=10,
                #color=y,                # set color to an array/list of desired values
                colorscale='Rainbow',   # choose a colorscale
                opacity=1,
                line_width=1
            )
        )])
    # tight layout
    fig.update_layout(margin=dict(l=50,r=50,b=50,t=50),width=1800,height=1000)
    fig.layout.template = 'plotly_dark'

    fig.show()
    
pca = PCA(n_components = 3)
principalComponents = pca.fit_transform(faces)
principal = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2','principal component 3'])

plot_2d(principalComponents[:, 0], principalComponents[:, 1])
latent_dim = 100
pca = PCA(n_components = latent_dim)
principalComponents = pca.fit_transform(faces)

plt.figure()
plt.plot(pca.explained_variance_ratio_)
plt.xlabel('Principal Component Index')
plt.ylabel('Proportion with Respect to Total Variance')
plt.title('Contribution of each Principal Component \n to Total Variance')
plt.grid()
plt.show()
fig, axs = plt.subplots(5,5,figsize = (12,12))

for i in range(5):
    for j in range(5):
        axs[i,j].imshow(pca.components_.shape[i + j], cmap = 'gray')
        axs[i,j].axis('off')
fig, axes = plt.subplots(5, 5, figsize=(12,12),
subplot_kw={'xticks':[], 'yticks':[]},gridspec_kw=dict(hspace=0.01, wspace=0.01))
for i, ax in enumerate(axes.flat):
    ax.imshow(pca.components_[i].reshape(32, 32).T, cmap = 'gray')<jupyter_output><empty_output><jupyter_text>Part B<jupyter_code>def pca_reconstruction(data,trained_pca,up_to):
    pca_mean = trained_pca.mean_ 
    mean_removed = data - pca_mean
    pca_components = trained_pca.components_[:up_to]
    reconstructed = mean_removed @ pca_components.T @ pca_components + pca_mean
    
    return reconstructed
faces_PCA_10 = pca_reconstruction(faces,pca,10)
faces_PCA_25 = pca_reconstruction(faces,pca,25)
faces_PCA_50 = pca_reconstruction(faces,pca,50)
fig, axes = plt.subplots(6, 6, figsize=(10,10), facecolor='white',subplot_kw={'xticks':[], 'yticks':[]})
fig.suptitle('Original Versions of the First 36 Images', fontsize='16')
fig.tight_layout(rect=[0, 0, 1, .95])
for i, ax in enumerate(axes.flat):
    ax.imshow(faces[i].reshape(32, 32).T, cmap=plt.cm.gray)
    ax.set_xlabel(i+1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14074883848045913, 'fraction_numerical': 0.036075430445476904, 'mean_word_length': 3.4634146341463414, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '44342', 'license': 'Apache-2.0', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1318, 'n_words': 241, 'path': 'HW4_Can_Kocagil/Assignment/EEE482_Can_Kocagil_21602218_Hw4.ipynb', 'repo_name': 'cankocagil/CompNeuro', 'size': '3659'}