forked from pyoadfe/seminars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpca.py
More file actions
71 lines (57 loc) · 1.55 KB
/
pca.py
File metadata and controls
71 lines (57 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""
Редактор Spyder
Это временный скриптовый файл.
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
from PIL import Image
import glob
dim = 10
samples = 100
k = 5
cov = np.diag(np.arange(1, dim+1, dtype=float))
gauss = multivariate_normal(mean=np.zeros(dim),
cov=cov)
data = gauss.rvs(size=samples)
print(data.shape)
u, s, v = np.linalg.svd(data)
print(s)
encoder = v[:k]
x = data[0]
delta = v.T @ (v @ x) - x
print(delta)
encoded_x = encoder @ x
x_transcoded = encoder.T @ encoded_x
print('x', x)
print('encoded', encoded_x)
print('delta', x - x_transcoded)
u, s, v = np.linalg.svd(data)
plt.figure()
plt.title('Principal components')
for i in range(3):
plt.plot(v[i], label=str(i))
plt.legend()
path = r'D:\Downloads\emoji\emoji\*.png'
images = [Image.open(f).convert('L')
for f in glob.glob(path)]
shape = images[0].size
samples = [np.array(image, dtype=float).reshape(-1)
for image in images]
data = np.vstack(samples)
plt.imshow(data[10].reshape(shape), cmap='gray')
mean = data.mean(axis=0)
data -= mean
plt.figure()
u, s, v = np.linalg.svd(data)
for i in range(5):
plt.figure()
plt.title(str(i))
plt.imshow(v[i].reshape(shape), label=str(i),
cmap='gray')
x = data[10]
encoder = v[:20]
transcoded = encoder.T @ (encoder @ x)
plt.figure()
plt.imshow(transcoded.reshape(shape) + mean.reshape(shape), cmap='gray')