-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_synthetic.py
More file actions
64 lines (54 loc) · 2.17 KB
/
example_synthetic.py
File metadata and controls
64 lines (54 loc) · 2.17 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
import numpy as np
from scipy.spatial.distance import euclidean
from scipy.spatial.distance import mahalanobis
from diffusionmap import DiffusionMap
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import matplotlib.cm as cm
if __name__ == '__main__':
# Load data
covariance = [[100, 0.5, 0.5],
[0.5, 50, 0.5],
[0.5, 0.5, 10]]
data = np.concatenate([np.random.multivariate_normal([0,0,0], covariance, 1000),
np.random.multivariate_normal([100,0,0], covariance, 1000),
np.random.multivariate_normal([0,50,0], covariance, 1000)])
# Plot luminance
plt.imshow(data.reshape((60, 50, 3)), origin='lower')
plt.savefig('synthetic.png')
plt.show()
# # Diffusion map clustering based on Euclidean distances
# e_dm = DiffusionMap(data, kernel_params={'eps': 1e3}, neighbors=500)
# e_w, e_v = e_dm.map(3)
#
# kmeans = KMeans(n_clusters=3, n_init=100)
# kmeans.fit(e_v)
# e_y = kmeans.predict(e_v)
#
# plt.imshow(e_y.reshape((60, 50)), origin='lower')
# plt.savefig('synthetic_euclidean.png')
# plt.show()
#
# # Diffusion map clustering based on Mahalanobis distances with overall covariances
# inv_cov = np.linalg.inv(np.cov(data, rowvar=False))
# def mdistance(x, y):
# return mahalanobis(x, y, VI=inv_cov)
# m_dm = DiffusionMap(data, kernel_params={'eps': 1e3, 'distance': mdistance}, neighbors=500)
# m_w, m_v = m_dm.map(3)
#
# kmeans = KMeans(n_clusters=3, n_init=100)
# kmeans.fit(m_v)
# m_y = kmeans.predict(m_v)
#
# plt.imshow(m_y.reshape((60, 50)), origin='lower')
# plt.savefig('synthetic_mahalanobis.png')
# plt.show()
# Diffusion map clustering based on Mahalanobis distances with local covariances
lm_dm = DiffusionMap(data, kernel_params={'eps': 1e3}, neighbors=500)
lm_w, lm_v = lm_dm.map(3, local_mahalanobis=True, clusters=25)
kmeans = KMeans(n_clusters=3, n_init=100)
kmeans.fit(lm_v)
lm_y = kmeans.predict(lm_v)
plt.imshow(lm_y.reshape((60, 50)), origin='lower')
plt.savefig('synthetic_local_gmm_mahalanobis.png')
plt.show()