-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_eigenvalues.py
136 lines (102 loc) · 3.42 KB
/
plot_eigenvalues.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 27 14:05:49 2020
@author: Skulpt-PC
Martin Buck, Math 123, Final project
Plot eigenvalues for different sigma
"""
import numpy as np
from scipy.io import loadmat
from scipy.io import savemat
import matplotlib.pyplot as plt
import os
gauss_mat = loadmat('datasets/gaussian.mat')
gauss_data = gauss_mat['X']
ellipse_mat = loadmat('datasets/ellipses.mat')
ellipse_data = ellipse_mat['X']
circle_mat = loadmat('datasets/circles.mat')
circle_data = circle_mat['X']
spiral_mat = loadmat('datasets/spirals.mat')
spiral_data = spiral_mat['X']
moon_mat = loadmat('datasets/moon.mat')
moon_data = moon_mat['X']
s_arr = [.001, .01, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 10, 100]
def main(data):
"""Plot eigenvalues for different values of sigma."""
eigenvalues = np.zeros((np.shape(data)[0], len(s_arr)))
eigenvectors = np.zeros((np.shape(data)[0], np.shape(data)[0], len(s_arr)))
count = 0
for s in s_arr:
folder = 'plots/sigma = ' + str(s) + '/spirals'
dataset_str = 'mat files/spiral_laplacian.mat_' + str(s) + '.mat'
if not os.path.isdir(folder):
os.makedirs(folder)
if not os.path.isfile(dataset_str):
L = laplacian(spiral_data, folder, dataset_str, s)
else:
laplacian_matrix = loadmat(dataset_str)
L = laplacian_matrix['L']
# Now compute eigenvectors of L
w, v = np.linalg.eig(L)
# get rid of imaginary parts
if s < .1:
w = np.real(w)
v = np.real(v)
# order and display eigenvalues and eigenvectors from smallest to
# largest
ind = np.argsort(w)
w = w[ind]
v = v[:, ind]
eigenvalues[:, count] = w
eigenvectors[:, :, count] = v
plot_eigenvectors(v, w, folder, s)
count += 1
# plot eigenvalues and eigenvectors
plot_eigenvalues(eigenvalues[:, 1:-2], folder)
np.savetxt(folder + '/' + 'eigenvalues' + '.csv', eigenvalues,
delimiter=',')
def laplacian(X, folder, dataset_str, s):
"""Create the Laplacian matrix."""
n = np.shape(X)[0]
W = np.zeros((n, n))
D = np.zeros((n, n))
for i in range(n):
for j in range(i, n):
w = gauss(X[i, :], X[j, :], s)
if i != j:
W[i, j] = w
W[j, i] = w
D[i, i] = np.sum(W[i, :])
# Laplacian is the difference of these two matrices
L = D - W
# visualize matrix
plt.figure()
plt.matshow(W)
plt.title('Weight matrix')
plt.savefig(folder + '/' + 'Weight matrix')
plt.show()
savemat(dataset_str, {'L': L})
return L
def plot_eigenvalues(evals, folder):
"""Plot eigenvalues of Laplacian operator."""
plt.figure()
plt.plot(evals)
plt.ylabel('Eigenvalues')
plt.title('Eigenvalues of Laplacian')
plt.savefig('Eigenvalues as a function of sigma.png')
plt.show()
def plot_eigenvectors(evecs, evals, folder, s):
"""Plot eigenvectors of Laplacian operator."""
handles = [221, 222, 223, 224]
plt.figure()
for i in range(4):
plt.subplot(handles[i])
plt.plot(evecs[:, i])
plt.legend([str(round(evals[i], 4))])
plt.savefig(folder + '/' + 'Eigenvectors')
plt.show()
def gauss(x, y, sigma):
"""Calculate the Gaussian kernel."""
return np.exp((-(np.linalg.norm(x-y)**2)/(2*(sigma**2))))
if __name__ == "__main__":
main(spiral_data)