44
Visualization
Tensors
Advanced Visualization
Visualization tools new in v1.0.0: line plots, scatter, histograms, heatmaps, confusion matrices, ROC curves, learning curves, decision boundaries, dendrograms, and 3D scatter. This example uses deepbox/plot, deepbox/ndarray and focuses on plot, scatter, histogram, heatmap, plotConfusionMatrix, plotRocCurve, plotLearningCurve, plotFeatureImportance, plotElbowCurve, plotResiduals, figure, subplot, title, xlabel, ylabel, savefig; tensor, linspace.
Deepbox Modules Used
deepbox/plotdeepbox/ndarrayWhat You Will Learn
- Use deepbox/plot for plot, scatter, histogram, heatmap, plotConfusionMatrix, plotRocCurve, plotLearningCurve, plotFeatureImportance, plotElbowCurve, plotResiduals, figure, subplot, title, xlabel, ylabel, savefig.
- Use deepbox/ndarray for tensor, linspace.
- Visualization tools new in v1.0.0: line plots, scatter, histograms, heatmaps, confusion matrices, ROC curves, learning curves, decision boundaries, dendrograms, and 3D scatter.
Source Files
index.ts
1/**2 * Example 44: Advanced Visualization3 *4 * New in v1.0.0: Comprehensive plotting module with line plots, scatter,5 * histograms, heatmaps, confusion matrices, ROC curves, learning curves,6 * decision boundaries, feature importances, and more.7 */89import { linspace, tensor } from "deepbox/ndarray";10import {11 bar,12 heatmap,13 hist,14 plot,15 plotConfusionMatrix,16 plotElbowCurve,17 plotFeatureImportance,18 plotResiduals,19 plotRocCurve,20 scatter,21 show,22} from "deepbox/plot";2324console.log("=".repeat(60));25console.log("Example 44: Advanced Visualization");26console.log("=".repeat(60));2728// ============================================================================29// Part 1: Line Plot30// ============================================================================31console.log("\n📈 Part 1: Line Plot");32console.log("-".repeat(60));3334// Create x values from 0 to 2π35const x = linspace(0, 2 * Math.PI, 100);36const sinData: number[] = [];37const cosData: number[] = [];38const xData = x.data as Float64Array;3940for (let i = 0; i < x.size; i++) {41 const val = Number(xData[x.offset + i]);42 sinData.push(Math.sin(val));43 cosData.push(Math.cos(val));44}4546const ySin = tensor(sinData);47const yCos = tensor(cosData);4849// Plot sine and cosine curves50plot(x, ySin, { color: "blue", label: "sin(x)" });51plot(x, yCos, { color: "red", label: "cos(x)" });52const lineSvg = show({ format: "svg" });53console.log("Line plot (sin/cos) rendered:");54console.log(` SVG output: ${lineSvg.svg.length} characters`);5556// ============================================================================57// Part 2: Scatter Plot58// ============================================================================59console.log("\n🟠 Part 2: Scatter Plot");60console.log("-".repeat(60));6162const scatterX = tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);63const scatterY = tensor([2.1, 3.9, 6.2, 7.8, 10.1, 12.0, 13.8, 16.1, 18.2, 19.9]);6465scatter(scatterX, scatterY, { color: "blue", label: "data points" });66const scatterSvg = show({ format: "svg" });67console.log("Scatter plot rendered:");68console.log(` SVG output: ${scatterSvg.svg.length} characters`);6970// ============================================================================71// Part 3: Bar Chart72// ============================================================================73console.log("\n📊 Part 3: Bar Chart");74console.log("-".repeat(60));7576const categories = tensor([1, 2, 3, 4, 5]);77const values = tensor([23, 45, 12, 67, 34]);7879bar(categories, values, { color: "steelblue", label: "Sales" });80const barSvg = show({ format: "svg" });81console.log("Bar chart rendered:");82console.log(` SVG output: ${barSvg.svg.length} characters`);8384// ============================================================================85// Part 4: Histogram86// ============================================================================87console.log("\n📉 Part 4: Histogram");88console.log("-".repeat(60));8990// Generate random-like data91const histData: number[] = [];92for (let i = 0; i < 200; i++) {93 // Box-Muller approximation for normal distribution94 const u1 = (i + 1) / 201;95 const u2 = (((i * 7 + 3) % 200) + 1) / 201;96 histData.push(Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2));97}9899hist(tensor(histData), 20, { color: "green", label: "Normal-like data" });100const histSvg = show({ format: "svg" });101console.log("Histogram (20 bins) rendered:");102console.log(` SVG output: ${histSvg.svg.length} characters`);103104// ============================================================================105// Part 5: Heatmap106// ============================================================================107console.log("\n🟥 Part 5: Heatmap");108console.log("-".repeat(60));109110const heatmapData = tensor([111 [1, 2, 3, 4],112 [5, 6, 7, 8],113 [9, 10, 11, 12],114 [13, 14, 15, 16],115]);116117heatmap(heatmapData, { label: "4×4 heatmap" });118const heatSvg = show({ format: "svg" });119console.log("Heatmap rendered:");120console.log(` SVG output: ${heatSvg.svg.length} characters`);121122// ============================================================================123// Part 6: Confusion Matrix124// ============================================================================125console.log("\n🎯 Part 6: Confusion Matrix");126console.log("-".repeat(60));127128const confMatrix = tensor([129 [45, 5, 2],130 [3, 40, 7],131 [1, 4, 43],132]);133134plotConfusionMatrix(confMatrix, ["Cat", "Dog", "Bird"]);135const cmSvg = show({ format: "svg" });136console.log("Confusion Matrix rendered:");137console.log(` SVG output: ${cmSvg.svg.length} characters`);138console.log(" Classes: Cat, Dog, Bird");139140// ============================================================================141// Part 7: ROC Curve142// ============================================================================143console.log("\n📐 Part 7: ROC Curve");144console.log("-".repeat(60));145146// Simulated ROC curve data147const fpr = tensor([0, 0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 1.0]);148const tpr = tensor([0, 0.4, 0.65, 0.8, 0.88, 0.94, 0.98, 1.0]);149150plotRocCurve(fpr, tpr, 0.87);151const rocSvg = show({ format: "svg" });152console.log("ROC Curve rendered (AUC = 0.87):");153console.log(` SVG output: ${rocSvg.svg.length} characters`);154155// ============================================================================156// Part 8: Feature Importance157// ============================================================================158console.log("\n⭐ Part 8: Feature Importance");159console.log("-".repeat(60));160161const importances = tensor([0.35, 0.25, 0.15, 0.12, 0.08, 0.05]);162const featureNames = ["income", "age", "credit_score", "tenure", "balance", "products"];163164plotFeatureImportance(importances, featureNames);165const fiSvg = show({ format: "svg" });166console.log("Feature Importance chart rendered:");167console.log(` SVG output: ${fiSvg.svg.length} characters`);168169// ============================================================================170// Part 9: Elbow Curve171// ============================================================================172console.log("\n📏 Part 9: Elbow Curve (KMeans)");173console.log("-".repeat(60));174175const kValues = tensor([2, 3, 4, 5, 6, 7, 8]);176const inertias = tensor([500, 300, 180, 120, 100, 90, 85]);177178plotElbowCurve(kValues, inertias);179const elbowSvg = show({ format: "svg" });180console.log("Elbow Curve rendered:");181console.log(` SVG output: ${elbowSvg.svg.length} characters`);182console.log(" Optimal k appears around 4-5 (elbow point)");183184// ============================================================================185// Part 10: Residual Plot186// ============================================================================187console.log("\n📊 Part 10: Residual Plot");188console.log("-".repeat(60));189190const yTrue = tensor([3, 5, 7, 9, 11, 13, 15]);191const yPred = tensor([3.1, 4.8, 7.3, 8.7, 11.2, 12.8, 15.1]);192193plotResiduals(yTrue, yPred);194const resSvg = show({ format: "svg" });195console.log("Residual Plot rendered:");196console.log(` SVG output: ${resSvg.svg.length} characters`);197198// ============================================================================199// Summary200// ============================================================================201console.log("\n💡 Key Takeaways");202console.log("-".repeat(60));203console.log("• plot/scatter: line and point plots for continuous data");204console.log("• bar/barh: vertical and horizontal bar charts for categorical data");205console.log("• hist: histograms for distribution visualization");206console.log("• heatmap: 2D color-coded matrix visualization");207console.log("• plotConfusionMatrix: classification performance at a glance");208console.log("• plotRocCurve: binary classifier threshold tradeoffs");209console.log("• plotFeatureImportance: which features matter most");210console.log("• plotElbowCurve: optimal number of clusters (KMeans)");211console.log("• plotResiduals: regression model diagnostics");212console.log("• All plots render to SVG (vector) or PNG (raster)");213214console.log("\n✅ Advanced Visualization Example Complete!");215console.log("=".repeat(60));216Console Output
$ npx tsx 44-advanced-visualization/index.ts
See the example README for the expected console and artifact output.