File size: 21,214 Bytes
d0a6b4f | 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | {% extends "layout.html" %}
{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Decision Tree Regression - Interactive Flow Visualization</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #f0f4ff;
margin: 0;
padding: 40px 20px;
color: #333;
display: flex;
justify-content: center;
}
.container {
max-width: 900px;
background: white;
border-radius: 20px;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
padding: 30px 40px;
}
h1 {
text-align: center;
color: #4facfe;
margin-bottom: 30px;
font-weight: 700;
}
.stepper {
display: flex;
justify-content: space-between;
margin-bottom: 40px;
}
.step {
flex: 1;
text-align: center;
position: relative;
cursor: pointer;
color: #bbb;
font-weight: 600;
user-select: none;
}
.step.active, .step.completed {
color: #4facfe;
}
.step::after {
content: '';
position: absolute;
top: 18px;
right: -50%;
width: 100%;
height: 4px;
background: #bbb;
z-index: -1;
transition: background 0.3s;
}
.step:last-child::after {
display: none;
}
.step.completed::after {
background: #4facfe;
}
.step-circle {
width: 36px;
height: 36px;
border-radius: 50%;
border: 3px solid currentColor;
margin: 0 auto 8px;
line-height: 30px;
font-size: 18px;
color: inherit;
background: white;
display: flex;
align-items: center;
justify-content: center;
}
.content {
min-height: 280px;
position: relative;
}
.content > div {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
opacity: 0;
pointer-events: none;
transition: opacity 0.5s ease;
}
.content > div.active {
opacity: 1;
pointer-events: auto;
position: relative;
}
.content h2 {
color: #4facfe;
margin-bottom: 15px;
}
.content p {
font-size: 1.1rem;
line-height: 1.5;
margin-bottom: 25px;
}
.svg-container {
width: 100%;
height: 180px;
}
svg {
width: 100%;
height: 100%;
}
.nav-buttons {
text-align: center;
margin-top: 30px;
}
button {
background: #4facfe;
border: none;
color: white;
font-weight: 700;
padding: 12px 28px;
margin: 0 10px;
border-radius: 10px;
cursor: pointer;
font-size: 1rem;
transition: background 0.3s;
}
button:disabled {
background: #a0c8ff;
cursor: not-allowed;
}
button:hover:not(:disabled) {
background: #3a9dfd;
}
/* Colors for SVG elements */
.color-data { fill: #ff6b6b; }
.color-split { stroke: #fd746c; stroke-width: 4; }
.color-left { fill: #56ab2f; fill-opacity: 0.3; stroke: #56ab2f; stroke-width: 2; }
.color-right { fill: #fd746c; fill-opacity: 0.3; stroke: #fd746c; stroke-width: 2; }
.color-node { fill: #4facfe; fill-opacity: 0.15; stroke: #4facfe; stroke-width: 2; }
.color-path { stroke: #ff00ff; stroke-width: 3; }
.color-prediction { fill: #ff00ff; }
</style>
<style>
body {
background: #fff;
color: #000;
font-family: 'Inter', sans-serif;
line-height: 1.6;
padding: 1rem 1rem 3rem;
}
h1, h2, h3 {
font-weight: 700;
margin-top: 0;
margin-bottom: 1rem;
}
p, li {
font-weight: 600;
margin-bottom: 1rem;
}
ul {
padding-left: 1.25rem;
margin-bottom: 1rem;
}
.card {
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 12px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
padding: 1.5rem 2rem;
margin-bottom: 2rem;
}
label {
font-weight: 700;
margin-right: 0.5rem;
min-width: 70px;
display: inline-block;
}
input, button {
background: #e5e7eb;
border: 1px solid #9ca3af;
border-radius: 8px;
padding: 0.5rem 1rem;
font-weight: 600;
color: #000;
font-size: 1rem;
}
input:focus, button:focus {
outline: none;
border-color: #6b7280;
box-shadow: 0 0 5px #6b7280;
}
button {
cursor: pointer;
font-weight: 700;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #d1d5db;
}
#dataPointsList li {
background: #e5e7eb;
padding: 0.5rem 1rem;
border-radius: 8px;
margin: 0.25rem;
font-weight: 600;
display: inline-block;
}
.plot-container {
background: #fff !important;
border-radius: 12px;
padding: 1rem;
border: 1px solid #e5e7eb;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
height: 400px;
}
</style>
<div class="max-w-7xl mx-auto">
<header class="text-center mb-8">
<h1 class="text-4xl sm:text-5xl">Decision Tree Regression (DTR) Visualization</h1>
<p class="text-lg font-semibold">Explore how DTR predicts continuous values with interactive examples.</p>
</header>
<!-- THEORY SECTIONS -->
<section class="card">
<h3>What is Decision Tree Regression?</h3>
<p>It’s a smart algorithm that predicts numbers — like house prices or temperatures — by splitting data into smaller groups based on features. Imagine a tree where each branch asks a question, and leaves give the final prediction.</p>
</section>
<section class="card">
<h3>How Does It Work?</h3>
<p>The tree splits data recursively, choosing the best points to divide so that each group is as similar as possible. It stops splitting when the groups are small or deep enough.</p>
<ul>
<li><strong>Variance Reduction:</strong> Splits aim to reduce differences within groups.</li>
<li><strong>Mean Squared Error (MSE):</strong> The algorithm picks splits that minimize prediction errors.</li>
</ul>
</section>
<section class="card">
<h3>Making Predictions</h3>
<p>To predict a new value, the data point travels down the tree following the split rules until it reaches a leaf. The prediction is the average of all training points in that leaf.</p>
</section>
<section class="card">
<h3>Key Hyperparameters</h3>
<ul>
<li><strong>max_depth:</strong> Limits tree height to avoid overfitting.</li>
<li><strong>min_samples_split:</strong> Minimum data points to split a node.</li>
<li><strong>min_samples_leaf:</strong> Minimum data points in a leaf.</li>
</ul>
</section>
<section class="card">
<h3>Comparison with Other Models</h3>
<ul>
<li><strong>Decision Tree vs. Linear Regression:</strong>
DTR can model non-linear relationships, whereas Linear Regression assumes a linear relationship.
DTR is generally more flexible but also more prone to overfitting.
</li>
<li><strong>Decision Tree vs. SVR (Support Vector Regression):</strong>
SVR is a powerful model that finds the best fit line (or hyperplane) while tolerating some error.
SVR can be very effective but is often more complex to tune than DTR.
</li>
<li><strong>Decision Tree vs. Random Forest:</strong>
Random Forest is an ensemble of Decision Trees. It builds multiple trees and averages their predictions.
This significantly reduces variance and improves stability, making it a much better and more common choice in practice than a single DTR.
</li>
</ul>
</section>
<section class="card">
<h3>Key Hyperparameters (Detailed)</h3>
<ul>
<li>
<strong>max_depth:</strong> How deep the tree can grow.
<br>👉 Bigger depth = tree keeps splitting → very detailed → <em>more overfitting</em>.
<br>👉 Smaller depth = tree stops early → simpler → <em>less overfitting</em>.
</li>
<li>
<strong>min_samples_split:</strong> Minimum samples needed to split a node.
<br>👉 Smaller value (like 2) = splits happen easily → <em>more overfitting</em>.
<br>👉 Larger value (like 10) = splits happen only with many samples → <em>less overfitting</em>.
<br><em>Example:</em> If <code>min_samples_split=2</code>, even 2 points can split → tree memorizes tiny patterns.
If <code>min_samples_split=10</code>, need 10+ points to split → tree generalizes.
</li>
<li>
<strong>min_samples_leaf:</strong> Minimum samples in a leaf node.
<br>👉 Smaller value (like 1) = tiny leaves → <em>more overfitting</em>.
<br>👉 Larger value (like 5 or 10) = bigger leaves → <em>less overfitting</em>.
<br><em>Example:</em> If <code>min_samples_leaf=1</code>, each data point might get its own leaf.
If <code>min_samples_leaf=10</code>, each leaf must cover at least 10 points → tree generalizes.
</li>
<li>
<strong>max_features:</strong> Number of features considered at each split.
<br>👉 Smaller value = fewer features per split → adds randomness → can reduce overfitting (especially in Random Forests).
<br>👉 Larger value = considers all features → risk of overfitting.
</li>
</ul>
<p><strong>✅ Summary Memory Trick:</strong><br>
Big numbers (<code>min_samples_split ↑</code>, <code>min_samples_leaf ↑</code>) + small <code>max_depth ↓</code> → simpler tree → less overfitting.<br>
Small numbers (<code>min_samples_split ↓</code>, <code>min_samples_leaf ↓</code>) + big <code>max_depth ↑</code> → complex tree → more overfitting.
</p>
</section>
decis
<section class="card">
<h3>Why Use DTR?</h3>
<ul>
<li>Easy to understand and visualize.</li>
<li>Captures complex, non-linear relationships.</li>
<li>No need to scale features.</li>
</ul>
</section>
<section class="card">
<h3>Limitations</h3>
<ul>
<li>Can overfit if not controlled.</li>
<li>Small data changes can cause big model changes.</li>
<li>Less stable than ensemble methods like Random Forest.</li>
</ul>
</section>
<section class="card">
<h3>Evaluation Metrics</h3>
<ul>
<li><strong>MSE:</strong> Average squared error.</li>
<li><strong>RMSE:</strong> Square root of MSE, same units as target.</li>
<li><strong>R² Score:</strong> How well predictions fit actual data (1 = perfect).</li>
</ul>
</section>
<section class="card">
<h3>Applications</h3>
<ul>
<li>Predicting house prices.</li>
<li>Estimating medical costs.</li>
<li>Forecasting sales.</li>
<li>Predicting energy consumption.</li>
</ul>
</section>
<a href="/dtrg"
class="inline-block bg-white text-black font-bold px-4 py-2 rounded shadow-md
border border-black relative
hover:after:content-[''] hover:after:absolute hover:after:right-0 hover:after:bottom-0
hover:after:w-full hover:after:h-[2px] hover:after:bg-black
hover:before:content-[''] hover:before:absolute hover:before:right-0 hover:before:bottom-0
hover:before:w-[2px] hover:before:h-full hover:before:bg-black">
try your self
</a>
<!-- Include Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<!-- Include Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Include Math.js -->
<script src="https://cdn.jsdelivr.net/npm/mathjs@11.8.0/lib/browser/math.js"></script>
<!-- Include MathJax for LaTeX rendering -->
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body>
<div class="container" role="main" aria-label="Decision Tree Regression Flow Visualization">
<h1>Decision Tree Regression: Step-by-Step Flow</h1>
<div class="stepper" role="tablist" aria-label="Steps of Decision Tree Regression">
<div class="step active" role="tab" aria-selected="true" tabindex="0" aria-controls="step1" id="tab1" data-step="0">
<div class="step-circle">1</div>
Input Data
</div>
<div class="step" role="tab" aria-selected="false" tabindex="-1" aria-controls="step2" id="tab2" data-step="1">
<div class="step-circle">2</div>
Find Split
</div>
<div class="step" role="tab" aria-selected="false" tabindex="-1" aria-controls="step3" id="tab3" data-step="2">
<div class="step-circle">3</div>
Split Data
</div>
<div class="step" role="tab" aria-selected="false" tabindex="-1" aria-controls="step4" id="tab4" data-step="3">
<div class="step-circle">4</div>
Build Tree
</div>
<div class="step" role="tab" aria-selected="false" tabindex="-1" aria-controls="step5" id="tab5" data-step="4">
<div class="step-circle">5</div>
Predict
</div>
</div>
<div class="content">
<!-- Step 1 -->
<div id="step1" class="active" role="tabpanel" aria-labelledby="tab1">
<h2>1. Input Data Points</h2>
<p>The algorithm starts with your input data points (X, y). These points represent the relationship we want to model.</p>
<div class="svg-container" aria-hidden="true">
<svg viewBox="0 0 300 180" >
<rect x="20" y="20" width="260" height="140" fill="#4facfe" fill-opacity="0.1" stroke="#4facfe" stroke-width="2" rx="12" ry="12"/>
<circle cx="60" cy="140" r="8" class="color-data" />
<circle cx="120" cy="100" r="8" class="color-data" />
<circle cx="180" cy="70" r="8" class="color-data" />
<circle cx="240" cy="130" r="8" class="color-data" />
<text x="100" y="40" fill="#4facfe" font-weight="700" font-size="16">Data Points (X, y)</text>
</svg>
</div>
</div>
<!-- Step 2 -->
<div id="step2" role="tabpanel" aria-labelledby="tab2">
<h2>2. Find Best Split</h2>
<p>The algorithm tries different thresholds on the feature (X) to split the data into two groups, aiming to reduce variance in each group.</p>
<div class="svg-container" aria-hidden="true">
<svg viewBox="0 0 300 180" >
<rect x="20" y="20" width="260" height="140" fill="#4facfe" fill-opacity="0.1" stroke="#4facfe" stroke-width="2" rx="12" ry="12"/>
<line x1="150" y1="20" x2="150" y2="160" class="color-split" />
<circle cx="60" cy="140" r="8" class="color-data" />
<circle cx="120" cy="100" r="8" class="color-data" />
<circle cx="180" cy="70" r="8" class="color-data" />
<circle cx="240" cy="130" r="8" class="color-data" />
<text x="160" y="40" fill="#fd746c" font-weight="700" font-size="16">Split Threshold</text>
</svg>
</div>
</div>
<!-- Step 3 -->
<div id="step3" role="tabpanel" aria-labelledby="tab3">
<h2>3. Split Data</h2>
<p>Data is split into left and right groups based on the threshold. Each group is more homogeneous (less variance).</p>
<div class="svg-container" aria-hidden="true">
<svg viewBox="0 0 300 180" >
<rect x="20" y="20" width="120" height="140" class="color-left" rx="12" ry="12"/>
<rect x="160" y="20" width="120" height="140" class="color-right" rx="12" ry="12"/>
<circle cx="60" cy="140" r="8" fill="#56ab2f" />
<circle cx="90" cy="100" r="8" fill="#56ab2f" />
<circle cx="200" cy="70" r="8" fill="#fd746c" />
<circle cx="230" cy="130" r="8" fill="#fd746c" />
<text x="50" y="40" fill="#56ab2f" font-weight="700" font-size="16">Left Group</text>
<text x="190" y="40" fill="#fd746c" font-weight="700" font-size="16">Right Group</text>
</svg>
</div>
</div>
<!-- Step 4 -->
<div id="step4" role="tabpanel" aria-labelledby="tab4">
<h2>4. Recursively Build Subtrees</h2>
<p>The algorithm repeats the splitting process on each group until max depth or minimum variance is reached, building a tree structure.</p>
<div class="svg-container" aria-hidden="true">
<svg viewBox="0 0 300 180" >
<!-- Root node -->
<rect x="120" y="10" width="60" height="40" class="color-node" rx="10" ry="10"/>
<text x="150" y="35" fill="#4facfe" font-weight="700" font-size="16" text-anchor="middle">Root</text>
<!-- Left child -->
<rect x="40" y="90" width="60" height="40" class="color-left" rx="10" ry="10"/>
<text x="70" y="115" fill="#56ab2f" font-weight="700" font-size="16" text-anchor="middle">Left</text>
<!-- Right child -->
<rect x="200" y="90" width="60" height="40" class="color-right" rx="10" ry="10"/>
<text x="230" y="115" fill="#fd746c" font-weight="700" font-size="16" text-anchor="middle">Right</text>
<!-- Lines -->
<line x1="150" y1="50" x2="70" y2="90" class="color-split" stroke-width="3" />
<line x1="150" y1="50" x2="230" y2="90" class="color-split" stroke-width="3" />
</svg>
</div>
</div>
<!-- Step 5 -->
<div id="step5" role="tabpanel" aria-labelledby="tab5">
<h2>5. Make Predictions</h2>
<p>To predict a new point, the tree is traversed from root to leaf by comparing the input to thresholds, returning the leaf's average value.</p>
<div class="svg-container" aria-hidden="true">
<svg viewBox="0 0 300 180" >
<!-- Root node -->
<rect x="120" y="10" width="60" height="40" class="color-node" rx="10" ry="10"/>
<text x="150" y="35" fill="#4facfe" font-weight="700" font-size="16" text-anchor="middle">Root</text>
<!-- Left child -->
<rect x="40" y="90" width="60" height="40" class="color-left" rx="10" ry="10"/>
<text x="70" y="115" fill="#56ab2f" font-weight="700" font-size="16" text-anchor="middle">Left Leaf</text>
<!-- Right child -->
<rect x="200" y="90" width="60" height="40" class="color-right" rx="10" ry="10"/>
<text x="230" y="115" fill="#fd746c" font-weight="700" font-size="16" text-anchor="middle">Right Leaf</text>
<!-- Lines -->
<line x1="150" y1="50" x2="70" y2="90" class="color-split" stroke-width="3" />
<line x1="150" y1="50" x2="230" y2="90" class="color-split" stroke-width="3" />
<!-- Prediction point -->
<circle cx="140" cy="5" r="10" class="color-prediction" />
<text x="160" y="15" fill="#ff00ff" font-weight="700" font-size="14">New X</text>
<!-- Traversal path -->
<line x1="140" y1="15" x2="70" y2="90" class="color-path" />
</svg>
</div>
</div>
</div>
<div class="nav-buttons" role="group" aria-label="Step navigation">
<button id="prevBtn" disabled aria-disabled="true" aria-label="Previous step">← Previous</button>
<button id="nextBtn" aria-label="Next step">Next →</button>
</div>
</div>
<script>
const steps = document.querySelectorAll('.step');
const contents = document.querySelectorAll('.content > div');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
let currentStep = 0;
function updateUI() {
steps.forEach((step, i) => {
step.classList.toggle('active', i === currentStep);
step.classList.toggle('completed', i < currentStep);
step.setAttribute('aria-selected', i === currentStep ? 'true' : 'false');
step.tabIndex = i === currentStep ? 0 : -1;
});
contents.forEach((content, i) => {
content.classList.toggle('active', i === currentStep);
});
prevBtn.disabled = currentStep === 0;
prevBtn.setAttribute('aria-disabled', currentStep === 0 ? 'true' : 'false');
nextBtn.disabled = currentStep === steps.length - 1;
nextBtn.setAttribute('aria-disabled', currentStep === steps.length - 1 ? 'true' : 'false');
}
prevBtn.addEventListener('click', () => {
if (currentStep > 0) {
currentStep--;
updateUI();
}
});
nextBtn.addEventListener('click', () => {
if (currentStep < steps.length - 1) {
currentStep++;
updateUI();
}
});
steps.forEach(step => {
step.addEventListener('click', () => {
currentStep = parseInt(step.dataset.step);
updateUI();
});
step.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
currentStep = parseInt(step.dataset.step);
updateUI();
}
});
});
// Initialize UI
updateUI();
</script>
</body>
</html>
{% endblock %}
|