-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLE.py
49 lines (40 loc) · 1.2 KB
/
LLE.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
from sklearn import manifold, datasets
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial.distance import cdist
import numpy as np
import time
Axes3D
n_points = 1000
X, color = datasets.samples_generator.make_s_curve(n_points, random_state=0)
# x = np.linspace(0, 10, 20)
# y = np.linspace(0, 10, 20)
# x, y = np.meshgrid(x, y)
# x = x + np.random.randn(20) * 0.01
# y = y + np.random.randn(20) * 0.01
# z = np.sin(x)
# X = np.vstack((x.flatten(), y.flatten(), z.flatten())).T
# print(X.shape)
n_neighbors = 10
tic = time.process_time()
dist = cdist(X, X)
w = np.zeros_like(dist)
tol = 1e-4
for i in range(n_points):
idx = np.argsort(dist[i])[1:n_neighbors+1]
neighbors = X[idx]
Z = neighbors - X[i]
inv = np.linalg.pinv(Z @ Z.T + np.eye(n_neighbors) * tol)
w_tmp = np.sum(inv, 1) / np.sum(inv)
w[idx, i] = w_tmp
I = np.eye(n_points)
M = (I - w) @ (I - w).T
u, s, vh = np.linalg.svd(M)
toc = time.process_time()
fig = plt.figure()
ax = fig.add_subplot(121, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral)
ax = fig.add_subplot(122)
plt.scatter(u[:, -2], u[:, -3], c=color, cmap=plt.cm.Spectral)
plt.show()
print(toc - tic)