-
Notifications
You must be signed in to change notification settings - Fork 6
/
clustering_with_kpca_example.py
108 lines (88 loc) · 2.45 KB
/
clustering_with_kpca_example.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
from HiPart.clustering import DePDDP
from sklearn.decomposition import KernelPCA
from sklearn.datasets import make_circles
import HiPart.visualizations as viz
import matplotlib.pyplot as plt
import numpy as np
def plot_manafolds(kpca, X, y, vals, gamma, title):
"""
2D plot for the generation of a scatter plot with the same meshgrid as the
one used by the KernelPCA method.
Parameters
----------
X : numpy.ndarray
Data for the scatter plot.
y : numpy.ndarray
Data labels.
vals : list
Label values.
gamma : flaot
Meshgrid gamma.
title : str
Title of the plot.
Returns
-------
plt :
The final plot.
"""
plt.title(title)
reds = y == vals[0]
blues = y == vals[1]
plt.scatter(X[reds, 0], X[reds, 1], c="red", s=20, edgecolor="k")
plt.scatter(X[blues, 0], X[blues, 1], c="blue", s=20, edgecolor="k")
plt.xlabel("$x_1$")
plt.ylabel("$x_2$")
X1, X2 = np.meshgrid(
np.linspace(-gamma, gamma, 50),
np.linspace(-gamma, gamma, 50),
)
X_grid = np.array([np.ravel(X1), np.ravel(X2)]).T
# projection on the first principal component (in the phi space)
Z_grid = kpca.transform(X_grid)[:, 0].reshape(X1.shape)
plt.contour(X1, X2, Z_grid, colors="grey", linewidths=1, origin="lower")
return plt
if __name__ == "__main__":
# set a random seed for results consistency
np.random.seed(123)
# Create the dataset
X, y = make_circles(n_samples=400, factor=0.3, noise=0.05)
# execution of the for visulaization purposes
kPCA_params = {
"kernel": "rbf",
"fit_inverse_transform": True,
"gamma": 1.5,
}
kpca = KernelPCA(**kPCA_params)
X_kpca = kpca.fit(X)
# visualize the data on the original space
plot_manafolds(
kpca,
X=X,
y=y,
vals=[0, 1],
gamma=1.5,
title="Original space",
).show()
# data clustering
clusternumber = 2
outObj = DePDDP(
decomposition_method="kpca",
max_clusters_number=clusternumber,
bandwidth_scale=0.5,
percentile=0.1,
**kPCA_params
).fit(X)
#####
# split visualization
fig = viz.split_visualization(outObj)
fig.show()
# Clustered data visulization
out_y = outObj.labels_
plot_manafolds(
kpca,
X=X,
y=out_y,
vals=[0, 1],
gamma=1.5,
title="Clusterd data",
).show()