00
Basics
ML
DataFrame
Quick Start Guide
This example walks you through the three pillars of Deepbox in a single file. You start by creating tensors — the fundamental N-dimensional arrays that power every computation — and performing element-wise addition and computing a mean. Next, you construct a DataFrame from plain objects, giving you a tabular data structure with named columns. Finally, you train a LinearRegression model on synthetic data (y = 2x + 1), split into train/test sets, and verify that the model correctly predicts unseen values.
Deepbox Modules Used
deepbox/ndarraydeepbox/dataframedeepbox/mldeepbox/preprocessWhat You Will Learn
- Create tensors from JavaScript arrays using tensor()
- Perform element-wise arithmetic with add(), and compute statistics with mean()
- Build tabular datasets with DataFrame from plain objects
- Train and evaluate a LinearRegression model end-to-end
- Use trainTestSplit to hold out data for evaluation
Source Code
00-quick-start/index.ts
1import { DataFrame } from "deepbox/dataframe";2import { LinearRegression } from "deepbox/ml";3import { add, mean, tensor } from "deepbox/ndarray";4import { trainTestSplit } from "deepbox/preprocess";56console.log("🚀 Welcome to Deepbox!\n");78// 1. Tensors (N-dimensional arrays)9console.log("1️⃣ Tensors:");10const a = tensor([1, 2, 3, 4, 5]);11const b = tensor([10, 20, 30, 40, 50]);12const c = add(a, b);13console.log(" a + b =", c.toString());14console.log(" mean(a) =", `${mean(a).toString()}\n`);1516// 2. DataFrames (tabular data)17console.log("2️⃣ DataFrames:");18const df = new DataFrame({19 name: ["Alice", "Bob", "Charlie"],20 age: [25, 30, 35],21 score: [85, 90, 78],22});23console.log(`${df.toString()}\n`);2425// 3. Machine Learning26console.log("3️⃣ Machine Learning:");2728// Generate simple data: y = 2x + 129const X = tensor([[1], [2], [3], [4], [5], [6], [7], [8]]);30const y = tensor([3, 5, 7, 9, 11, 13, 15, 17]);3132const [X_train, X_test, y_train, y_test] = trainTestSplit(X, y, {33 testSize: 0.25,34 randomState: 42,35});3637const model = new LinearRegression();38model.fit(X_train, y_train);39const predictions = model.predict(X_test);4041console.log(" Trained linear regression model");42console.log(" Predictions:", predictions.toString());43console.log(" Actual: ", y_test.toString());4445console.log("\n✨ That's Deepbox in a nutshell!");46console.log("📖 Explore the numbered examples (01-32) to learn more.");47console.log("💡 Each example focuses on a specific feature with detailed comments.\n");Console Output
$ npx tsx 00-quick-start/index.ts
🚀 Welcome to Deepbox!
1️⃣ Tensors:
a + b = tensor([11, 22, 33, 44, 55], dtype=float32)
mean(a) = tensor(3, dtype=float64)
2️⃣ DataFrames:
name age score
0 Alice 25 85
1 Bob 30 90
2 Charlie 35 78
3️⃣ Machine Learning:
Trained linear regression model
Predictions: tensor([9, 11], dtype=float32)
Actual: tensor([9, 11], dtype=float32)
✨ That's Deepbox in a nutshell!
📖 Explore the numbered examples (01-32) to learn more.
💡 Each example focuses on a specific feature with detailed comments.