-
Notifications
You must be signed in to change notification settings - Fork 2
/
SC-2c.py
66 lines (48 loc) · 1.28 KB
/
SC-2c.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
import numpy as np
from keras.datasets import mnist
import random as rd
import matplotlib.pyplot as plt
def build_distances_black(x_train):
v = []
for image in x_train :
v.append(np.sum(image*image))
return v
def euclidean_distance(img_a, img_b):
return np.sum((img_a - img_b) * (img_a - img_b))
def find_neighbors_graph(x_train, size):
v = []
for image1 in x_train:
z = []
for image2 in x_train:
z.append(euclidean_distance(image1, image2))
v.append(z)
return v
def make_laplacian(A):
i= 0
for row in A :
z = 0
for cell in row :
z = z + cell
row[i] =(-1 * z)
i = i+1
return A
(X_train, y_train), (X_test, y_test) = mnist.load_data()
i = 0
total_correct = 0
# to have faster run i slice the samples
x_train = X_train[:70]
y_train = y_train[:70]
A = find_neighbors_graph(x_train, len(x_train))
# print (len(A[69]))
L = make_laplacian(A)
print ("Laplacian matrix -------------")
print(L)
eigval, eigvec = np.linalg.eig(L)
print ("EigenValue matrix -------------")
print(eigval)
y_spec = eigvec[:, 1].copy()
y_spec[y_spec < 0] = 0
y_spec[y_spec > 0] = 1
fig, ax = plt.subplots(figsize=(6,4))
ax.scatter(y_train,build_distances_black(x_train),c=y_spec ,s=25)
plt.show()