-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclustering.html
More file actions
359 lines (324 loc) · 15.6 KB
/
Copy pathclustering.html
File metadata and controls
359 lines (324 loc) · 15.6 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tablero Interactivo de Clustering K-Means</title>
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FNRL3XH0JH"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-FNRL3XH0JH');
</script>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-2.32.0.min.js"></script>
<!-- Google Fonts: Inter -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
body { font-family: 'Inter', sans-serif; }
.control-panel-card { @apply bg-white p-6 rounded-xl shadow-lg; }
.metric-card { @apply bg-gray-50 p-4 rounded-lg text-center border border-gray-200; }
.metric-title { @apply text-sm font-medium text-gray-500; }
.metric-value { @apply text-2xl font-bold text-indigo-600; }
.metric-desc { @apply text-xs text-gray-400 mt-1; }
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none; appearance: none; width: 20px; height: 20px;
background: #4f46e5; cursor: pointer; border-radius: 50%;
}
input[type="range"]::-moz-range-thumb {
width: 20px; height: 20px; background: #4f46e5; cursor: pointer; border-radius: 50%;
}
.btn { @apply px-4 py-2 text-white font-semibold rounded-lg shadow-md transition-colors duration-300; }
.btn-primary { @apply bg-indigo-600 hover:bg-indigo-700; }
</style>
</head>
<body class="bg-gray-100">
<div class="container mx-auto p-4 md:p-8">
<header class="text-center mb-8">
<h1 class="text-4xl font-bold text-gray-800">Explorando Clustering con K-Means</h1>
<p class="text-lg text-gray-600 mt-2">Visualiza el impacto de las métricas de distancia en la formación de clústeres.</p>
</header>
<div class="flex flex-col lg:flex-row gap-8">
<!-- Panel de Control -->
<aside class="w-full lg:w-1/3">
<div class="control-panel-card space-y-6">
<div>
<h3 class="text-lg font-semibold text-gray-700 mb-3">1. Generación de Datos</h3>
<label for="data-shape" class="text-sm font-medium text-gray-600">Forma de los datos</label>
<select id="data-shape" class="w-full mt-1 p-2 border border-gray-300 rounded-lg">
<option value="blobs" selected>Grupos (Blobs)</option>
<option value="circles">Círculos Concéntricos</option>
<option value="moons">Medias Lunas (Moons)</option>
<option value="random">Aleatorio Uniforme</option>
</select>
</div>
<div>
<h3 class="text-lg font-semibold text-gray-700 mb-3">2. Parámetros del Algoritmo</h3>
<div class="space-y-4">
<div>
<label for="k-clusters" class="text-sm font-medium text-gray-600 flex justify-between">Número de Clústeres (k): <span id="k-clusters-value" class="font-bold text-indigo-600">3</span></label>
<input type="range" id="k-clusters" min="2" max="10" value="3" step="1" class="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer">
</div>
<div>
<label class="text-sm font-medium text-gray-600">Métrica de Distancia</label>
<div id="distance-metric" class="flex flex-wrap gap-2 mt-2">
<button data-metric="euclidean" class="flex-1 px-3 py-2 text-sm font-semibold rounded-lg bg-indigo-600 text-white">Euclidiana (L2)</button>
<button data-metric="manhattan" class="flex-1 px-3 py-2 text-sm font-semibold rounded-lg bg-gray-200 text-gray-700">Manhattan (L1)</button>
<button data-metric="chebyshev" class="flex-1 px-3 py-2 text-sm font-semibold rounded-lg bg-gray-200 text-gray-700">Chebyshev (L∞)</button>
</div>
</div>
</div>
</div>
<div class="pt-4 border-t flex justify-center">
<button id="run-button" class="btn btn-primary">Ejecutar K-Means</button>
</div>
</div>
<!-- Métricas de Evaluación -->
<div class="control-panel-card mt-8">
<h2 class="text-xl font-bold text-gray-800 mb-4">Métricas de Evaluación</h2>
<div class="grid grid-cols-2 gap-4">
<div class="metric-card">
<h4 class="metric-title">Inercia</h4>
<p id="inertia-value" class="metric-value">N/A</p>
<p class="metric-desc">Homogeneidad (menor es mejor)</p>
</div>
<div class="metric-card">
<h4 class="metric-title">Coef. de Silueta</h4>
<p id="silhouette-value" class="metric-value">N/A</p>
<p class="metric-desc">Separación (cercano a 1 es mejor)</p>
</div>
</div>
</div>
</aside>
<!-- Gráfica -->
<main class="w-full lg:w-2/3 bg-white p-4 rounded-xl shadow-lg min-h-[600px]">
<div id="plot"></div>
</main>
</div>
</div>
<script>
// --- ELEMENTOS DEL DOM ---
const dataShapeSelect = document.getElementById('data-shape');
const kClustersSlider = document.getElementById('k-clusters');
const kClustersValue = document.getElementById('k-clusters-value');
const distanceMetricContainer = document.getElementById('distance-metric');
const runButton = document.getElementById('run-button');
const plotDiv = document.getElementById('plot');
const inertiaValueEl = document.getElementById('inertia-value');
const silhouetteValueEl = document.getElementById('silhouette-value');
let data = [];
let state = {
k: 3,
distance: 'euclidean'
};
// --- GENERACIÓN DE DATOS ---
function generateData() {
const shape = dataShapeSelect.value;
const n_samples = 300;
data = [];
if (shape === 'blobs') {
const centers = [[-3, -3], [3, 3], [-3, 3], [3, -3], [0,0]];
const k_blobs = 5;
for (let i = 0; i < n_samples; i++) {
const center = centers[Math.floor(Math.random() * k_blobs)];
data.push([center[0] + (Math.random() - 0.5) * 3, center[1] + (Math.random() - 0.5) * 3]);
}
} else if (shape === 'circles') {
for (let i = 0; i < n_samples; i++) {
const r = i < n_samples / 2 ? 3 : 6;
const angle = Math.random() * 2 * Math.PI;
data.push([r * Math.cos(angle) + (Math.random()-0.5)*0.5, r * Math.sin(angle) + (Math.random()-0.5)*0.5]);
}
} else if (shape === 'moons') {
for (let i = 0; i < n_samples; i++) {
const angle = (i / n_samples) * Math.PI;
if (i < n_samples / 2) {
data.push([Math.cos(angle) * 5, Math.sin(angle) * 5 + (Math.random()-0.5)]);
} else {
data.push([Math.cos(angle) * 5 + 2.5, Math.sin(angle) * -5 + (Math.random()-0.5) + 2]);
}
}
} else if (shape === 'random') {
for (let i = 0; i < n_samples; i++) {
data.push([(Math.random() - 0.5) * 20, (Math.random() - 0.5) * 20]);
}
}
drawInitialPlot();
}
// --- LÓGICA DE CLUSTERING (K-MEANS) ---
const distanceFunctions = {
euclidean: (a, b) => Math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2),
manhattan: (a, b) => Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]),
chebyshev: (a, b) => Math.max(Math.abs(a[0] - b[0]), Math.abs(a[1] - b[1]))
};
function kMeans(data, k, distanceMetric) {
const distFn = distanceFunctions[distanceMetric];
let centroids = data.slice(0, k).map(d => [...d]); // Iniciar con los primeros k puntos
let assignments = new Array(data.length);
let changed = true;
for (let iter = 0; iter < 100 && changed; iter++) {
changed = false;
// Paso de asignación
data.forEach((point, i) => {
let minDist = Infinity;
let bestCluster = -1;
centroids.forEach((centroid, j) => {
const d = distFn(point, centroid);
if (d < minDist) {
minDist = d;
bestCluster = j;
}
});
if (assignments[i] !== bestCluster) {
assignments[i] = bestCluster;
changed = true;
}
});
// Paso de actualización
const newCentroids = Array.from({ length: k }, () => [0, 0]);
const counts = new Array(k).fill(0);
data.forEach((point, i) => {
const cluster = assignments[i];
newCentroids[cluster][0] += point[0];
newCentroids[cluster][1] += point[1];
counts[cluster]++;
});
centroids = newCentroids.map((sum, i) =>
counts[i] > 0 ? [sum[0] / counts[i], sum[1] / counts[i]] : centroids[i] // si un clúster está vacío, no mover centroide
);
}
return { assignments, centroids };
}
// --- CÁLCULO DE MÉTRICAS ---
function calculateMetrics(data, assignments, centroids) {
// 1. Inercia (Suma de distancias euclidianas al cuadrado)
let inertia = 0;
data.forEach((point, i) => {
const centroid = centroids[assignments[i]];
if(centroid){
inertia += distanceFunctions.euclidean(point, centroid) ** 2;
}
});
// 2. Coeficiente de Silueta
let totalSilhouette = 0;
data.forEach((point, i) => {
const clusterIndex = assignments[i];
const ownCluster = data.filter((_, j) => assignments[j] === clusterIndex);
// a(i): Distancia media dentro del clúster
let a_i = 0;
if (ownCluster.length > 1) {
ownCluster.forEach(otherPoint => {
if (point !== otherPoint) a_i += distanceFunctions.euclidean(point, otherPoint);
});
a_i /= (ownCluster.length - 1);
}
// b(i): Distancia media al clúster más cercano
let b_i = Infinity;
for (let k = 0; k < centroids.length; k++) {
if (k === clusterIndex) continue;
const otherCluster = data.filter((_, j) => assignments[j] === k);
if (otherCluster.length > 0) {
let avgDistOther = 0;
otherCluster.forEach(otherPoint => {
avgDistOther += distanceFunctions.euclidean(point, otherPoint);
});
avgDistOther /= otherCluster.length;
b_i = Math.min(b_i, avgDistOther);
}
}
if (ownCluster.length > 1 && b_i !== Infinity) {
totalSilhouette += (b_i - a_i) / Math.max(a_i, b_i);
}
});
const silhouette = data.length > 0 ? totalSilhouette / data.length : 0;
inertiaValueEl.textContent = inertia.toFixed(0);
silhouetteValueEl.textContent = silhouette.toFixed(3);
}
// --- VISUALIZACIÓN ---
function drawInitialPlot() {
const trace = {
x: data.map(d => d[0]),
y: data.map(d => d[1]),
mode: 'markers',
type: 'scatter',
marker: { color: 'gray', size: 7, opacity: 0.7 }
};
const layout = {
title: 'Datos Generados (Sin Agrupar)',
xaxis: { title: 'Característica 1' },
yaxis: { title: 'Característica 2' },
};
Plotly.newPlot(plotDiv, [trace], layout, {responsive: true});
inertiaValueEl.textContent = "N/A";
silhouetteValueEl.textContent = "N/A";
}
function drawClusteredPlot(assignments, centroids) {
const k = centroids.length;
const traces = [];
const colors = ['#636EFA', '#EF553B', '#00CC96', '#AB63FA', '#FFA15A', '#19D3F3', '#FF6692', '#B6E880', '#FF97FF', '#FECB52'];
for (let i = 0; i < k; i++) {
const clusterPoints = data.filter((_, j) => assignments[j] === i);
traces.push({
x: clusterPoints.map(d => d[0]),
y: clusterPoints.map(d => d[1]),
mode: 'markers',
type: 'scatter',
name: `Clúster ${i + 1}`,
marker: { color: colors[i % colors.length], size: 7, opacity: 0.7 }
});
}
// Añadir centroides
traces.push({
x: centroids.map(c => c[0]),
y: centroids.map(c => c[1]),
mode: 'markers',
type: 'scatter',
name: 'Centroides',
marker: { color: 'black', size: 14, symbol: 'cross', line: {width: 3} }
});
const layout = {
title: `Resultado de K-Means (k=${k}, Distancia: ${state.distance})`,
xaxis: { title: 'Característica 1' },
yaxis: { title: 'Característica 2' },
showlegend: true
};
Plotly.newPlot(plotDiv, traces, layout, {responsive: true});
}
// --- MANEJADORES DE EVENTOS ---
kClustersSlider.addEventListener('input', (e) => {
state.k = parseInt(e.target.value);
kClustersValue.textContent = state.k;
});
distanceMetricContainer.addEventListener('click', (e) => {
if (e.target.tagName === 'BUTTON') {
state.distance = e.target.dataset.metric;
// Actualizar estilo de botones
distanceMetricContainer.querySelectorAll('button').forEach(btn => {
btn.classList.remove('bg-indigo-600', 'text-white');
btn.classList.add('bg-gray-200', 'text-gray-700');
});
e.target.classList.add('bg-indigo-600', 'text-white');
e.target.classList.remove('bg-gray-200', 'text-gray-700');
}
});
dataShapeSelect.addEventListener('change', generateData);
runButton.addEventListener('click', () => {
if (data.length === 0) return;
const { assignments, centroids } = kMeans(data, state.k, state.distance);
drawClusteredPlot(assignments, centroids);
calculateMetrics(data, assignments, centroids);
});
// --- INICIALIZACIÓN ---
window.addEventListener('load', () => {
generateData();
kClustersValue.textContent = kClustersSlider.value;
});
</script>
</body>
</html>