492 lines
15 KiB
HTML
492 lines
15 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>k-NN Animation - Micro-régions</title>
|
|
<style>
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
margin: 0;
|
|
padding: 20px;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
background: white;
|
|
border-radius: 15px;
|
|
padding: 30px;
|
|
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
|
|
}
|
|
|
|
h1 {
|
|
text-align: center;
|
|
color: #333;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.subtitle {
|
|
text-align: center;
|
|
color: #666;
|
|
margin-bottom: 30px;
|
|
font-style: italic;
|
|
}
|
|
|
|
canvas {
|
|
border: 2px solid #ddd;
|
|
border-radius: 10px;
|
|
display: block;
|
|
margin: 20px auto;
|
|
cursor: crosshair;
|
|
background: #fafafa;
|
|
}
|
|
|
|
.controls {
|
|
background: #f5f5f5;
|
|
padding: 20px;
|
|
border-radius: 10px;
|
|
margin: 20px 0;
|
|
}
|
|
|
|
.control-group {
|
|
margin: 15px 0;
|
|
}
|
|
|
|
label {
|
|
font-weight: bold;
|
|
color: #555;
|
|
display: block;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
input[type="range"] {
|
|
width: 100%;
|
|
height: 8px;
|
|
border-radius: 5px;
|
|
background: #ddd;
|
|
outline: none;
|
|
}
|
|
|
|
input[type="range"]::-webkit-slider-thumb {
|
|
appearance: none;
|
|
width: 20px;
|
|
height: 20px;
|
|
border-radius: 50%;
|
|
background: #667eea;
|
|
cursor: pointer;
|
|
}
|
|
|
|
input[type="range"]::-moz-range-thumb {
|
|
width: 20px;
|
|
height: 20px;
|
|
border-radius: 50%;
|
|
background: #667eea;
|
|
cursor: pointer;
|
|
border: none;
|
|
}
|
|
|
|
.value-display {
|
|
display: inline-block;
|
|
background: #667eea;
|
|
color: white;
|
|
padding: 5px 15px;
|
|
border-radius: 20px;
|
|
font-weight: bold;
|
|
margin-left: 10px;
|
|
}
|
|
|
|
.legend {
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 20px;
|
|
margin: 20px 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.legend-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
padding: 8px 15px;
|
|
background: white;
|
|
border-radius: 8px;
|
|
border: 2px solid #ddd;
|
|
}
|
|
|
|
.legend-circle {
|
|
width: 20px;
|
|
height: 20px;
|
|
border-radius: 50%;
|
|
border: 2px solid #333;
|
|
}
|
|
|
|
.result {
|
|
text-align: center;
|
|
font-size: 1.5em;
|
|
font-weight: bold;
|
|
margin: 20px 0;
|
|
padding: 20px;
|
|
border-radius: 10px;
|
|
background: #f0f0f0;
|
|
}
|
|
|
|
.votes {
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 15px;
|
|
margin: 15px 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.vote-box {
|
|
padding: 15px 20px;
|
|
border-radius: 10px;
|
|
text-align: center;
|
|
min-width: 100px;
|
|
border: 3px solid transparent;
|
|
}
|
|
|
|
.vote-count {
|
|
font-size: 2em;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.vote-label {
|
|
font-size: 0.9em;
|
|
margin-top: 5px;
|
|
}
|
|
|
|
.buttons {
|
|
display: flex;
|
|
gap: 10px;
|
|
justify-content: center;
|
|
margin: 20px 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
button {
|
|
padding: 12px 24px;
|
|
font-size: 16px;
|
|
border: none;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
font-weight: bold;
|
|
transition: all 0.3s;
|
|
}
|
|
|
|
button:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
|
}
|
|
|
|
.btn-reset {
|
|
background: #f44336;
|
|
color: white;
|
|
}
|
|
|
|
.btn-random {
|
|
background: #4CAF50;
|
|
color: white;
|
|
}
|
|
|
|
.instructions {
|
|
background: #fff3cd;
|
|
border-left: 4px solid #ffc107;
|
|
padding: 15px;
|
|
margin: 20px 0;
|
|
border-radius: 5px;
|
|
}
|
|
|
|
.instructions h3 {
|
|
margin-top: 0;
|
|
color: #856404;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>🎯 Animation k-NN : Classification multi-zones</h1>
|
|
<p class="subtitle">Algorithme des k plus proches voisins avec plusieurs classes</p>
|
|
|
|
<div class="instructions">
|
|
<h3>📋 Instructions</h3>
|
|
<ul>
|
|
<li><strong>Cliquez</strong> sur le graphique pour placer un nouveau point</li>
|
|
<li><strong>Ajustez le curseur k</strong> pour voir l'impact du nombre de voisins</li>
|
|
<li>Observez comment les <strong>votes</strong> se répartissent entre les zones</li>
|
|
<li>Les lignes colorées montrent les <strong>k plus proches voisins</strong></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<canvas id="canvas" width="800" height="600"></canvas>
|
|
|
|
<div class="legend" id="legend"></div>
|
|
|
|
<div class="controls">
|
|
<div class="control-group">
|
|
<label>
|
|
Nombre de voisins (k) :
|
|
<span class="value-display" id="kValue">5</span>
|
|
</label>
|
|
<input type="range" id="kSlider" min="1" max="21" value="5" step="2">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="result" id="result">
|
|
Cliquez sur le graphique pour classifier un point
|
|
</div>
|
|
|
|
<div class="votes" id="votes"></div>
|
|
|
|
<div class="buttons">
|
|
<button class="btn-random" onclick="placeRandomPoint()">🎲 Point Aléatoire</button>
|
|
<button class="btn-reset" onclick="resetPoint()">🔄 Réinitialiser</button>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const canvas = document.getElementById('canvas');
|
|
const ctx = canvas.getContext('2d');
|
|
const kSlider = document.getElementById('kSlider');
|
|
const kValue = document.getElementById('kValue');
|
|
const result = document.getElementById('result');
|
|
const votesDiv = document.getElementById('votes');
|
|
const legendDiv = document.getElementById('legend');
|
|
|
|
// Définition des zones (micro-régions)
|
|
const zones = [
|
|
{ name: 'Alta Rocca', color: '#e74c3c', shortName: 'Alta Rocca' },
|
|
{ name: 'Balagne', color: '#3498db', shortName: 'Balagne' },
|
|
{ name: 'Centre Corse', color: '#2ecc71', shortName: 'Centre' },
|
|
{ name: 'Extrême Sud', color: '#f39c12', shortName: 'Sud' },
|
|
{ name: 'Cap Corse', color: '#9b59b6', shortName: 'Cap' }
|
|
];
|
|
|
|
// Points d'apprentissage (générés automatiquement par zone)
|
|
const trainingPoints = [];
|
|
|
|
// Zone 1 - Alta Rocca (rouge, bas gauche)
|
|
for (let i = 0; i < 15; i++) {
|
|
trainingPoints.push({
|
|
x: 100 + Math.random() * 150,
|
|
y: 400 + Math.random() * 150,
|
|
zone: 0
|
|
});
|
|
}
|
|
|
|
// Zone 2 - Balagne (bleu, haut gauche)
|
|
for (let i = 0; i < 15; i++) {
|
|
trainingPoints.push({
|
|
x: 100 + Math.random() * 150,
|
|
y: 50 + Math.random() * 150,
|
|
zone: 1
|
|
});
|
|
}
|
|
|
|
// Zone 3 - Centre Corse (vert, centre)
|
|
for (let i = 0; i < 15; i++) {
|
|
trainingPoints.push({
|
|
x: 300 + Math.random() * 200,
|
|
y: 200 + Math.random() * 200,
|
|
zone: 2
|
|
});
|
|
}
|
|
|
|
// Zone 4 - Extrême Sud (jaune, bas droite)
|
|
for (let i = 0; i < 15; i++) {
|
|
trainingPoints.push({
|
|
x: 550 + Math.random() * 150,
|
|
y: 400 + Math.random() * 150,
|
|
zone: 3
|
|
});
|
|
}
|
|
|
|
// Zone 5 - Cap Corse (violet, haut droite)
|
|
for (let i = 0; i < 15; i++) {
|
|
trainingPoints.push({
|
|
x: 550 + Math.random() * 150,
|
|
y: 50 + Math.random() * 150,
|
|
zone: 4
|
|
});
|
|
}
|
|
|
|
let testPoint = null;
|
|
let k = 5;
|
|
|
|
// Créer la légende
|
|
function createLegend() {
|
|
legendDiv.innerHTML = '';
|
|
zones.forEach(zone => {
|
|
const item = document.createElement('div');
|
|
item.className = 'legend-item';
|
|
item.innerHTML = `
|
|
<div class="legend-circle" style="background: ${zone.color};"></div>
|
|
<span>${zone.name}</span>
|
|
`;
|
|
legendDiv.appendChild(item);
|
|
});
|
|
}
|
|
|
|
function distance(p1, p2) {
|
|
return Math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2);
|
|
}
|
|
|
|
function findKNearest(point, k) {
|
|
const distances = trainingPoints.map(p => ({
|
|
point: p,
|
|
dist: distance(point, p)
|
|
}));
|
|
|
|
distances.sort((a, b) => a.dist - b.dist);
|
|
return distances.slice(0, k);
|
|
}
|
|
|
|
function classify(point, k) {
|
|
const nearest = findKNearest(point, k);
|
|
const votes = {};
|
|
|
|
zones.forEach((_, idx) => votes[idx] = 0);
|
|
|
|
nearest.forEach(n => {
|
|
votes[n.point.zone]++;
|
|
});
|
|
|
|
// Trouver la zone avec le plus de votes
|
|
let maxVotes = -1;
|
|
let predictedZone = -1;
|
|
|
|
for (let zone in votes) {
|
|
if (votes[zone] > maxVotes) {
|
|
maxVotes = votes[zone];
|
|
predictedZone = parseInt(zone);
|
|
}
|
|
}
|
|
|
|
return {
|
|
zone: predictedZone,
|
|
votes: votes,
|
|
nearest: nearest
|
|
};
|
|
}
|
|
|
|
function draw() {
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
// Dessiner les points d'apprentissage
|
|
trainingPoints.forEach(p => {
|
|
ctx.beginPath();
|
|
ctx.arc(p.x, p.y, 6, 0, 2 * Math.PI);
|
|
ctx.fillStyle = zones[p.zone].color;
|
|
ctx.fill();
|
|
ctx.strokeStyle = '#333';
|
|
ctx.lineWidth = 1;
|
|
ctx.stroke();
|
|
});
|
|
|
|
// Si un point test existe
|
|
if (testPoint) {
|
|
const classification = classify(testPoint, k);
|
|
|
|
// Dessiner les lignes vers les k plus proches voisins
|
|
classification.nearest.forEach((n, index) => {
|
|
ctx.beginPath();
|
|
ctx.moveTo(testPoint.x, testPoint.y);
|
|
ctx.lineTo(n.point.x, n.point.y);
|
|
ctx.strokeStyle = zones[n.point.zone].color;
|
|
ctx.globalAlpha = 0.3 + 0.7 * (k - index) / k;
|
|
ctx.lineWidth = 2;
|
|
ctx.stroke();
|
|
ctx.globalAlpha = 1;
|
|
});
|
|
|
|
// Dessiner un cercle autour du k-ième plus proche voisin
|
|
if (classification.nearest.length > 0) {
|
|
const maxDist = classification.nearest[classification.nearest.length - 1].dist;
|
|
ctx.beginPath();
|
|
ctx.arc(testPoint.x, testPoint.y, maxDist, 0, 2 * Math.PI);
|
|
ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)';
|
|
ctx.lineWidth = 2;
|
|
ctx.setLineDash([5, 5]);
|
|
ctx.stroke();
|
|
ctx.setLineDash([]);
|
|
}
|
|
|
|
// Dessiner le point test
|
|
ctx.beginPath();
|
|
ctx.arc(testPoint.x, testPoint.y, 12, 0, 2 * Math.PI);
|
|
ctx.fillStyle = '#95a5a6';
|
|
ctx.fill();
|
|
ctx.strokeStyle = '#000';
|
|
ctx.lineWidth = 3;
|
|
ctx.stroke();
|
|
|
|
// Afficher le résultat
|
|
displayResults(classification);
|
|
}
|
|
}
|
|
|
|
function displayResults(classification) {
|
|
const zoneName = zones[classification.zone].name;
|
|
const zoneColor = zones[classification.zone].color;
|
|
|
|
result.innerHTML = `Classification : <span style="color: ${zoneColor};">${zoneName}</span>`;
|
|
result.style.background = zoneColor + '22';
|
|
|
|
// Afficher les votes
|
|
votesDiv.innerHTML = '';
|
|
zones.forEach((zone, idx) => {
|
|
const voteBox = document.createElement('div');
|
|
voteBox.className = 'vote-box';
|
|
voteBox.style.background = zone.color + '22';
|
|
voteBox.style.borderColor = classification.zone === idx ? zone.color : 'transparent';
|
|
|
|
const voteCount = classification.votes[idx] || 0;
|
|
|
|
voteBox.innerHTML = `
|
|
<div class="vote-count" style="color: ${zone.color};">${voteCount}</div>
|
|
<div class="vote-label">${zone.shortName}</div>
|
|
`;
|
|
votesDiv.appendChild(voteBox);
|
|
});
|
|
}
|
|
|
|
canvas.addEventListener('click', (e) => {
|
|
const rect = canvas.getBoundingClientRect();
|
|
testPoint = {
|
|
x: e.clientX - rect.left,
|
|
y: e.clientY - rect.top
|
|
};
|
|
draw();
|
|
});
|
|
|
|
kSlider.addEventListener('input', (e) => {
|
|
k = parseInt(e.target.value);
|
|
kValue.textContent = k;
|
|
draw();
|
|
});
|
|
|
|
function placeRandomPoint() {
|
|
testPoint = {
|
|
x: Math.random() * (canvas.width - 100) + 50,
|
|
y: Math.random() * (canvas.height - 100) + 50
|
|
};
|
|
draw();
|
|
}
|
|
|
|
function resetPoint() {
|
|
testPoint = null;
|
|
result.innerHTML = 'Cliquez sur le graphique pour classifier un point';
|
|
result.style.background = '#f0f0f0';
|
|
votesDiv.innerHTML = '';
|
|
draw();
|
|
}
|
|
|
|
// Initialisation
|
|
createLegend();
|
|
draw();
|
|
</script>
|
|
</body>
|
|
</html>
|